From feb20999e3858a9985e2a35670ef08bd96d1d0ae Mon Sep 17 00:00:00 2001 From: Rasmus Soeborg Date: Mon, 16 Sep 2024 13:08:56 +0200 Subject: [PATCH 1/2] refactor: update project configuration and improve JSON conversion handling for TypeId --- ...TypeId.Serialization.SystemTextJson.csproj | 14 ++---- .../TypeIdDecodedConverterFactory.cs | 49 ++++++++++++++++--- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj b/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj index 903fa7b..38bd8f2 100644 --- a/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj +++ b/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj @@ -5,12 +5,13 @@ enable enable FastIDs.TypeId.Serialization.SystemTextJson + 1.0.2 - FastIDs.TypeId.Serialization.SystemTextJson + Sublime.FastIDs.TypeId.Serialization.SystemTextJson System.Text.Json serialization helpers for FastIDs.TypeId Apache-2.0 guid,uuid,id,typeid,type-id,uuid7,identifiers,json,jsonnet,serialization - https://github.com/firenero/TypeId + https://github.com/Sublime-IT/TypeId README.md Mykhailo Matviiv true @@ -34,16 +35,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - typeid-textjson-v - true - - diff --git a/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeIdDecodedConverterFactory.cs b/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeIdDecodedConverterFactory.cs index da29da2..cdd3ca0 100644 --- a/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeIdDecodedConverterFactory.cs +++ b/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeIdDecodedConverterFactory.cs @@ -39,14 +39,51 @@ private sealed class TypeIdDecodedConverter : JsonConverter throw new JsonException($"Cannot convert null to {typeof(T)}."); } - var val = reader.GetString(); - if (!string.IsNullOrEmpty(val)) + if (reader.TokenType == JsonTokenType.String) { - var decoded = TypeId.Parse(val).Decode(); - return (T)(object)decoded; + var val = reader.GetString(); + if (!string.IsNullOrEmpty(val)) + { + var decoded = TypeId.Parse(val).Decode(); + return (T)(object)decoded; + } + + throw new JsonException($"Expected a non-null, non-empty string for {typeof(T)}."); + } + else if (reader.TokenType == JsonTokenType.StartObject) + { + // Deserialize the object representation + var jsonObject = JsonSerializer.Deserialize(ref reader, options); + + if (jsonObject.TryGetProperty("type", out var typeProperty) && + jsonObject.TryGetProperty("id", out var idProperty)) + { + var type = typeProperty.GetString(); + var id = idProperty.GetString(); + + if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(id)) + { + if (!Guid.TryParse(id, out var parsedId)) + throw new JsonException($"The 'id' property must be a valid UUID for {typeof(T)}."); + + // Create the TypeIdDecoded instance from type and id + var typeId = TypeId.FromUuidV7(type, parsedId); + return (T)(object)typeId; + } + else + { + throw new JsonException($"The 'type' and 'id' properties must be non-null strings for {typeof(T)}."); + } + } + else + { + throw new JsonException($"Expected properties 'type' and 'id' in the JSON object for {typeof(T)}."); + } + } + else + { + throw new JsonException($"Unexpected token parsing {typeof(T)}. Expected String or StartObject, got {reader.TokenType}."); } - - throw new JsonException($"Expected a non-null, non-empty string for {typeof(T)}."); } public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) From 0a0e2d93dd2def8f04b0dcc8ce289161b72dec0c Mon Sep 17 00:00:00 2001 From: Rasmus Soeborg Date: Mon, 16 Sep 2024 13:14:24 +0200 Subject: [PATCH 2/2] revert: changes to SystemTextJson.csproj --- ...TypeId.Serialization.SystemTextJson.csproj | 76 ++++++++++--------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj b/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj index 38bd8f2..33617f5 100644 --- a/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj +++ b/src/FastIDs.TypeId.Serialization/TypeId.Serialization.SystemTextJson/TypeId.Serialization.SystemTextJson.csproj @@ -1,41 +1,49 @@ - - net8.0 - enable - enable - FastIDs.TypeId.Serialization.SystemTextJson - 1.0.2 + + net8.0 + enable + enable + FastIDs.TypeId.Serialization.SystemTextJson - Sublime.FastIDs.TypeId.Serialization.SystemTextJson - System.Text.Json serialization helpers for FastIDs.TypeId - Apache-2.0 - guid,uuid,id,typeid,type-id,uuid7,identifiers,json,jsonnet,serialization - https://github.com/Sublime-IT/TypeId - README.md - Mykhailo Matviiv - true - true - snupkg - Copyright (c) Mykhailo Matviiv 2023. - + FastIDs.TypeId.Serialization.SystemTextJson + System.Text.Json serialization helpers for FastIDs.TypeId + Apache-2.0 + guid,uuid,id,typeid,type-id,uuid7,identifiers,json,jsonnet,serialization + https://github.com/firenero/TypeId + README.md + Mykhailo Matviiv + true + true + snupkg + Copyright (c) Mykhailo Matviiv 2023. + - - - + + + - - All - true - + + All + true + - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + - + + typeid-textjson-v + true + + + \ No newline at end of file