A System.Text.Json converter for the ArchKind type used in editor serialization. It writes the ArchKind as its string Ident and reads either a string name, a legacy numeric ordinal, or null.
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Sunless.Architecture;
// Writes the bare name, which is exactly what every authored plan already carries. Reads a number too, because a
// plan hand-edited or written by a tool that dropped the string converter would carry the ordinal instead.
public sealed class ArchKindConverter : JsonConverter<ArchKind>
{
public override ArchKind Read( ref Utf8JsonReader reader, Type type, JsonSerializerOptions options )
{
return reader.TokenType switch
{
JsonTokenType.String => new ArchKind( reader.GetString() ?? "" ),
JsonTokenType.Number => ArchKindLegacy.Named( reader.GetInt32() ),
JsonTokenType.Null => ArchKind.None,
_ => throw new JsonException( $"An arch kind reads as a name or a legacy ordinal, not {reader.TokenType}." )
};
}
public override void Write( Utf8JsonWriter writer, ArchKind value, JsonSerializerOptions options )
{
writer.WriteStringValue( value.Ident );
}
}