From 886e42fb19b5d69a6a256e20bd1b948124f790a3 Mon Sep 17 00:00:00 2001 From: Thog <me@thog.eu> Date: Thu, 30 Apr 2020 14:07:41 +0200 Subject: Use the official JSON parser (#1151) This remove Utf8son and JsonPrettyPrinter dependencies. NOTE: the standard JSON parser doesn't support configurable indentation, as a result, all the pretty printed JSON are indented with 2 spaces. --- Ryujinx.Common/Utilities/JsonHelper.cs | 106 +++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Ryujinx.Common/Utilities/JsonHelper.cs (limited to 'Ryujinx.Common/Utilities/JsonHelper.cs') diff --git a/Ryujinx.Common/Utilities/JsonHelper.cs b/Ryujinx.Common/Utilities/JsonHelper.cs new file mode 100644 index 00000000..5aa46183 --- /dev/null +++ b/Ryujinx.Common/Utilities/JsonHelper.cs @@ -0,0 +1,106 @@ +using System.IO; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Ryujinx.Common.Utilities +{ + public class JsonHelper + { + public static JsonNamingPolicy SnakeCase { get; } + + private class SnakeCaseNamingPolicy : JsonNamingPolicy + { + public override string ConvertName(string name) + { + if (string.IsNullOrEmpty(name)) + { + return name; + } + + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < name.Length; i++) + { + char c = name[i]; + + if (char.IsUpper(c)) + { + if (i == 0 || char.IsUpper(name[i - 1])) + { + builder.Append(char.ToLowerInvariant(c)); + } + else + { + builder.Append("_"); + builder.Append(char.ToLowerInvariant(c)); + } + } + else + { + builder.Append(c); + } + } + + return builder.ToString(); + } + } + + static JsonHelper() + { + SnakeCase = new SnakeCaseNamingPolicy(); + } + + public static JsonSerializerOptions GetDefaultSerializerOptions(bool prettyPrint = false) + { + JsonSerializerOptions options = new JsonSerializerOptions + { + DictionaryKeyPolicy = SnakeCase, + PropertyNamingPolicy = SnakeCase, + WriteIndented = prettyPrint, + AllowTrailingCommas = true, + ReadCommentHandling = JsonCommentHandling.Skip + }; + + options.Converters.Add(new JsonStringEnumConverter()); + + return options; + } + + public static T Deserialize<T>(Stream stream) + { + using (BinaryReader reader = new BinaryReader(stream)) + { + return JsonSerializer.Deserialize<T>(reader.ReadBytes((int)(stream.Length - stream.Position)), GetDefaultSerializerOptions()); + } + } + + public static T DeserializeFromFile<T>(string path) + { + return Deserialize<T>(File.ReadAllText(path)); + } + + public static T Deserialize<T>(string json) + { + return JsonSerializer.Deserialize<T>(json, GetDefaultSerializerOptions()); + } + + public static void Serialize<TValue>(Stream stream, TValue obj, bool prettyPrint = false) + { + using (BinaryWriter writer = new BinaryWriter(stream)) + { + writer.Write(SerializeToUtf8Bytes(obj, prettyPrint)); + } + } + + public static string Serialize<TValue>(TValue obj, bool prettyPrint = false) + { + return JsonSerializer.Serialize(obj, GetDefaultSerializerOptions(prettyPrint)); + } + + public static byte[] SerializeToUtf8Bytes<T>(T obj, bool prettyPrint = false) + { + return JsonSerializer.SerializeToUtf8Bytes(obj, GetDefaultSerializerOptions(prettyPrint)); + } + } +} -- cgit v1.2.3-70-g09d2