Editor/Layers/ArchKindConverter.cs
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 );
	}
}