Skip to content
Merged
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

This file was deleted.

This file was deleted.

This file was deleted.

59 changes: 0 additions & 59 deletions json-csharp/CustomNamingPolicy/CustomNamingPolicy.NET8/Program.cs

This file was deleted.

12 changes: 0 additions & 12 deletions json-csharp/CustomNamingPolicy/CustomNamingPolicy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomNamingPolicy", "Custo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", "{F0A97070-D698-4309-BD2A-19C1EF410EAA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomNamingPolicy.NET8", "CustomNamingPolicy.NET8\CustomNamingPolicy.NET8.csproj", "{EA2B17EB-E1B1-4FF5-A64D-379DF523F1FE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomNamingPolicy.NET8.Test", "CustomNamingPolicy.NET8.Test\CustomNamingPolicy.NET8.Test.csproj", "{D9E5E15E-E8E7-470D-B634-D46E786F6889}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,14 +21,6 @@ Global
{F0A97070-D698-4309-BD2A-19C1EF410EAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0A97070-D698-4309-BD2A-19C1EF410EAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0A97070-D698-4309-BD2A-19C1EF410EAA}.Release|Any CPU.Build.0 = Release|Any CPU
{EA2B17EB-E1B1-4FF5-A64D-379DF523F1FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA2B17EB-E1B1-4FF5-A64D-379DF523F1FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA2B17EB-E1B1-4FF5-A64D-379DF523F1FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA2B17EB-E1B1-4FF5-A64D-379DF523F1FE}.Release|Any CPU.Build.0 = Release|Any CPU
{D9E5E15E-E8E7-470D-B634-D46E786F6889}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9E5E15E-E8E7-470D-B634-D46E786F6889}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9E5E15E-E8E7-470D-B634-D46E786F6889}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9E5E15E-E8E7-470D-B634-D46E786F6889}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class CamelCasePolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
return char.IsUpper(name[0]) ? char.ToLower(name[0]) + name.Substring(1) : name;
return char.IsUpper(name[0]) ? char.ToLower(name[0]) + name[1..] : name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ public class NodeSeparatorPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
ArgumentNullException.ThrowIfNull(name);

var sb = new StringBuilder();
sb.Append(char.ToLower(name[0]));
for (int i = 1; i < name.Length; i++)
Expand Down
107 changes: 75 additions & 32 deletions json-csharp/CustomNamingPolicy/CustomNamingPolicy/Program.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,80 @@
using System.Text.Json;
using CustomNamingPolicy;

namespace CustomNamingPolicy;
var person = new Person()
{
GivenName = "Name1",
surName = "Surname1"
};

// No custom policy used
var jsonString = JsonSerializer.Serialize(person);
Console.WriteLine(jsonString);

// camelCase Policy used
var camelCaseOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = new CamelCasePolicy()
};

jsonString = JsonSerializer.Serialize(person, camelCaseOptions);
Console.WriteLine(jsonString);

// node/separator Policy used
var nodeOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = new NodeSeparatorPolicy()
};

jsonString = JsonSerializer.Serialize(person, nodeOptions);
Console.WriteLine(jsonString);

// Built-in policies
ConvertToSnakeCaseLower();
ConvertToSnakeCaseUpper();
ConvertToKebabCaseLower();
ConvertToKebabCaseUpper();

static void ConvertToSnakeCaseLower()
{
var snakeCaseLowerPolicy = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};

var jsonObj = JsonSerializer.Serialize(new { PropertyName = "value" }, snakeCaseLowerPolicy);
Console.WriteLine(jsonObj);
}

static void ConvertToSnakeCaseUpper()
{
var snakeCaseUpperPolicy = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseUpper
};

var jsonObj = JsonSerializer.Serialize(new { PropertyName = "value" }, snakeCaseUpperPolicy);
Console.WriteLine(jsonObj);
}

public class CustomNamingPolicy
static void ConvertToKebabCaseLower()
{
public static void Main()
var kebabCaseLowerPolicy = new JsonSerializerOptions
{
var person = new Person()
{
GivenName = "Name1",
surName = "Surname1"
};

// No custom policy used
var jsonString = JsonSerializer.Serialize(person);
Console.WriteLine(jsonString);

// camelCase Policy used
var camelCaseOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = new CamelCasePolicy()
};

jsonString = JsonSerializer.Serialize(person, camelCaseOptions);
Console.WriteLine(jsonString);

// node/separator Policy used
var nodeOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = new NodeSeparatorPolicy()
};

