Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>FastIDs.TypeId.Serialization.SystemTextJson</RootNamespace>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>FastIDs.TypeId.Serialization.SystemTextJson</RootNamespace>

<PackageId>FastIDs.TypeId.Serialization.SystemTextJson</PackageId>
<Description>System.Text.Json serialization helpers for FastIDs.TypeId</Description>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageTags>guid,uuid,id,typeid,type-id,uuid7,identifiers,json,jsonnet,serialization</PackageTags>
<PackageProjectUrl>https://github.com/firenero/TypeId</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>Mykhailo Matviiv</Authors>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Copyright>Copyright (c) Mykhailo Matviiv 2023.</Copyright>
</PropertyGroup>
<PackageId>FastIDs.TypeId.Serialization.SystemTextJson</PackageId>
<Description>System.Text.Json serialization helpers for FastIDs.TypeId</Description>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageTags>guid,uuid,id,typeid,type-id,uuid7,identifiers,json,jsonnet,serialization</PackageTags>
<PackageProjectUrl>https://github.com/firenero/TypeId</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>Mykhailo Matviiv</Authors>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Copyright>Copyright (c) Mykhailo Matviiv 2023.</Copyright>
</PropertyGroup>

<ItemGroup>
<None Include="../../../README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<None Include="../../../README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<PropertyGroup>
<AnalysisModePerformance>All</AnalysisModePerformance>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<AnalysisModePerformance>All</AnalysisModePerformance>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FastIDs.TypeId" Version="1.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MinVer" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FastIDs.TypeId" Version="1.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MinVer" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="6.0.0" />
</ItemGroup>

<PropertyGroup>
<MinVerTagPrefix>typeid-textjson-v</MinVerTagPrefix>
<MinVerIgnoreHeight>true</MinVerIgnoreHeight>
</PropertyGroup>
<PropertyGroup>
<MinVerTagPrefix>typeid-textjson-v</MinVerTagPrefix>
<MinVerIgnoreHeight>true</MinVerIgnoreHeight>
</PropertyGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,51 @@ private sealed class TypeIdDecodedConverter<T> : JsonConverter<T>
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<JsonElement>(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)
Expand Down