| | | 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> |
| | 1 | 22 | | public StateJsonConverter(Func<TState, TRecord> factory) |
| | | 23 | | { |
| | 1 | 24 | | _factory = factory ?? throw new ArgumentNullException(nameof(factory)); |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public override TRecord Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 29 | | { |
| | 1 | 30 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 0 | 31 | | throw new JsonException($"Expected JSON object for state-backed type '{typeToConvert}'."); |
| | | 32 | | |
| | 1 | 33 | | if (!reader.Read() |
| | 1 | 34 | | || reader.TokenType != JsonTokenType.PropertyName |
| | 1 | 35 | | || !reader.ValueTextEquals(StatePropertyName)) |
| | | 36 | | { |
| | 0 | 37 | | throw new JsonException($"Expected '{StatePropertyName}' property for state-backed type '{typeToConvert}'.") |
| | | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | if (!reader.Read()) |
| | 0 | 41 | | throw new JsonException($"Expected '{StatePropertyName}' value for state-backed type '{typeToConvert}'."); |
| | | 42 | | |
| | 1 | 43 | | TState? state = JsonSerializer.Deserialize<TState>(ref reader, options); |
| | 1 | 44 | | if (state == null) |
| | 0 | 45 | | throw new JsonException($"Unable to deserialize '{StatePropertyName}' for state-backed type '{typeToConvert} |
| | | 46 | | |
| | 1 | 47 | | if (!reader.Read() || reader.TokenType != JsonTokenType.EndObject) |
| | 0 | 48 | | throw new JsonException($"Expected end of JSON object for state-backed type '{typeToConvert}'."); |
| | | 49 | | |
| | 1 | 50 | | return _factory(state); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public override void Write(Utf8JsonWriter writer, TRecord value, JsonSerializerOptions options) |
| | | 55 | | { |
| | 1 | 56 | | if (value == null) |
| | | 57 | | { |
| | 0 | 58 | | writer.WriteNullValue(); |
| | 0 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | writer.WriteStartObject(); |
| | 1 | 63 | | writer.WritePropertyName(StatePropertyName); |
| | 1 | 64 | | JsonSerializer.Serialize(writer, value.State, options); |
| | 1 | 65 | | writer.WriteEndObject(); |
| | 1 | 66 | | } |
| | | 67 | | } |