Skip to content

Commit c9295b1

Browse files
committed
Fixed problem with dictionaries in Ext.SystemTextJson
1 parent ceaaf3a commit c9295b1

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

Enum.Ext/Enum.Ext.SystemTextJson.Tests/ConvertTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Enum.Ext.Tests.Shared;
33
using FluentAssertions;
44
using NUnit.Framework;
5+
using System.Collections.Generic;
6+
using System.Linq;
57
using System.Text.Json;
68

79
namespace Enum.Ext.SystemTextJson.Tests
@@ -81,5 +83,49 @@ public void Test_ConvertToNullFromJsonValueWithAnnotation()
8183

8284
tempClass.Item.Should().Be(null);
8385
}
86+
87+
[Test]
88+
public void Test_ConvertDictonaryKeyToJsonWithAnnotation()
89+
{
90+
var tempClass = new Dictionary<WeekdayWithAnnotation, string>
91+
{
92+
{ WeekdayWithAnnotation.Monday, "value" }
93+
};
94+
95+
var json = JsonSerializer.Serialize(tempClass);
96+
97+
json.Should().Be("{\"1\":\"value\"}");
98+
}
99+
100+
[Test]
101+
public void Test_ConvertDictonaryKeyFormJsonWithAnnotation()
102+
{
103+
var tempClass = JsonSerializer.Deserialize<Dictionary<WeekdayWithAnnotation, string>>("{\"1\":\"value\"}");
104+
105+
tempClass.Keys.First().Should().Be(WeekdayWithAnnotation.Monday);
106+
tempClass[WeekdayWithAnnotation.Monday].Should().Be("value");
107+
}
108+
109+
[Test]
110+
public void Test_ConvertDictonaryValueToJsonWithAnnotation()
111+
{
112+
var tempClass = new Dictionary<string, WeekdayWithAnnotation>
113+
{
114+
{ "value", WeekdayWithAnnotation.Monday }
115+
};
116+
117+
var json = JsonSerializer.Serialize(tempClass);
118+
119+
json.Should().Be("{\"value\":1}");
120+
}
121+
122+
[Test]
123+
public void Test_ConvertDictonaryValueFormJsonWithAnnotation()
124+
{
125+
var tempClass = JsonSerializer.Deserialize<Dictionary<string, WeekdayWithAnnotation>>("{\"value\":1}");
126+
127+
tempClass.Keys.First().Should().Be("value");
128+
tempClass["value"].Should().Be(WeekdayWithAnnotation.Monday);
129+
}
84130
}
85131
}

Enum.Ext/Enum.Ext.SystemTextJson/Enum.Ext.SystemTextJson.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.0.7</Version>
6+
<Version>1.0.8</Version>
77
<Authors>Mauracher Simon, Juen Marcel</Authors>
88
<PackageProjectUrl>https://github.com/simonmau/enum_ext</PackageProjectUrl>
99
<RepositoryUrl>https://github.com/simonmau/enum_ext</RepositoryUrl>
@@ -26,7 +26,7 @@
2626

2727
<ItemGroup>
2828
<PackageReference Include="Enum.Ext" Version="1.0.7" />
29-
<PackageReference Include="System.Text.Json" Version="6.0.0" />
29+
<PackageReference Include="System.Text.Json" Version="7.0.0" />
3030
</ItemGroup>
3131

3232
</Project>

Enum.Ext/Enum.Ext.SystemTextJson/JsonTypeSafeEnumConverter.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ public override void Write(Utf8JsonWriter writer, TypeSafeEnum<TValue, TKey> val
5959
JsonSerializer.Serialize(writer, value.Id, options);
6060
}
6161

62+
public override void WriteAsPropertyName(Utf8JsonWriter writer, TypeSafeEnum<TValue, TKey> value, JsonSerializerOptions options)
63+
{
64+
writer.WritePropertyName(value.Id.ToString());
65+
}
66+
67+
public override TypeSafeEnum<TValue, TKey> ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
68+
{
69+
var item = reader.GetString();
70+
71+
if (int.TryParse(item, out var id))
72+
{
73+
var keyType = GetKeyType(typeToConvert);
74+
var method = GetBaseMethod(typeToConvert);
75+
76+
return (TypeSafeEnum<TValue, TKey>)method.Invoke(null, new[] { Convert.ChangeType(id, keyType) });
77+
}
78+
79+
throw new NotImplementedException();
80+
}
81+
6282
public bool CanRead => true;
6383
}
6484
}

0 commit comments

Comments
 (0)