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
Expand Up @@ -26,15 +26,34 @@ public void TypeId_NestedProperty_Serialized()
json.Should().Be($"{{\"Id\":\"{TypeIdStr}\",\"Value\":42}}");
}

[Test]
public void TypeId_NestedProperty_Null_Serialized()
{
var obj = new TypeIdDecodedContainer(null, 42);
var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be("{\"Id\":null,\"Value\":42}");
}

[Test]
public void TypeId_Collection_Serialized()
{
var obj = new TypeIdDecodedArrayContainer(new[] { TypeId.Parse(TypeIdStr).Decode(), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs").Decode() });
var obj = new TypeIdDecodedArrayContainer(new TypeIdDecoded?[]
{ TypeId.Parse(TypeIdStr).Decode(), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs").Decode() });
var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be($"{{\"Items\":[\"{TypeIdStr}\",\"prefix_0123456789abcdefghjkmnpqrs\"]}}");
}

[Test]
public void TypeId_Collection_WithNull_Serialized()
{
var obj = new TypeIdDecodedArrayContainer(new TypeIdDecoded?[] { TypeId.Parse(TypeIdStr).Decode(), null });
var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be($"{{\"Items\":[\"{TypeIdStr}\",null]}}");
}

[Test]
public void TypeId_Plain_Deserialized()
{
Expand All @@ -43,20 +62,49 @@ public void TypeId_Plain_Deserialized()
typeId.Should().Be(TypeId.Parse(TypeIdStr).Decode());
}

[Test]
public void TypeId_Plain_Null_Deserialized()
{
var typeId = JsonSerializer.Deserialize<TypeIdDecoded?>("null", _options);

typeId.Should().BeNull();
}

[Test]
public void TypeId_NestedProperty_Deserialized()
{
var obj = JsonSerializer.Deserialize<TypeIdDecodedContainer>($"{{\"Id\":\"{TypeIdStr}\",\"Value\":42}}", _options);
var obj = JsonSerializer.Deserialize<TypeIdDecodedContainer>($"{{\"Id\":\"{TypeIdStr}\",\"Value\":42}}",
_options);

obj.Should().Be(new TypeIdDecodedContainer(TypeId.Parse(TypeIdStr).Decode(), 42));
}

[Test]
public void TypeId_NestedProperty_Null_Deserialized()
{
var obj = JsonSerializer.Deserialize<TypeIdDecodedContainer>("{\"Id\":null,\"Value\":42}", _options);

obj.Should().Be(new TypeIdDecodedContainer(null, 42));
}

[Test]
public void TypeId_Collection_Deserialized()
{
var obj = JsonSerializer.Deserialize<TypeIdDecodedArrayContainer>($"{{\"Items\":[\"{TypeIdStr}\",\"prefix_0123456789abcdefghjkmnpqrs\"]}}", _options);
var obj = JsonSerializer.Deserialize<TypeIdDecodedArrayContainer>(
$"{{\"Items\":[\"{TypeIdStr}\",\"prefix_0123456789abcdefghjkmnpqrs\"]}}", _options);

obj.Should().BeEquivalentTo(new TypeIdDecodedArrayContainer(new[] { TypeId.Parse(TypeIdStr).Decode(), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs").Decode() }));
obj.Should().BeEquivalentTo(new TypeIdDecodedArrayContainer(new TypeIdDecoded?[]
{ TypeId.Parse(TypeIdStr).Decode(), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs").Decode() }));
}

[Test]
public void TypeId_Collection_WithNull_Deserialized()
{
var obj = JsonSerializer.Deserialize<TypeIdDecodedArrayContainer>($"{{\"Items\":[\"{TypeIdStr}\",null]}}",
_options);

obj.Should().BeEquivalentTo(new TypeIdDecodedArrayContainer(new TypeIdDecoded?[]
{ TypeId.Parse(TypeIdStr).Decode(), null }));
}

[Test]
Expand All @@ -69,15 +117,44 @@ public void TypeId_DictionaryKey_Serialized()
json.Should().Be($"{{\"{TypeIdStr}\":\"Test\"}}");
}

[Test]
public void TypeId_DictionaryValue_Null_Serialized()
{
var obj = new Dictionary<string, TypeIdDecoded?> { { "Key", null } };

var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be("{\"Key\":null}");
}

[Test]
public void TypeId_DictionaryKey_DeSerialized()
{
var obj = JsonSerializer.Deserialize<Dictionary<TypeIdDecoded, string>>($"{{\"{TypeIdStr}\":\"Test\"}}", _options);
var obj = JsonSerializer.Deserialize<Dictionary<TypeIdDecoded, string>>($"{{\"{TypeIdStr}\":\"Test\"}}",
_options);

obj.Should().BeEquivalentTo(new Dictionary<TypeIdDecoded, string>
{ { TypeId.Parse(TypeIdStr).Decode(), "Test" } });
}

[Test]
public void TypeId_DictionaryKey_Null_DeSerialized()
{
// Deserializing a dictionary with a null key is invalid in JSON; test for exception
Action act = () => JsonSerializer.Deserialize<Dictionary<TypeIdDecoded, string>>("{null:\"Test\"}", _options);

act.Should().Throw<JsonException>();
}

[Test]
public void TypeId_DictionaryValue_Null_DeSerialized()
{
var obj = JsonSerializer.Deserialize<Dictionary<string, TypeIdDecoded?>>("{\"Key\":null}", _options);

obj.Should().BeEquivalentTo(new Dictionary<TypeIdDecoded, string> { { TypeId.Parse(TypeIdStr).Decode(), "Test" } });
obj.Should().BeEquivalentTo(new Dictionary<string, TypeIdDecoded?> { { "Key", null } });
}

private record TypeIdDecodedContainer(TypeIdDecoded Id, int Value);
private record TypeIdDecodedContainer(TypeIdDecoded? Id, int Value);

private record TypeIdDecodedArrayContainer(TypeIdDecoded[] Items);
private record TypeIdDecodedArrayContainer(TypeIdDecoded?[] Items);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public void TypeId_Plain_Serialized()
json.Should().Be($"\"{TypeIdStr}\"");
}

[Test]
public void TypeId_Plain_Null_Serialized()
{
TypeId? typeId = null;
var json = JsonSerializer.Serialize(typeId, _options);

json.Should().Be("null");
}

[Test]
public void TypeId_NestedProperty_Serialized()
{
Expand All @@ -26,15 +35,35 @@ public void TypeId_NestedProperty_Serialized()
json.Should().Be($"{{\"Id\":\"{TypeIdStr}\",\"Value\":42}}");
}

[Test]
public void TypeId_NestedProperty_Null_Serialized()
{
var obj = new TypeIdContainer(null, 42);
var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be($"{{\"Id\":null,\"Value\":42}}");
}

[Test]
public void TypeId_Collection_Serialized()
{
var obj = new TypeIdArrayContainer(new[] { TypeId.Parse(TypeIdStr), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs") });
var obj = new TypeIdArrayContainer(new TypeId?[]
{ TypeId.Parse(TypeIdStr), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs") });
var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be($"{{\"Items\":[\"{TypeIdStr}\",\"prefix_0123456789abcdefghjkmnpqrs\"]}}");
}

[Test]
public void TypeId_Collection_WithNull_Serialized()
{
var obj = new TypeIdArrayContainer(new TypeId?[]
{ TypeId.Parse(TypeIdStr), null });
var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be($"{{\"Items\":[\"{TypeIdStr}\",null]}}");
}

[Test]
public void TypeId_Plain_Deserialized()
{
Expand All @@ -43,6 +72,14 @@ public void TypeId_Plain_Deserialized()
typeId.Should().Be(TypeId.Parse(TypeIdStr));
}

[Test]
public void TypeId_Plain_Null_Deserialized()
{
var typeId = JsonSerializer.Deserialize<TypeId?>("null", _options);

typeId.Should().BeNull();
}

[Test]
public void TypeId_NestedProperty_Deserialized()
{
Expand All @@ -51,23 +88,53 @@ public void TypeId_NestedProperty_Deserialized()
obj.Should().Be(new TypeIdContainer(TypeId.Parse(TypeIdStr), 42));
}

[Test]
public void TypeId_NestedProperty_Null_Deserialized()
{
var obj = JsonSerializer.Deserialize<TypeIdContainer>($"{{\"Id\":null,\"Value\":42}}", _options);

obj.Should().Be(new TypeIdContainer(null, 42));
}

[Test]
public void TypeId_Collection_Deserialized()
{
var obj = JsonSerializer.Deserialize<TypeIdArrayContainer>($"{{\"Items\":[\"{TypeIdStr}\",\"prefix_0123456789abcdefghjkmnpqrs\"]}}", _options);
var obj = JsonSerializer.Deserialize<TypeIdArrayContainer>(
$"{{\"Items\":[\"{TypeIdStr}\",\"prefix_0123456789abcdefghjkmnpqrs\"]}}", _options);

obj.Should().BeEquivalentTo(new TypeIdArrayContainer(new TypeId?[]
{ TypeId.Parse(TypeIdStr), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs") }));
}

[Test]
public void TypeId_Collection_WithNull_Deserialized()
{
var obj = JsonSerializer.Deserialize<TypeIdArrayContainer>(
$"{{\"Items\":[\"{TypeIdStr}\",null]}}", _options);

obj.Should().BeEquivalentTo(new TypeIdArrayContainer(new[] { TypeId.Parse(TypeIdStr), TypeId.Parse("prefix_0123456789abcdefghjkmnpqrs") }));
obj.Should().BeEquivalentTo(new TypeIdArrayContainer(new TypeId?[]
{ TypeId.Parse(TypeIdStr), null }));
}

[Test]
public void TypeId_DictionaryKey_Serialized()
{
var obj = new Dictionary<TypeId, string> { { TypeId.Parse(TypeIdStr), "Test"} };
var obj = new Dictionary<TypeId, string> { { TypeId.Parse(TypeIdStr), "Test" } };

var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be($"{{\"{TypeIdStr}\":\"Test\"}}");
}

[Test]
public void TypeId_DictionaryValue_Null_Serialized()
{
var obj = new Dictionary<string, TypeId?> { { "Key", null } };

var json = JsonSerializer.Serialize(obj, _options);

json.Should().Be("{\"Key\":null}");
}

[Test]
public void TypeId_DictionaryKey_DeSerialized()
Expand All @@ -77,7 +144,23 @@ public void TypeId_DictionaryKey_DeSerialized()
obj.Should().BeEquivalentTo(new Dictionary<TypeId, string> { { TypeId.Parse(TypeIdStr), "Test" } });
}

private record TypeIdContainer(TypeId Id, int Value);
[Test]
public void TypeId_DictionaryKey_Null_DeSerialized()
{
Action act = () => JsonSerializer.Deserialize<Dictionary<TypeId, string>>($"{{null:\"Test\"}}", _options);

act.Should().Throw<JsonException>();
}

[Test]
public void TypeId_DictionaryValue_Null_DeSerialized()
{
var obj = JsonSerializer.Deserialize<Dictionary<string, TypeId?>>("{\"Key\":null}", _options);

obj.Should().BeEquivalentTo(new Dictionary<string, TypeId?> { { "Key", null } });
}

private record TypeIdContainer(TypeId? Id, int Value);

private record TypeIdArrayContainer(TypeId[] Items);
private record TypeIdArrayContainer(TypeId?[] Items);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public static class Extensions
{
public static JsonSerializerOptions ConfigureForTypeId(this JsonSerializerOptions options)
{
options.Converters.Add(new TypeIdConverter());
options.Converters.Add(new TypeIdDecodedConverter());
options.Converters.Add(new TypeIdConverterFactory());
options.Converters.Add(new TypeIdDecodedConverterFactory());
return options;
}
}
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>
Loading