Skip to content

Commit dccf6d2

Browse files
authored
System.Text.Json support (#1)
1 parent 2430f7b commit dccf6d2

17 files changed

+393
-0
lines changed

NamingStrategyConverter.sln

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NamingStrategyConverter.Ben
88
EndProject
99
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D6E05A82-460A-4263-8260-F5C9EEDBD97D}"
1010
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NamingStrategyConverter.SystemTextJson", "src\NamingStrategyConverter.SystemTextJson\NamingStrategyConverter.SystemTextJson.csproj", "{33591802-B36E-436B-80BC-7C0A89EA0721}"
12+
EndProject
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NamingStrategyConverter.SystemTextJson.Tests", "tests\NamingStrategyConverter.SystemTextJson.Tests\NamingStrategyConverter.SystemTextJson.Tests.csproj", "{360200C9-EB1F-43F9-B8F9-663B95E50ACF}"
14+
EndProject
1115
Global
1216
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1317
Debug|Any CPU = Debug|Any CPU
@@ -26,8 +30,17 @@ Global
2630
{6F2F8EEE-CD1F-4865-982D-BF5FB49148F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
2731
{6F2F8EEE-CD1F-4865-982D-BF5FB49148F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
2832
{6F2F8EEE-CD1F-4865-982D-BF5FB49148F2}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{33591802-B36E-436B-80BC-7C0A89EA0721}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{33591802-B36E-436B-80BC-7C0A89EA0721}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{33591802-B36E-436B-80BC-7C0A89EA0721}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{33591802-B36E-436B-80BC-7C0A89EA0721}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{360200C9-EB1F-43F9-B8F9-663B95E50ACF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{360200C9-EB1F-43F9-B8F9-663B95E50ACF}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{360200C9-EB1F-43F9-B8F9-663B95E50ACF}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{360200C9-EB1F-43F9-B8F9-663B95E50ACF}.Release|Any CPU.Build.0 = Release|Any CPU
2941
EndGlobalSection
3042
GlobalSection(NestedProjects) = preSolution
3143
{ABC04C2B-F31A-4441-A7BB-0CB8DBE4F95A} = {D6E05A82-460A-4263-8260-F5C9EEDBD97D}
44+
{360200C9-EB1F-43F9-B8F9-663B95E50ACF} = {D6E05A82-460A-4263-8260-F5C9EEDBD97D}
3245
EndGlobalSection
3346
EndGlobal
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text.Json;
2+
3+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
4+
5+
public class CamelCaseJsonNamingPolicy : JsonNamingPolicy
6+
{
7+
public override string ConvertName(string name) => name.ToCamelCase();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text.Json;
2+
3+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
4+
5+
public class KebabCaseJsonNamingPolicy : JsonNamingPolicy
6+
{
7+
public override string ConvertName(string name) => name.ToKebabCase();
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<LangVersion>default</LangVersion>
8+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
9+
<Title>NamingStrategyConverter.SystemTextJson</Title>
10+
<Authors>Dan Walsh</Authors>
11+
<RootNamespace>DanWalsh.NamingStrategyConverter.SystemTextJson</RootNamespace>
12+
<Description>System.Text.Json extension for NamingStrategyConverter</Description>
13+
<PackageProjectUrl>https://github.com/danjrwalsh/NamingStrategyConverter</PackageProjectUrl>
14+
<RepositoryUrl>https://github.com/danjrwalsh/NamingStrategyConverter</RepositoryUrl>
15+
<RepositoryType>git</RepositoryType>
16+
<PackageLicenseUrl>https://github.com/danjrwalsh/NamingStrategyConverter/blob/main/LICENSE</PackageLicenseUrl>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="System.Text.Json" Version="7.0.3" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\NamingStrategyConverter\NamingStrategyConverter.csproj" />
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text.Json;
2+
3+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
4+
5+
public class PascalCaseJsonNamingPolicy : JsonNamingPolicy
6+
{
7+
public override string ConvertName(string name) => name.ToPascalCase();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text.Json;
2+
3+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
4+
5+
public class SnakeCaseJsonNamingPolicy : JsonNamingPolicy
6+
{
7+
public override string ConvertName(string name) => name.ToSnakeCase();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text.Json;
2+
3+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
4+
5+
public class UpperKebabCaseJsonNamingPolicy : JsonNamingPolicy
6+
{
7+
public override string ConvertName(string name) => name.ToUpperKebabCase();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text.Json;
2+
3+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
4+
5+
public class UpperSnakeCaseJsonNamingPolicy : JsonNamingPolicy
6+
{
7+
public override string ConvertName(string name) => name.ToUpperSnakeCase();
8+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Text.Json;
2+
using Xunit;
3+
4+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
5+
6+
public sealed class CamelCaseJsonConversionTests
7+
{
8+
private readonly JsonSerializerOptions _options;
9+
10+
public CamelCaseJsonConversionTests()
11+
{
12+
_options = new JsonSerializerOptions { PropertyNamingPolicy = new CamelCaseJsonNamingPolicy() };
13+
}
14+
15+
[Fact]
16+
public void Serialize_ToCamelCasePropertyNames()
17+
{
18+
var testData = new TestData();
19+
20+
string json = JsonSerializer.Serialize(testData, _options);
21+
22+
Assert.Contains("testProperty", json);
23+
Assert.DoesNotContain("TestProperty", json);
24+
}
25+
26+
[Fact]
27+
public void Deserialize_FromCamelCasePropertyNames()
28+
{
29+
const string json = """{ "testProperty": "someValue" }""";
30+
31+
var result = JsonSerializer.Deserialize<TestData>(json, _options);
32+
33+
Assert.Equal("someValue", result!.TestProperty);
34+
}
35+
36+
[Fact]
37+
public void DeserializeFails_FromSnakeCasePropertyNames_ExpectedCamel()
38+
{
39+
const string json = """{ "test_property": "someValue" }""";
40+
41+
var result = JsonSerializer.Deserialize<TestData>(json, _options);
42+
43+
Assert.NotEqual("someValue", result!.TestProperty);
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Text.Json;
2+
using Xunit;
3+
4+
namespace DanWalsh.NamingStrategyConverter.SystemTextJson;
5+
6+
public sealed class KebabCaseJsonConversionTests
7+
{
8+
private readonly JsonSerializerOptions _options;
9+
10+
public KebabCaseJsonConversionTests()
11+
{
12+
_options = new JsonSerializerOptions { PropertyNamingPolicy = new KebabCaseJsonNamingPolicy() };
13+
}
14+
15+
[Fact]
16+
public void Serialize_ToKebabCasePropertyNames()
17+
{
18+
var testData = new TestData();
19+
20+
string json = JsonSerializer.Serialize(testData, _options);
21+
22+
Assert.Contains("test-property", json);
23+
Assert.DoesNotContain("testProperty", json);
24+
}
25+
26+
[Fact]
27+
public void Deserialize_FromKebabCasePropertyNames()
28+
{
29+
const string json = """{ "test-property": "someValue" }""";
30+
31+
var result = JsonSerializer.Deserialize<TestData>(json, _options);
32+
33+
Assert.Equal("someValue", result!.TestProperty);
34+
}
35+
36+
[Fact]
37+
public void DeserializeFails_FromSnakeCasePropertyNames_ExpectedKebab()
38+
{
39+
const string json = """{ "test_property": "someValue" }""";
40+
41+
var result = JsonSerializer.Deserialize<TestData>(json, _options);
42+
43+
Assert.NotEqual("someValue", result!.TestProperty);
44+
}
45+
}

0 commit comments

Comments
 (0)