Files
SabreTools.RedumpLib/SabreTools.RedumpLib.Legacy/Converters/LanguageSelectionConverter.cs
2026-07-05 00:57:52 -04:00

56 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SabreTools.RedumpLib.Legacy.Data;
namespace SabreTools.RedumpLib.Legacy.Converters
{
/// <summary>
/// Serialize LanguageSelection enum values
/// </summary>
public class LanguageSelectionConverter : JsonConverter<LanguageSelection?[]>
{
public override bool CanRead { get { return true; } }
public override LanguageSelection?[] ReadJson(JsonReader reader, Type objectType, LanguageSelection?[]? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
// If we have a value already, don't overwrite it
if (hasExistingValue)
return existingValue ?? [];
// Get the current depth for checking
int currentDepth = reader.Depth;
// Read the array while it exists
List<LanguageSelection> selections = [];
while (reader.Read() && reader.Depth > currentDepth)
{
if (reader.Value is not string value)
continue;
LanguageSelection? sel = value.ToLanguageSelection();
if (sel is not null)
selections.Add(sel.Value);
}
return [.. selections];
}
public override void WriteJson(JsonWriter writer, LanguageSelection?[]? value, JsonSerializer serializer)
{
if (value is null)
return;
JArray array = [];
foreach (var val in value)
{
JToken t = JToken.FromObject(val.LongName() ?? string.Empty);
array.Add(t);
}
array.WriteTo(writer);
}
}
}