jsonString = JsonSerializer.Serialize(person, nodeOptions);
Console.WriteLine(jsonString);
}
}
PropertyNamingPolicy = JsonNamingPolicy.KebabCaseLower
};

var jsonObj = JsonSerializer.Serialize(new { PropertyName = "value" }, kebabCaseLowerPolicy);
Console.WriteLine(jsonObj);
}

static void ConvertToKebabCaseUpper()
{
var kebabCaseUpperPolicy = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.KebabCaseUpper
};

var jsonObj = JsonSerializer.Serialize(new { PropertyName = "value" }, kebabCaseUpperPolicy);
Console.WriteLine(jsonObj);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System.Text.Json;

namespace CustomNamingPolicy.NET8.Test;
namespace Test;

public class Tests
public class BuiltInPolicyTests
{
[SetUp]
public void Setup()
{
}

[Test]
public void GivenObject_WhenSerializeWithSnakeCaseLowerPolicy_ReturnSnakeCaseLowerResult()
{
Expand All @@ -24,7 +19,7 @@ public void GivenObject_WhenSerializeWithSnakeCaseLowerPolicy_ReturnSnakeCaseLow
var expectedJsonObj = "{\"property_name\":\"value\"}";

//Assert
Assert.AreEqual(expectedJsonObj, jsonObj);
Assert.That(jsonObj, Is.EqualTo(expectedJsonObj));
}

[Test]
Expand All @@ -42,7 +37,7 @@ public void GivenObject_WhenSerializeWithSnakeCaseUpperPolicy_ReturnSnakeCaseUpp
var expectedJsonObj = "{\"PROPERTY_NAME\":\"value\"}";

//Assert
Assert.AreEqual(expectedJsonObj, jsonObj);
Assert.That(jsonObj, Is.EqualTo(expectedJsonObj));
}

[Test]
Expand All @@ -60,7 +55,7 @@ public void GivenObject_WhenSerializeWithKebabCaseLowerPolicy_ReturnKebabCaseLow
var expectedJsonObj = "{\"property-name\":\"value\"}";

//Assert
Assert.AreEqual(expectedJsonObj, jsonObj);
Assert.That(jsonObj, Is.EqualTo(expectedJsonObj));
}

[Test]
Expand All @@ -78,6 +73,6 @@ public void GivenObject_WhenSerializeWithKebabCaseUpperPolicy_ReturnKebabCaseUpp
var expectedJsonObj = "{\"PROPERTY-NAME\":\"value\"}";

//Assert
Assert.AreEqual(expectedJsonObj, jsonObj);
Assert.That(jsonObj, Is.EqualTo(expectedJsonObj));
}
}
}
8 changes: 4 additions & 4 deletions json-csharp/CustomNamingPolicy/Test/CustomPolicyUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void GivenPerson_WhenSerializeWithDefaultOptions_ReturnDefaultResult()
var expectedJson = "{\"GivenName\":\"Name1\",\"surName\":\"Surname1\"}";

//Assert
Assert.AreEqual(expectedJson, jsonString);
Assert.That(jsonString, Is.EqualTo(expectedJson));
}

[Test]
Expand All @@ -42,7 +42,7 @@ public void GivenPerson_WhenSerializeWithNodeSeparatorPolicy_ReturnNodeSeparator
var expectedJson = "{\"given/name\":\"Name1\",\"sur/name\":\"Surname1\"}";

//Assert
Assert.AreEqual(expectedJson, jsonString);
Assert.That(jsonString, Is.EqualTo(expectedJson));
}

[Test]
Expand All @@ -59,6 +59,6 @@ public void GivenPerson_WhenSerializeWithCamelCasePolicy_ReturnCamelCasePolicyRe
var expectedJson = "{\"givenName\":\"Name1\",\"surName\":\"Surname1\"}";

//Assert
Assert.AreEqual(expectedJson, jsonString);
Assert.That(jsonString, Is.EqualTo(expectedJson));
}
}
}
12 changes: 6 additions & 6 deletions json-csharp/CustomNamingPolicy/Test/Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="NUnit" Version="4.6.1" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.14.0" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading