aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Utilities/JsonHelper.cs
diff options
context:
space:
mode:
authorThog <me@thog.eu>2020-04-30 14:07:41 +0200
committerGitHub <noreply@github.com>2020-04-30 14:07:41 +0200
commit886e42fb19b5d69a6a256e20bd1b948124f790a3 (patch)
tree8648c0a8b3cb82032e9cc9adae411cc8d1dbc8d3 /Ryujinx.Common/Utilities/JsonHelper.cs
parent7ab3fccd4d13bf3ed07a7fa207cfee61b43c56f3 (diff)
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.
Diffstat (limited to 'Ryujinx.Common/Utilities/JsonHelper.cs')
-rw-r--r--Ryujinx.Common/Utilities/JsonHelper.cs106
1 files changed, 106 insertions, 0 deletions
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));
+ }
+ }
+}