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-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