Skip to content

Commit b368f4d

Browse files
CamelCase serialization: retarget net10.0, cache the options instance, add the web preset
- Both projects net7.0 -> net10.0. - Test packages to current stable: Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1. - JsonSerializerExtensions now holds one static readonly JsonSerializerOptions instead of building a fresh instance per call, and DeserializeFromCamelCase<T> returns T? (Deserialize<T> can return null; the old signature raised CS8603). - Program.cs demonstrates new JsonSerializerOptions(JsonSerializerDefaults.Web), the preset ASP.NET Core applies, with a covering test. - ExtensionTests: xUnit assertion arguments were reversed (Assert.Equal(actual, expected)); swapped to (expected, actual) and used Assert.True for the boolean.
1 parent a59a6f6 commit b368f4d

5 files changed

Lines changed: 45 additions & 26 deletions

File tree

‎json-csharp/CamelCaseSerialization/CamelCaseSerialization/CamelCaseSerialization.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

‎json-csharp/CamelCaseSerialization/CamelCaseSerialization/JsonSerializerExtensions.cs‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ namespace CamelCaseSerialization;
44

55
public static class JsonSerializerExtensions
66
{
7-
public static string SerializeWithCamelCase<T>(this T data)
7+
private static readonly JsonSerializerOptions CamelCaseOptions = new()
88
{
9-
return JsonSerializer.Serialize(data, new JsonSerializerOptions
10-
{
11-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
12-
});
13-
}
9+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
10+
};
1411

15-
public static T DeserializeFromCamelCase<T>(this string json)
16-
{
17-
return JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions
18-
{
19-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
20-
});
21-
}
12+
public static string SerializeWithCamelCase<T>(this T data) =>
13+
JsonSerializer.Serialize(data, CamelCaseOptions);
14+
15+
public static T? DeserializeFromCamelCase<T>(this string json) =>
16+
JsonSerializer.Deserialize<T>(json, CamelCaseOptions);
2217
}

‎json-csharp/CamelCaseSerialization/CamelCaseSerialization/Program.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@
3232
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
3333
})}");
3434

35+
// With the JsonSerializerDefaults.Web preset, the same options ASP.NET Core applies
36+
var webOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
37+
Console.WriteLine($"{JsonSerializer.Serialize(person, webOptions)}");
38+
3539
// With extension method
3640
Console.WriteLine($"{person.SerializeWithCamelCase()}");
3741

3842
var personObject = personString.DeserializeFromCamelCase<Person>();
39-
Console.WriteLine($"{personObject.FirstName} {personObject.Surname}({personObject.IsActive}) is {personObject.Age}");
43+
Console.WriteLine($"{personObject?.FirstName} {personObject?.Surname}({personObject?.IsActive}) is {personObject?.Age}");

‎json-csharp/CamelCaseSerialization/Tests/CamelCaseSerialization.Tests/CamelCaseSerialization.Tests.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
13-
<PackageReference Include="xunit" Version="2.4.2" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
13+
<PackageReference Include="xunit" Version="2.9.3" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
18-
<PackageReference Include="coverlet.collector" Version="3.1.2">
18+
<PackageReference Include="coverlet.collector" Version="10.0.1">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>

‎json-csharp/CamelCaseSerialization/Tests/CamelCaseSerialization.Tests/ExtensionTests.cs‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json;
2+
13
namespace CamelCaseSerialization.Tests;
24

35
public class ExtensionTests
@@ -15,7 +17,7 @@ public void GivenPerson_WhenUsingExtensionMethod_ThenSerializeWithCamelCase()
1517

1618
var result = person.SerializeWithCamelCase();
1719

18-
Assert.Equal(result, """{"firstName":"John","surname":"Doe","age":20,"isActive":true}""");
20+
Assert.Equal("""{"firstName":"John","surname":"Doe","age":20,"isActive":true}""", result);
1921
}
2022

2123
[Fact]
@@ -24,10 +26,28 @@ public void GivenJsonStringInCamelCase_WhenUsingExtensionMethod_ThenDeserializeI
2426
var personJson = """{"firstName":"John","surname":"Doe","age":20,"isActive":true}""";
2527

2628
var result = personJson.DeserializeFromCamelCase<Person>();
27-
28-
Assert.Equal(result.Age, 20);
29-
Assert.Equal(result.FirstName, "John");
30-
Assert.Equal(result.Surname, "Doe");
31-
Assert.Equal(result.IsActive, true);
29+
30+
Assert.NotNull(result);
31+
Assert.Equal(20, result.Age);
32+
Assert.Equal("John", result.FirstName);
33+
Assert.Equal("Doe", result.Surname);
34+
Assert.True(result.IsActive);
35+
}
36+
37+
[Fact]
38+
public void GivenPerson_WhenSerializingWithWebDefaults_ThenPropertyNamesAreCamelCase()
39+
{
40+
var person = new Person
41+
{
42+
Age = 20,
43+
FirstName = "John",
44+
Surname = "Doe",
45+
IsActive = true
46+
};
47+
var webOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
48+
49+
var result = JsonSerializer.Serialize(person, webOptions);
50+
51+
Assert.Equal("""{"firstName":"John","surname":"Doe","age":20,"isActive":true}""", result);
3252
}
3353
}

0 commit comments

Comments
 (0)