| | | 1 | | using System; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace Chronicler; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Serializes and deserializes an object through its canonical state value. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <typeparam name="TRecord">The state-backed record type.</typeparam> |
| | | 11 | | /// <typeparam name="TState">The serializable state type.</typeparam> |
| | | 12 | | public sealed class StateJsonConverter<TRecord, TState> : JsonConverter<TRecord> |
| | | 13 | | where TRecord : class, IStateBacked<TState> |
| | | 14 | | { |
| | | 15 | | private const string StatePropertyName = "State"; |
| | | 16 | | |
| | | 17 | | private readonly Func<TState, TRecord> _factory; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates a converter that restores records from serialized state using the supplied factory. |
| | | 21 | | /// </summary> |
| | 11 | 22 | | public StateJsonConverter(Func<TState, TRecord> factory) |
| | | 23 | | { |
| | 11 | 24 | | _factory = factory ?? throw new ArgumentNullException(nameof(factory)); |
| | 10 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public override TRecord Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 29 | | { |
| | 9 | 30 | | EnsureStartObject(reader.TokenType, typeToConvert); |
| | 8 | 31 | | ReadStateProperty(ref reader, typeToConvert); |
| | 5 | 32 | | TState state = ReadStateValue(ref reader, typeToConvert, options); |
| | 3 | 33 | | EnsureEndObject(ref reader, typeToConvert); |
| | | 34 | | |
| | 1 | 35 | | return _factory(state); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | private static void EnsureStartObject(JsonTokenType tokenType, Type typeToConvert) |
| | | 39 | | { |
| | 9 | 40 | | if (tokenType != JsonTokenType.StartObject) |
| | 1 | 41 | | throw new JsonException($"Expected JSON object for state-backed type '{typeToConvert}'."); |
| | 8 | 42 | | } |
| | | 43 | | |
| | | 44 | | private static void ReadStateProperty(ref Utf8JsonReader reader, Type typeToConvert) |
| | | 45 | | { |
| | 8 | 46 | | if (!reader.Read()) |
| | 1 | 47 | | throw new JsonException($"Expected '{StatePropertyName}' property for state-backed type '{typeToConvert}'.") |
| | 7 | 48 | | if (reader.TokenType != JsonTokenType.PropertyName || !reader.ValueTextEquals(StatePropertyName)) |
| | 2 | 49 | | throw new JsonException($"Expected '{StatePropertyName}' property for state-backed type '{typeToConvert}'.") |
| | 5 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static TState ReadStateValue( |
| | | 53 | | ref Utf8JsonReader reader, |
| | | 54 | | Type typeToConvert, |
| | | 55 | | JsonSerializerOptions options) |
| | | 56 | | { |
| | 5 | 57 | | if (!reader.Read()) |
| | 1 | 58 | | throw new JsonException($"Expected '{StatePropertyName}' value for state-backed type '{typeToConvert}'."); |
| | | 59 | | |
| | 4 | 60 | | TState? state = JsonSerializer.Deserialize<TState>(ref reader, options); |
| | 4 | 61 | | if (state == null) |
| | 1 | 62 | | throw new JsonException($"Unable to deserialize '{StatePropertyName}' for state-backed type '{typeToConvert} |
| | | 63 | | |
| | 3 | 64 | | return state; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private static void EnsureEndObject(ref Utf8JsonReader reader, Type typeToConvert) |
| | | 68 | | { |
| | 3 | 69 | | if (!reader.Read()) |
| | 1 | 70 | | throw new JsonException($"Expected end of JSON object for state-backed type '{typeToConvert}'."); |
| | 2 | 71 | | if (reader.TokenType != JsonTokenType.EndObject) |
| | 1 | 72 | | throw new JsonException($"Expected end of JSON object for state-backed type '{typeToConvert}'."); |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public override void Write(Utf8JsonWriter writer, TRecord value, JsonSerializerOptions options) |
| | | 77 | | { |
| | 2 | 78 | | if (value == null) |
| | | 79 | | { |
| | 1 | 80 | | writer.WriteNullValue(); |
| | 1 | 81 | | return; |
| | | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | writer.WriteStartObject(); |
| | 1 | 85 | | writer.WritePropertyName(StatePropertyName); |
| | 1 | 86 | | JsonSerializer.Serialize(writer, value.State, options); |
| | 1 | 87 | | writer.WriteEndObject(); |
| | 1 | 88 | | } |
| | | 89 | | } |