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..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,49 +1,49 @@
-
- net8.0
- enable
- enable
- FastIDs.TypeId.Serialization.SystemTextJson
+
+ net8.0
+ enable
+ enable
+ FastIDs.TypeId.Serialization.SystemTextJson
- 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.
-
+ 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
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
-
- typeid-textjson-v
- true
-
+
+ typeid-textjson-v
+ true
+
-
+
\ No newline at end of file
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)