fix: Cleans up core code (#1187)
**Only one functional change** * Fixes a bug in LogFactory where `Warning` is being logged as `Information` Non-functional changes: * Updates/Fixes copyright headers * Removes namespace scopes for core files. View with [whitespace off](https://github.com/modernuo/ModernUO/pull/1187/files?w=1).
This commit is contained in:
parent
0138d40bda
commit
f268d5d4e2
262 changed files with 28527 additions and 28646 deletions
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: ClientVersionConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -25,4 +25,4 @@ public class ClientVersionConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new ClientVersionConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: GuidConverter.cs *
|
||||
* *
|
||||
|
|
@ -33,4 +33,4 @@ public class GuidConverter : JsonConverter<Guid>
|
|||
|
||||
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: GuidConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -25,4 +25,4 @@ public class GuidConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new GuidConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: IPEndPointConverter.cs *
|
||||
* *
|
||||
|
|
@ -34,4 +34,4 @@ public class IPEndPointConverter : JsonConverter<IPEndPoint>
|
|||
|
||||
public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: IPEndPointConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -26,4 +26,4 @@ public class IPEndPointConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new IPEndPointConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: MapConverter.cs *
|
||||
* *
|
||||
|
|
@ -31,4 +31,4 @@ public class MapConverter : JsonConverter<Map>
|
|||
|
||||
public override void Write(Utf8JsonWriter writer, Map value, JsonSerializerOptions options) =>
|
||||
writer.WriteStringValue(value.Name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: MapConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -25,4 +25,4 @@ public class MapConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new MapConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Point2DConverter.cs *
|
||||
* *
|
||||
|
|
@ -107,4 +107,4 @@ public class Point2DConverter : JsonConverter<Point2D>
|
|||
writer.WriteNumberValue(value.Y);
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Point2DConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -26,4 +26,4 @@ public class Point2DConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new Point2DConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Point3DConverter.cs *
|
||||
* *
|
||||
|
|
@ -109,4 +109,4 @@ public class Point3DConverter : JsonConverter<Point3D>
|
|||
writer.WriteNumberValue(value.Z);
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Point3DConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -26,4 +26,4 @@ public class Point3DConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new Point3DConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Rectangle3DConverter.cs *
|
||||
* *
|
||||
|
|
@ -180,4 +180,4 @@ public class Rectangle3DConverter : JsonConverter<Rectangle3D>
|
|||
writer.WriteNumberValue(value.Depth);
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Rectangle3DConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -25,4 +25,4 @@ public class Rectangle3DConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new Rectangle3DConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: TimeSpanConverter.cs *
|
||||
* *
|
||||
|
|
@ -26,4 +26,4 @@ public class TimeSpanConverter : JsonConverter<TimeSpan>
|
|||
|
||||
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: TimeSpanConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -25,4 +25,4 @@ public class TimeSpanConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new TimeSpanConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: TypeConverter.cs *
|
||||
* *
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: TypeConverterFactory.cs *
|
||||
* *
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: WorldLocationConverter.cs *
|
||||
* *
|
||||
|
|
@ -185,4 +185,4 @@ public class WorldLocationConverter : JsonConverter<WorldLocation>
|
|||
writer.WriteStringValue(value.Map.ToString());
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: WorldLocationConverterFactory.cs *
|
||||
* *
|
||||
|
|
@ -25,4 +25,4 @@ public class WorldLocationConverterFactory : JsonConverterFactory
|
|||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new WorldLocationConverter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: DynamicJson.cs *
|
||||
* *
|
||||
|
|
@ -18,50 +18,49 @@ using System.Collections.Generic;
|
|||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Server.Json
|
||||
namespace Server.Json;
|
||||
|
||||
public class DynamicJson
|
||||
{
|
||||
public class DynamicJson
|
||||
public static DynamicJson Create(Type type) => new()
|
||||
{
|
||||
public static DynamicJson Create(Type type) => new()
|
||||
Type = type.Name,
|
||||
Data = new Dictionary<string, JsonElement>()
|
||||
};
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement> Data { get; set; }
|
||||
|
||||
// TODO: Use JSON Node in .NET 6
|
||||
public void SetProperty<T>(string key, JsonSerializerOptions options, T value)
|
||||
{
|
||||
using var doc = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes(value, options));
|
||||
Data[key] = doc.RootElement.Clone();
|
||||
}
|
||||
|
||||
public bool GetProperty<T>(string key, JsonSerializerOptions options, out T t)
|
||||
{
|
||||
if (Data.TryGetValue(key, out var el))
|
||||
{
|
||||
Type = type.Name,
|
||||
Data = new Dictionary<string, JsonElement>()
|
||||
};
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement> Data { get; set; }
|
||||
|
||||
// TODO: Use JSON Node in .NET 6
|
||||
public void SetProperty<T>(string key, JsonSerializerOptions options, T value)
|
||||
{
|
||||
using var doc = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes(value, options));
|
||||
Data[key] = doc.RootElement.Clone();
|
||||
t = el.ToObject<T>(options);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool GetProperty<T>(string key, JsonSerializerOptions options, out T t)
|
||||
{
|
||||
if (Data.TryGetValue(key, out var el))
|
||||
{
|
||||
t = el.ToObject<T>(options);
|
||||
return true;
|
||||
}
|
||||
t = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
t = default;
|
||||
return false;
|
||||
public bool GetEnumProperty<T>(string key, JsonSerializerOptions options, out T t) where T : struct, Enum
|
||||
{
|
||||
if (Data.TryGetValue(key, out var el))
|
||||
{
|
||||
return Enum.TryParse(el.ToObject<string>(options), out t);
|
||||
}
|
||||
|
||||
public bool GetEnumProperty<T>(string key, JsonSerializerOptions options, out T t) where T : struct, Enum
|
||||
{
|
||||
if (Data.TryGetValue(key, out var el))
|
||||
{
|
||||
return Enum.TryParse(el.ToObject<string>(options), out t);
|
||||
}
|
||||
|
||||
t = default;
|
||||
return false;
|
||||
}
|
||||
t = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: JsonConfig.cs *
|
||||
* *
|
||||
|
|
@ -21,93 +21,92 @@ using System.Text.Json;
|
|||
using System.Text.Json.Serialization;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server.Json
|
||||
namespace Server.Json;
|
||||
|
||||
public static class JsonConfig
|
||||
{
|
||||
public static class JsonConfig
|
||||
public static readonly JsonSerializerOptions DefaultOptions = GetOptions();
|
||||
|
||||
public static JsonSerializerOptions GetOptions(params JsonConverterFactory[] converters)
|
||||
{
|
||||
public static readonly JsonSerializerOptions DefaultOptions = GetOptions();
|
||||
|
||||
public static JsonSerializerOptions GetOptions(params JsonConverterFactory[] converters)
|
||||
// In the future this should be optimized by cloning DefaultOptions
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
// In the future this should be optimized by cloning DefaultOptions
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
AllowTrailingCommas = true,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
WriteIndented = true,
|
||||
AllowTrailingCommas = true,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
|
||||
options.Converters.Add(new ClientVersionConverterFactory());
|
||||
options.Converters.Add(new GuidConverterFactory());
|
||||
options.Converters.Add(new JsonStringEnumConverter());
|
||||
options.Converters.Add(new MapConverterFactory());
|
||||
options.Converters.Add(new Point3DConverterFactory());
|
||||
options.Converters.Add(new Rectangle3DConverterFactory());
|
||||
options.Converters.Add(new TimeSpanConverterFactory());
|
||||
options.Converters.Add(new IPEndPointConverterFactory());
|
||||
options.Converters.Add(new TypeConverterFactory());
|
||||
options.Converters.Add(new WorldLocationConverterFactory());
|
||||
options.Converters.Add(new ClientVersionConverterFactory());
|
||||
options.Converters.Add(new GuidConverterFactory());
|
||||
options.Converters.Add(new JsonStringEnumConverter());
|
||||
options.Converters.Add(new MapConverterFactory());
|
||||
options.Converters.Add(new Point3DConverterFactory());
|
||||
options.Converters.Add(new Rectangle3DConverterFactory());
|
||||
options.Converters.Add(new TimeSpanConverterFactory());
|
||||
options.Converters.Add(new IPEndPointConverterFactory());
|
||||
options.Converters.Add(new TypeConverterFactory());
|
||||
options.Converters.Add(new WorldLocationConverterFactory());
|
||||
|
||||
for (var i = 0; i < converters.Length; i++)
|
||||
{
|
||||
options.Converters.Add(converters[i]);
|
||||
}
|
||||
|
||||
return options;
|
||||
for (var i = 0; i < converters.Length; i++)
|
||||
{
|
||||
options.Converters.Add(converters[i]);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(string filePath, JsonSerializerOptions options = null)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
var text = File.ReadAllText(filePath, TextEncoding.UTF8);
|
||||
return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions);
|
||||
public static T Deserialize<T>(string filePath, JsonSerializerOptions options = null)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
public static string Serialize(object value, JsonSerializerOptions options = null) =>
|
||||
JsonSerializer.Serialize(value, options ?? DefaultOptions);
|
||||
var text = File.ReadAllText(filePath, TextEncoding.UTF8);
|
||||
return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions);
|
||||
}
|
||||
|
||||
public static void Serialize(string filePath, object value, JsonSerializerOptions options = null)
|
||||
public static string Serialize(object value, JsonSerializerOptions options = null) =>
|
||||
JsonSerializer.Serialize(value, options ?? DefaultOptions);
|
||||
|
||||
public static void Serialize(string filePath, object value, JsonSerializerOptions options = null)
|
||||
{
|
||||
var contents = Serialize(value, options);
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var contents = Serialize(value, options);
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
|
||||
|
||||
File.WriteAllText(filePath, contents);
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
||||
public static T ToObject<T>(this ref Utf8JsonReader reader, JsonSerializerOptions options = null) =>
|
||||
JsonSerializer.Deserialize<T>(ref reader, options);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
|
||||
|
||||
public static T ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
|
||||
File.WriteAllText(filePath, contents);
|
||||
}
|
||||
|
||||
public static T ToObject<T>(this ref Utf8JsonReader reader, JsonSerializerOptions options = null) =>
|
||||
JsonSerializer.Deserialize<T>(ref reader, options);
|
||||
|
||||
public static T ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
|
||||
{
|
||||
var bufferWriter = new ArrayBufferWriter<byte>();
|
||||
using (var writer = new Utf8JsonWriter(bufferWriter))
|
||||
{
|
||||
var bufferWriter = new ArrayBufferWriter<byte>();
|
||||
using (var writer = new Utf8JsonWriter(bufferWriter))
|
||||
{
|
||||
element.WriteTo(writer);
|
||||
}
|
||||
|
||||
return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options);
|
||||
element.WriteTo(writer);
|
||||
}
|
||||
|
||||
public static T ToObject<T>(this JsonDocument document, JsonSerializerOptions options = null)
|
||||
{
|
||||
if (document == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(document));
|
||||
}
|
||||
return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options);
|
||||
}
|
||||
|
||||
return document.RootElement.ToObject<T>(options);
|
||||
public static T ToObject<T>(this JsonDocument document, JsonSerializerOptions options = null)
|
||||
{
|
||||
if (document == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(document));
|
||||
}
|
||||
|
||||
return document.RootElement.ToObject<T>(options);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: JsonPropertySorter.cs *
|
||||
* *
|
||||
|
|
@ -20,81 +20,80 @@ using System.Text;
|
|||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Server.Json
|
||||
namespace Server.Json;
|
||||
|
||||
public static class JsonUtilities
|
||||
{
|
||||
public static class JsonUtilities
|
||||
public static string SortByPropertyName(string jsonStr)
|
||||
{
|
||||
public static string SortByPropertyName(string jsonStr)
|
||||
using JsonDocument doc = JsonDocument.Parse(jsonStr);
|
||||
return SortByPropertyName(doc.RootElement);
|
||||
}
|
||||
|
||||
public static string SortByPropertyName(JsonElement je)
|
||||
{
|
||||
// TODO: Better way to do this than a stream?
|
||||
using var ms = new MemoryStream();
|
||||
JsonWriterOptions opts = new JsonWriterOptions
|
||||
{
|
||||
using JsonDocument doc = JsonDocument.Parse(jsonStr);
|
||||
return SortByPropertyName(doc.RootElement);
|
||||
Indented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
|
||||
using (var writer = new Utf8JsonWriter(ms, opts))
|
||||
{
|
||||
WriteJsonElementSorted(je, writer);
|
||||
}
|
||||
|
||||
public static string SortByPropertyName(JsonElement je)
|
||||
ms.TryGetBuffer(out var buffer);
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
}
|
||||
|
||||
private static void WriteJsonElementSorted(JsonElement je, Utf8JsonWriter writer)
|
||||
{
|
||||
switch(je.ValueKind)
|
||||
{
|
||||
// TODO: Better way to do this than a stream?
|
||||
using var ms = new MemoryStream();
|
||||
JsonWriterOptions opts = new JsonWriterOptions
|
||||
{
|
||||
Indented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
case JsonValueKind.Object:
|
||||
writer.WriteStartObject();
|
||||
|
||||
using (var writer = new Utf8JsonWriter(ms, opts))
|
||||
{
|
||||
WriteJsonElementSorted(je, writer);
|
||||
}
|
||||
// TODO: This is slow, can make it faster?
|
||||
foreach (JsonProperty x in je.EnumerateObject().OrderBy(prop => prop.Name))
|
||||
{
|
||||
writer.WritePropertyName(x.Name);
|
||||
WriteJsonElementSorted(x.Value, writer);
|
||||
}
|
||||
|
||||
ms.TryGetBuffer(out var buffer);
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
}
|
||||
writer.WriteEndObject();
|
||||
break;
|
||||
case JsonValueKind.Array:
|
||||
writer.WriteStartArray();
|
||||
foreach(JsonElement x in je.EnumerateArray())
|
||||
{
|
||||
WriteJsonElementSorted(x, writer);
|
||||
}
|
||||
writer.WriteEndArray();
|
||||
break;
|
||||
case JsonValueKind.Number:
|
||||
writer.WriteNumberValue(je.GetDouble());
|
||||
break;
|
||||
case JsonValueKind.String:
|
||||
// Escape the string
|
||||
writer.WriteStringValue(je.GetString());
|
||||
break;
|
||||
case JsonValueKind.Null:
|
||||
writer.WriteNullValue();
|
||||
break;
|
||||
case JsonValueKind.True:
|
||||
writer.WriteBooleanValue(true);
|
||||
break;
|
||||
case JsonValueKind.False:
|
||||
writer.WriteBooleanValue(false);
|
||||
break;
|
||||
case JsonValueKind.Undefined: // Don't write anything
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException($"Kind: {je.ValueKind}");
|
||||
|
||||
private static void WriteJsonElementSorted(JsonElement je, Utf8JsonWriter writer)
|
||||
{
|
||||
switch(je.ValueKind)
|
||||
{
|
||||
case JsonValueKind.Object:
|
||||
writer.WriteStartObject();
|
||||
|
||||
// TODO: This is slow, can make it faster?
|
||||
foreach (JsonProperty x in je.EnumerateObject().OrderBy(prop => prop.Name))
|
||||
{
|
||||
writer.WritePropertyName(x.Name);
|
||||
WriteJsonElementSorted(x.Value, writer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
break;
|
||||
case JsonValueKind.Array:
|
||||
writer.WriteStartArray();
|
||||
foreach(JsonElement x in je.EnumerateArray())
|
||||
{
|
||||
WriteJsonElementSorted(x, writer);
|
||||
}
|
||||
writer.WriteEndArray();
|
||||
break;
|
||||
case JsonValueKind.Number:
|
||||
writer.WriteNumberValue(je.GetDouble());
|
||||
break;
|
||||
case JsonValueKind.String:
|
||||
// Escape the string
|
||||
writer.WriteStringValue(je.GetString());
|
||||
break;
|
||||
case JsonValueKind.Null:
|
||||
writer.WriteNullValue();
|
||||
break;
|
||||
case JsonValueKind.True:
|
||||
writer.WriteBooleanValue(true);
|
||||
break;
|
||||
case JsonValueKind.False:
|
||||
writer.WriteBooleanValue(false);
|
||||
break;
|
||||
case JsonValueKind.Undefined: // Don't write anything
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException($"Kind: {je.ValueKind}");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue