Adds property sorting for json output (#266)

This commit is contained in:
Kamron Batman 2020-09-26 22:38:50 -07:00 • committed by GitHub
parent ddc58ec707
commit 12e935e4f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 228 additions and 130 deletions

View file

@ -138,17 +138,6 @@ namespace Server
Save();
}
public static T GetMetadata<T>(string key) where T : class
{
m_Settings.metadata.TryGetValue(key, out var value);
return value as T;
}
public static void SetMetadata(string key, object value)
{
m_Settings.metadata[key] = value;
}
// If mock is enabled we skip the console readline.
public static void Load(bool mocked = false)
{
@ -291,14 +280,14 @@ namespace Server
internal class ServerSettings
{
[JsonPropertyName("dataDirectories")] public List<string> dataDirectories { get; set; } = new List<string>();
[JsonPropertyName("dataDirectories")]
public List<string> dataDirectories { get; set; } = new List<string>();
[JsonPropertyName("listeners")] public List<IPEndPoint> listeners { get; set; } = new List<IPEndPoint>();
[JsonPropertyName("listeners")]
public List<IPEndPoint> listeners { get; set; } = new List<IPEndPoint>();
[JsonPropertyName("settings")]
public Dictionary<string, string> settings { get; set; } = new Dictionary<string, string>();
[JsonExtensionData] public Dictionary<string, object> metadata { get; set; } = new Dictionary<string, object>();
public SortedDictionary<string, string> settings { get; set; } = new SortedDictionary<string, string>();
}
}
}

View file

@ -859,7 +859,7 @@ namespace Server.Items
if (v == 0)
{
@group.Add(b);
group.Add(b);
}
else
{
@ -971,7 +971,7 @@ namespace Server.Items
if (v == 0)
{
@group.Add(b);
group.Add(b);
}
else
{
@ -1087,7 +1087,7 @@ namespace Server.Items
if (v == 0)
{
@group.Add(b);
group.Add(b);
}
else
{
@ -1404,7 +1404,7 @@ namespace Server.Items
if (v == 0)
{
@group.Add(b);
group.Add(b);
}
else
{
@ -1465,7 +1465,7 @@ namespace Server.Items
if (v == 0)
{
@group.Add(b);
group.Add(b);
}
else
{
@ -1522,7 +1522,7 @@ namespace Server.Items
if (v == 0)
{
@group.Add(b);
group.Add(b);
}
else
{

View file

@ -16,6 +16,7 @@
using System;
using System.Buffers;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
@ -33,7 +34,8 @@ namespace Server.Json
WriteIndented = true,
AllowTrailingCommas = true,
IgnoreNullValues = true,
ReadCommentHandling = JsonCommentHandling.Skip
ReadCommentHandling = JsonCommentHandling.Skip,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
options.Converters.Add(new MapConverterFactory());
@ -64,8 +66,13 @@ namespace Server.Json
return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions);
}
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))
{
File.Delete(filePath);
@ -73,7 +80,7 @@ namespace Server.Json
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
File.WriteAllText(filePath, JsonSerializer.Serialize(value, options ?? DefaultOptions));
File.WriteAllText(filePath, contents);
}
public static T ToObject<T>(this ref Utf8JsonReader reader, JsonSerializerOptions options = null) =>

View file

@ -0,0 +1,100 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: JsonPropertySorter.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
namespace Server.Json
{
public static class JsonUtilities
{
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
{
Indented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
using (var writer = new Utf8JsonWriter(ms, opts))
{
WriteJsonElementSorted(je, writer);
}
ms.TryGetBuffer(out var buffer);
return Encoding.UTF8.GetString(buffer);
}
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}");
}
}
}
}

View file

@ -649,8 +649,8 @@ namespace Server
public virtual bool OnTarget(Mobile m, Target t, object o) => Parent?.OnTarget(m, t, o) != false;
public virtual bool OnCombatantChange(Mobile m, Mobile old, Mobile @new) =>
Parent?.OnCombatantChange(m, old, @new) != false;
public virtual bool OnCombatantChange(Mobile m, Mobile old, Mobile newMobile) =>
Parent?.OnCombatantChange(m, old, newMobile) != false;
public virtual bool AllowHousing(Mobile from, Point3D p) => Parent?.AllowHousing(from, p) != false;