aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common
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
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')
-rw-r--r--Ryujinx.Common/Configuration/ConfigurationFileFormat.cs58
-rw-r--r--Ryujinx.Common/Configuration/Hid/KeyboardHotkeys.cs2
-rw-r--r--Ryujinx.Common/Configuration/Hid/NpadController.cs12
-rw-r--r--Ryujinx.Common/Configuration/Hid/NpadControllerLeft.cs18
-rw-r--r--Ryujinx.Common/Configuration/Hid/NpadControllerRight.cs18
-rw-r--r--Ryujinx.Common/Configuration/Hid/NpadKeyboard.cs6
-rw-r--r--Ryujinx.Common/Configuration/Hid/NpadKeyboardLeft.cs24
-rw-r--r--Ryujinx.Common/Configuration/Hid/NpadKeyboardRight.cs24
-rw-r--r--Ryujinx.Common/Configuration/Ui/GuiColumns.cs20
-rw-r--r--Ryujinx.Common/Logging/Targets/JsonLogTarget.cs12
-rw-r--r--Ryujinx.Common/Ryujinx.Common.csproj4
-rw-r--r--Ryujinx.Common/Utilities/JsonHelper.cs106
12 files changed, 182 insertions, 122 deletions
diff --git a/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs b/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs
index 812dc2c3..ff5a67c4 100644
--- a/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs
+++ b/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs
@@ -1,16 +1,12 @@
-using JsonPrettyPrinterPlus;
-using Ryujinx.Common.Logging;
-using System;
using System.Collections.Generic;
using System.IO;
-using System.Text;
-using Utf8Json;
-using Utf8Json.Resolvers;
+using Ryujinx.Common.Configuration.Hid;
+using Ryujinx.Common.Logging;
+using Ryujinx.Common.Utilities;
using Ryujinx.Configuration.System;
using Ryujinx.Configuration.Hid;
-using Ryujinx.Common.Configuration.Hid;
-using Ryujinx.UI.Input;
using Ryujinx.Configuration.Ui;
+using Ryujinx.UI.Input;
namespace Ryujinx.Configuration
{
@@ -179,15 +175,7 @@ namespace Ryujinx.Configuration
/// <param name="path">The path to the JSON configuration file</param>
public static ConfigurationFileFormat Load(string path)
{
- var resolver = CompositeResolver.Create(
- new[] { new ConfigurationEnumFormatter<Key>() },
- new[] { StandardResolver.AllowPrivateSnakeCase }
- );
-
- using (Stream stream = File.OpenRead(path))
- {
- return JsonSerializer.Deserialize<ConfigurationFileFormat>(stream, resolver);
- }
+ return JsonHelper.DeserializeFromFile<ConfigurationFileFormat>(path);
}
/// <summary>
@@ -196,41 +184,7 @@ namespace Ryujinx.Configuration
/// <param name="path">The path to the JSON configuration file</param>
public void SaveConfig(string path)
{
- IJsonFormatterResolver resolver = CompositeResolver.Create(
- new[] { new ConfigurationEnumFormatter<Key>() },
- new[] { StandardResolver.AllowPrivateSnakeCase }
- );
-
- byte[] data = JsonSerializer.Serialize(this, resolver);
- File.WriteAllText(path, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
- }
-
- public class ConfigurationEnumFormatter<T> : IJsonFormatter<T>
- where T : struct
- {
- public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
- {
- formatterResolver.GetFormatterWithVerify<string>()
- .Serialize(ref writer, value.ToString(), formatterResolver);
- }
-
- public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
- {
- if (reader.ReadIsNull())
- {
- return default(T);
- }
-
- string enumName = formatterResolver.GetFormatterWithVerify<string>()
- .Deserialize(ref reader, formatterResolver);
-
- if (Enum.TryParse<T>(enumName, out T result))
- {
- return result;
- }
-
- return default(T);
- }
+ File.WriteAllText(path, JsonHelper.Serialize(this, true));
}
}
} \ No newline at end of file
diff --git a/Ryujinx.Common/Configuration/Hid/KeyboardHotkeys.cs b/Ryujinx.Common/Configuration/Hid/KeyboardHotkeys.cs
index 1d0b0504..30cc8d84 100644
--- a/Ryujinx.Common/Configuration/Hid/KeyboardHotkeys.cs
+++ b/Ryujinx.Common/Configuration/Hid/KeyboardHotkeys.cs
@@ -2,6 +2,6 @@
{
public struct KeyboardHotkeys
{
- public Key ToggleVsync;
+ public Key ToggleVsync { get; set; }
}
}
diff --git a/Ryujinx.Common/Configuration/Hid/NpadController.cs b/Ryujinx.Common/Configuration/Hid/NpadController.cs
index f00865d5..94b985d5 100644
--- a/Ryujinx.Common/Configuration/Hid/NpadController.cs
+++ b/Ryujinx.Common/Configuration/Hid/NpadController.cs
@@ -5,31 +5,31 @@
/// <summary>
/// Enables or disables controller support
/// </summary>
- public bool Enabled;
+ public bool Enabled { get; set; }
/// <summary>
/// Controller Device Index
/// </summary>
- public int Index;
+ public int Index { get; set; }
/// <summary>
/// Controller Analog Stick Deadzone
/// </summary>
- public float Deadzone;
+ public float Deadzone { get; set; }
/// <summary>
/// Controller Trigger Threshold
/// </summary>
- public float TriggerThreshold;
+ public float TriggerThreshold { get; set; }
/// <summary>
/// Left JoyCon Controller Bindings
/// </summary>
- public NpadControllerLeft LeftJoycon;
+ public NpadControllerLeft LeftJoycon { get; set; }
/// <summary>
/// Right JoyCon Controller Bindings
/// </summary>
- public NpadControllerRight RightJoycon;
+ public NpadControllerRight RightJoycon { get; set; }
}
}
diff --git a/Ryujinx.Common/Configuration/Hid/NpadControllerLeft.cs b/Ryujinx.Common/Configuration/Hid/NpadControllerLeft.cs
index 54ac0f03..c221b5e8 100644
--- a/Ryujinx.Common/Configuration/Hid/NpadControllerLeft.cs
+++ b/Ryujinx.Common/Configuration/Hid/NpadControllerLeft.cs
@@ -2,14 +2,14 @@
{
public struct NpadControllerLeft
{
- public ControllerInputId Stick;
- public ControllerInputId StickButton;
- public ControllerInputId ButtonMinus;
- public ControllerInputId ButtonL;
- public ControllerInputId ButtonZl;
- public ControllerInputId DPadUp;
- public ControllerInputId DPadDown;
- public ControllerInputId DPadLeft;
- public ControllerInputId DPadRight;
+ public ControllerInputId Stick { get; set; }
+ public ControllerInputId StickButton { get; set; }
+ public ControllerInputId ButtonMinus { get; set; }
+ public ControllerInputId ButtonL { get; set; }
+ public ControllerInputId ButtonZl { get; set; }
+ public ControllerInputId DPadUp { get; set; }
+ public ControllerInputId DPadDown { get; set; }
+ public ControllerInputId DPadLeft { get; set; }
+ public ControllerInputId DPadRight { get; set; }
}
}
diff --git a/Ryujinx.Common/Configuration/Hid/NpadControllerRight.cs b/Ryujinx.Common/Configuration/Hid/NpadControllerRight.cs
index 315136d9..f52f6f16 100644
--- a/Ryujinx.Common/Configuration/Hid/NpadControllerRight.cs
+++ b/Ryujinx.Common/Configuration/Hid/NpadControllerRight.cs
@@ -2,14 +2,14 @@
{
public struct NpadControllerRight
{
- public ControllerInputId Stick;
- public ControllerInputId StickButton;
- public ControllerInputId ButtonA;
- public ControllerInputId ButtonB;
- public ControllerInputId ButtonX;
- public ControllerInputId ButtonY;
- public ControllerInputId ButtonPlus;
- public ControllerInputId ButtonR;
- public ControllerInputId ButtonZr;
+ public ControllerInputId Stick { get; set; }
+ public ControllerInputId StickButton { get; set; }
+ public ControllerInputId ButtonA { get; set; }
+ public ControllerInputId ButtonB { get; set; }
+ public ControllerInputId ButtonX { get; set; }
+ public ControllerInputId ButtonY { get; set; }
+ public ControllerInputId ButtonPlus { get; set; }
+ public ControllerInputId ButtonR { get; set; }
+ public ControllerInputId ButtonZr { get; set; }
}
}
diff --git a/Ryujinx.Common/Configuration/Hid/NpadKeyboard.cs b/Ryujinx.Common/Configuration/Hid/NpadKeyboard.cs
index 911f5119..5ae82756 100644
--- a/Ryujinx.Common/Configuration/Hid/NpadKeyboard.cs
+++ b/Ryujinx.Common/Configuration/Hid/NpadKeyboard.cs
@@ -5,16 +5,16 @@ namespace Ryujinx.UI.Input
/// <summary>
/// Left JoyCon Keyboard Bindings
/// </summary>
- public Configuration.Hid.NpadKeyboardLeft LeftJoycon;
+ public Configuration.Hid.NpadKeyboardLeft LeftJoycon { get; set; }
/// <summary>
/// Right JoyCon Keyboard Bindings
/// </summary>
- public Configuration.Hid.NpadKeyboardRight RightJoycon;
+ public Configuration.Hid.NpadKeyboardRight RightJoycon { get; set; }
/// <summary>
/// Hotkey Keyboard Bindings
/// </summary>
- public Configuration.Hid.KeyboardHotkeys Hotkeys;
+ public Configuration.Hid.KeyboardHotkeys Hotkeys { get; set; }
}
}
diff --git a/Ryujinx.Common/Configuration/Hid/NpadKeyboardLeft.cs b/Ryujinx.Common/Configuration/Hid/NpadKeyboardLeft.cs
index 799cdfdb..4a61d932 100644
--- a/Ryujinx.Common/Configuration/Hid/NpadKeyboardLeft.cs
+++ b/Ryujinx.Common/Configuration/Hid/NpadKeyboardLeft.cs
@@ -2,17 +2,17 @@
{
public struct NpadKeyboardLeft
{
- public Key StickUp;
- public Key StickDown;
- public Key StickLeft;
- public Key StickRight;
- public Key StickButton;
- public Key DPadUp;
- public Key DPadDown;
- public Key DPadLeft;
- public Key DPadRight;
- public Key ButtonMinus;
- public Key ButtonL;
- public Key ButtonZl;
+ public Key StickUp { get; set; }
+ public Key StickDown { get; set; }
+ public Key StickLeft { get; set; }
+ public Key StickRight { get; set; }
+ public Key StickButton { get; set; }
+ public Key DPadUp { get; set; }
+ public Key DPadDown { get; set; }
+ public Key DPadLeft { get; set; }
+ public Key DPadRight { get; set; }
+ public Key ButtonMinus { get; set; }
+ public Key ButtonL { get; set; }
+ public Key ButtonZl { get; set; }
}
}
diff --git a/Ryujinx.Common/Configuration/Hid/NpadKeyboardRight.cs b/Ryujinx.Common/Configuration/Hid/NpadKeyboardRight.cs
index 311504bb..0677b573 100644
--- a/Ryujinx.Common/Configuration/Hid/NpadKeyboardRight.cs
+++ b/Ryujinx.Common/Configuration/Hid/NpadKeyboardRight.cs
@@ -2,17 +2,17 @@
{
public struct NpadKeyboardRight
{
- public Key StickUp;
- public Key StickDown;
- public Key StickLeft;
- public Key StickRight;
- public Key StickButton;
- public Key ButtonA;
- public Key ButtonB;
- public Key ButtonX;
- public Key ButtonY;
- public Key ButtonPlus;
- public Key ButtonR;
- public Key ButtonZr;
+ public Key StickUp { get; set; }
+ public Key StickDown { get; set; }
+ public Key StickLeft { get; set; }
+ public Key StickRight { get; set; }
+ public Key StickButton { get; set; }
+ public Key ButtonA { get; set; }
+ public Key ButtonB { get; set; }
+ public Key ButtonX { get; set; }
+ public Key ButtonY { get; set; }
+ public Key ButtonPlus { get; set; }
+ public Key ButtonR { get; set; }
+ public Key ButtonZr { get; set; }
}
}
diff --git a/Ryujinx.Common/Configuration/Ui/GuiColumns.cs b/Ryujinx.Common/Configuration/Ui/GuiColumns.cs
index 2b3524aa..de4f7369 100644
--- a/Ryujinx.Common/Configuration/Ui/GuiColumns.cs
+++ b/Ryujinx.Common/Configuration/Ui/GuiColumns.cs
@@ -2,15 +2,15 @@
{
public struct GuiColumns
{
- public bool FavColumn;
- public bool IconColumn;
- public bool AppColumn;
- public bool DevColumn;
- public bool VersionColumn;
- public bool TimePlayedColumn;
- public bool LastPlayedColumn;
- public bool FileExtColumn;
- public bool FileSizeColumn;
- public bool PathColumn;
+ public bool FavColumn { get; set; }
+ public bool IconColumn { get; set; }
+ public bool AppColumn { get; set; }
+ public bool DevColumn { get; set; }
+ public bool VersionColumn { get; set; }
+ public bool TimePlayedColumn { get; set; }
+ public bool LastPlayedColumn { get; set; }
+ public bool FileExtColumn { get; set; }
+ public bool FileSizeColumn { get; set; }
+ public bool PathColumn { get; set; }
}
}
diff --git a/Ryujinx.Common/Logging/Targets/JsonLogTarget.cs b/Ryujinx.Common/Logging/Targets/JsonLogTarget.cs
index 3729b18d..95f96576 100644
--- a/Ryujinx.Common/Logging/Targets/JsonLogTarget.cs
+++ b/Ryujinx.Common/Logging/Targets/JsonLogTarget.cs
@@ -1,6 +1,5 @@
-using System;
-using System.IO;
-using Utf8Json;
+using System.IO;
+using System.Text.Json;
namespace Ryujinx.Common.Logging
{
@@ -26,7 +25,12 @@ namespace Ryujinx.Common.Logging
public void Log(object sender, LogEventArgs e)
{
- JsonSerializer.Serialize(_stream, e);
+ string text = JsonSerializer.Serialize(e);
+
+ using (BinaryWriter writer = new BinaryWriter(_stream))
+ {
+ writer.Write(text);
+ }
}
public void Dispose()
diff --git a/Ryujinx.Common/Ryujinx.Common.csproj b/Ryujinx.Common/Ryujinx.Common.csproj
index e902d26a..3eb75730 100644
--- a/Ryujinx.Common/Ryujinx.Common.csproj
+++ b/Ryujinx.Common/Ryujinx.Common.csproj
@@ -27,12 +27,8 @@
</PropertyGroup>
<ItemGroup>
- <PackageReference Include="JsonPrettyPrinter" Version="1.0.1.1">
- <NoWarn>NU1701</NoWarn>
- </PackageReference>
<PackageReference Include="MsgPack.Cli" Version="1.0.1" />
<PackageReference Include="System.Management" Version="4.7.0" />
- <PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>
</Project>
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));
+ }
+ }
+}