Skip to content

Commit

Permalink
Introduced new "TextCasingNamingStrategy" class
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Mar 24, 2023
1 parent 751bf70 commit 06ac09b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Newtonsoft.Json.Serialization;
using Skybrud.Essentials.Strings;

namespace Skybrud.Essentials.Json.Newtonsoft.Serialization {

/// <summary>
/// Custom implementation of <see cref="NamingStrategy"/> for converting property names to <see cref="TextCasing.CamelCase"/>.
/// </summary>
public class TextCasingNamingStrategy : NamingStrategy {

/// <summary>
/// Gets the casing the be used.
/// </summary>
public TextCasing Casing { get; }

/// <summary>
/// Initializes a new instance based on the specified <paramref name="casing"/>.
/// </summary>
/// <param name="casing">The casing the be used.</param>
public TextCasingNamingStrategy(TextCasing casing) {
Casing = casing;
}

/// <summary>
/// Resolves the specified property name.
/// </summary>
/// <param name="name">The property name to resolve.</param>
/// <returns>The resolved property name.</returns>
protected override string ResolvePropertyName(string name) {
return StringUtils.ToCasing(name, Casing);
}

}

}
57 changes: 57 additions & 0 deletions src/UnitTestProject1/Json/Newtonsoft/NamingStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Skybrud.Essentials.Json.Newtonsoft.Serialization;
using Skybrud.Essentials.Strings;

namespace UnitTestProject1.Json.Newtonsoft {

[TestClass]
public class NamingStrategyTests {

[TestMethod]
public void TestCasings() {

HelloValue value = new HelloValue("");

var samples = new[] {
new { Expected = "helloworld", Casing = TextCasing.LowerCase },
new { Expected = "HELLOWORLD", Casing = TextCasing.UpperCase },
new { Expected = "helloWorld", Casing = TextCasing.CamelCase },
new { Expected = "HelloWorld", Casing = TextCasing.PascalCase },
new { Expected = "hello-world", Casing = TextCasing.KebabCase },
new { Expected = "Hello-World", Casing = TextCasing.HeaderCase },
new { Expected = "HELLO-WORLD", Casing = TextCasing.TrainCase },
new { Expected = "hello_world", Casing = TextCasing.SnakeCase },
new { Expected = "HELLO_WORLD", Casing = TextCasing.ConstantCase }
};

foreach (var sample in samples) {

string expected = "{\"" + sample.Expected + "\":\"\"}";

JsonSerializerSettings settings = new JsonSerializerSettings {
ContractResolver = new DefaultContractResolver { NamingStrategy = new TextCasingNamingStrategy(sample.Casing) }
};

string result = JsonConvert.SerializeObject(value, settings);

Assert.AreEqual(expected, result, sample.Casing.ToString());

}

}

public class HelloValue {

public string HelloWorld { get; }

public HelloValue(string helloWorld) {
HelloWorld = helloWorld;
}

}

}

}
1 change: 1 addition & 0 deletions src/UnitTestProject1/UnitTestProject1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Json\Newtonsoft\JObjectTests\UInt64Tests.cs" />
<Compile Include="Json\Newtonsoft\JObjectTests\UInt32Tests.cs" />
<Compile Include="Json\Newtonsoft\JObjectTests\UInt16Tests.cs" />
<Compile Include="Json\Newtonsoft\NamingStrategyTests.cs" />
<Compile Include="Maps\DistanceTests.cs" />
<Compile Include="Locations\LocationTests.cs" />
<Compile Include="Maps\PointTests.cs" />
Expand Down

0 comments on commit 06ac09b

Please sign in to comment.