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;

View file

@ -25,16 +25,16 @@ namespace Server.Configurations
{
private const string m_RelPath = "Configuration/email-settings.json";
public static bool EmailEnabled { get; private set; }
public static MailboxAddress FromAddress { get; private set; }
public static MailboxAddress CrashAddress { get; private set; }
public static MailboxAddress SpeechLogPageAddress { get; private set; }
public static string EmailServer { get; private set; }
public static bool EmailEnabled { get; private set; }
public static int EmailPort { get; private set; }
public static string EmailServerUsername { get; private set; }
public static string EmailServerPassword { get; private set; }
public static int EmailSendRetryCount { get; private set; } // seconds
public static int EmailSendRetryDelay { get; private set; } // seconds
public static string EmailServer { get; private set; }
public static string EmailServerUsername { get; private set; }
public static string EmailServerPassword { get; private set; }
public static MailboxAddress FromAddress { get; private set; }
public static MailboxAddress SpeechLogPageAddress { get; private set; }
public static void Configure()
{

View file

@ -173,7 +173,7 @@ namespace Server.Engines.BulkOrders
if (points >= group.Points)
{
return @group;
return group;
}
}

View file

@ -498,7 +498,7 @@ namespace Server.Engines.Craft
if (index >= 0 && index < group.CraftItems.Count)
{
CraftItem(@group.CraftItems[index]);
CraftItem(group.CraftItems[index]);
}
}
@ -519,7 +519,7 @@ namespace Server.Engines.Craft
if (index >= 0 && index < group.CraftItems.Count)
{
m_From.SendGump(new CraftGumpItem(m_From, system, @group.CraftItems[index], m_Tool));
m_From.SendGump(new CraftGumpItem(m_From, system, group.CraftItems[index], m_Tool));
}
}

View file

@ -133,7 +133,7 @@ namespace Server.Guilds
if (pm == null || !IsMember(pm, guild) || !pm.GuildRank.GetFlag(RankFlags.CanInvitePlayer))
{
pm.SendLocalizedMessage(503301); // You don't have permission to do that.
from.SendLocalizedMessage(503301); // You don't have permission to do that.
}
else if (targ == null)
{

View file

@ -306,12 +306,15 @@ namespace Server.Gumps
AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID);
}
if (group == selected)
if (group != selected)
{
continue;
}
var indentMaskX = BorderSize;
var indentMaskY = y + EntryHeight + OffsetSize;
for (var j = 0; j < group.Skills.Length; ++j)
for (var j = 0; j < group!.Skills.Length; ++j)
{
var sk = target.Skills[group.Skills[j]];
@ -409,7 +412,6 @@ namespace Server.Gumps
);
}
}
}
public override void OnResponse(NetState sender, RelayInfo info)
{

View file

@ -120,7 +120,7 @@ namespace Server
for (var j = 0; !contains && j < group.Length; ++j)
{
contains = @group[j].IsAssignableFrom(type);
contains = group[j].IsAssignableFrom(type);
}
if (contains)

View file

@ -226,7 +226,7 @@ namespace Server.Spells.Necromancy
if (contains)
{
return @group;
return group;
}
}