Use correct JsonTypeInfo implementation

This commit is contained in:
LotP1 2024-12-16 21:50:02 +01:00
parent b98e762df8
commit 267357c3e3
2 changed files with 12 additions and 7 deletions

View File

@ -11,7 +11,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization.Metadata; using System.Text.Json.Serialization;
using System.Text.Unicode; using System.Text.Unicode;
namespace Ryujinx.Ava.Common.Locale namespace Ryujinx.Ava.Common.Locale
@ -160,8 +160,7 @@ namespace Ryujinx.Ava.Common.Locale
return null; return null;
} }
JsonSerializerOptions helperOptions = JsonHelper.GetDefaultSerializerOptions(); LocalesJSON json = JsonHelper.Deserialize(fileData, LocalesJSONContext.Default.LocalesJSON);
LocalesJSON json = JsonHelper.Deserialize(fileData, (JsonTypeInfo<LocalesJSON>)helperOptions.GetTypeInfo(typeof(LocalesJSON)));
foreach (LocalesEntry locale in json.Locales) foreach (LocalesEntry locale in json.Locales)
{ {
@ -211,12 +210,15 @@ namespace Ryujinx.Ava.Common.Locale
string location = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.GetDirectories("Assets")[0].GetFiles("locales.json")[0].FullName; string location = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.GetDirectories("Assets")[0].GetFiles("locales.json")[0].FullName;
JsonSerializerOptions options = new JsonSerializerOptions JsonSerializerOptions jsonOptions = new JsonSerializerOptions
{ {
WriteIndented = true, WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
}; };
string jsonString = JsonSerializer.Serialize(json, options);//GetSubEventString(rootEvent, 0);
LocalesJSONContext context = new LocalesJSONContext(jsonOptions);
string jsonString = JsonHelper.Serialize(json, context.LocalesJSON);
using (StreamWriter sw = new StreamWriter(location)) using (StreamWriter sw = new StreamWriter(location))
{ {
@ -241,4 +243,8 @@ namespace Ryujinx.Ava.Common.Locale
public string ID { get; set; } public string ID { get; set; }
public Dictionary<string, string> Translations { get; set; } public Dictionary<string, string> Translations { get; set; }
} }
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(LocalesJSON))]
internal partial class LocalesJSONContext : JsonSerializerContext { }
} }

View File

@ -60,8 +60,7 @@ namespace Ryujinx.Ava.UI.Views.Main
string languageJson = EmbeddedResources.ReadAllText(localePath); string languageJson = EmbeddedResources.ReadAllText(localePath);
JsonSerializerOptions helperOptions = JsonHelper.GetDefaultSerializerOptions(); LocalesJSON locales = JsonHelper.Deserialize(languageJson, LocalesJSONContext.Default.LocalesJSON);
LocalesJSON locales = JsonHelper.Deserialize(languageJson, (JsonTypeInfo<LocalesJSON>)helperOptions.GetTypeInfo(typeof(LocalesJSON)));
foreach (string language in locales.Languages) foreach (string language in locales.Languages)
{ {