From 78e197eb1820e7777bb23575e75a08e9b4375219 Mon Sep 17 00:00:00 2001 From: zorgoz Date: Thu, 21 Nov 2024 11:36:05 +0100 Subject: [PATCH] Update UlidJsonConverter.cs Added property name handling --- src/Ulid/UlidJsonConverter.cs | 110 ++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/src/Ulid/UlidJsonConverter.cs b/src/Ulid/UlidJsonConverter.cs index 3aa0d78..8432a76 100644 --- a/src/Ulid/UlidJsonConverter.cs +++ b/src/Ulid/UlidJsonConverter.cs @@ -7,53 +7,71 @@ namespace Cysharp.Serialization.Json { - public class UlidJsonConverter : JsonConverter + public class UlidJsonConverter: JsonConverter { - /// - /// Read a Ulid value represented by a string from JSON. - /// - public override Ulid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - try - { - if (reader.TokenType != JsonTokenType.String) throw new JsonException("Expected string"); - - if (reader.HasValueSequence) - { - // Parse using ValueSequence - var seq = reader.ValueSequence; - if (seq.Length != 26) throw new JsonException("Ulid invalid: length must be 26"); - Span buf = stackalloc byte[26]; - seq.CopyTo(buf); - Ulid.TryParse(buf, out var ulid); - return ulid; - } - else - { - // Parse usign ValueSpan - var buf = reader.ValueSpan; - if (buf.Length != 26) throw new JsonException("Ulid invalid: length must be 26"); - Ulid.TryParse(buf, out var ulid); - return ulid; - } - } - catch (IndexOutOfRangeException e) - { - throw new JsonException("Ulid invalid: length must be 26", e); - } - catch (OverflowException e) - { - throw new JsonException("Ulid invalid: invalid character", e); - } - } - - public override void Write(Utf8JsonWriter writer, Ulid value, JsonSerializerOptions options) - { - Span buf = stackalloc byte[26]; - value.TryWriteStringify(buf); - writer.WriteStringValue(buf); - } + /// + /// Read a Ulid value represented by a string from JSON. + /// + public override Ulid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + try + { + if (reader.TokenType != JsonTokenType.String && reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Expected string"); + + if (reader.HasValueSequence) + { + // Parse using ValueSequence + var seq = reader.ValueSequence; + if (seq.Length != 26) throw new JsonException("Ulid invalid: length must be 26"); + Span buf = stackalloc byte[26]; + seq.CopyTo(buf); + Ulid.TryParse(buf, out var ulid); + return ulid; + } + else + { + // Parse usign ValueSpan + var buf = reader.ValueSpan; + if (buf.Length != 26) throw new JsonException("Ulid invalid: length must be 26"); + Ulid.TryParse(buf, out var ulid); + return ulid; + } + } + catch (IndexOutOfRangeException e) + { + throw new JsonException("Ulid invalid: length must be 26", e); + } + catch (OverflowException e) + { + throw new JsonException("Ulid invalid: invalid character", e); + } + } + + public override void Write(Utf8JsonWriter writer, Ulid value, JsonSerializerOptions options) + { + Span buf = stackalloc byte[26]; + value.TryWriteStringify(buf); + writer.WriteStringValue(buf); + } + + public override void WriteAsPropertyName( + Utf8JsonWriter writer, + Ulid value, + JsonSerializerOptions options) + { + Span buf = stackalloc byte[26]; + value.TryWriteStringify(buf); + writer.WritePropertyName(buf); + } + + public override Ulid ReadAsPropertyName( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options) + { + return Read(ref reader, typeToConvert, options); + } } } -#endif \ No newline at end of file +#endif