From 06ac09b2dad22acf152f3a8bd210be8b4b9e6bc5 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Fri, 24 Mar 2023 20:41:38 +0100 Subject: [PATCH] Introduced new "TextCasingNamingStrategy" class --- .../Serialization/TextCasingNamingStrategy.cs | 35 ++++++++++++ .../Json/Newtonsoft/NamingStrategyTests.cs | 57 +++++++++++++++++++ src/UnitTestProject1/UnitTestProject1.csproj | 1 + 3 files changed, 93 insertions(+) create mode 100644 src/Skybrud.Essentials/Json/Newtonsoft/Serialization/TextCasingNamingStrategy.cs create mode 100644 src/UnitTestProject1/Json/Newtonsoft/NamingStrategyTests.cs diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Serialization/TextCasingNamingStrategy.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Serialization/TextCasingNamingStrategy.cs new file mode 100644 index 0000000..7779204 --- /dev/null +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Serialization/TextCasingNamingStrategy.cs @@ -0,0 +1,35 @@ +using Newtonsoft.Json.Serialization; +using Skybrud.Essentials.Strings; + +namespace Skybrud.Essentials.Json.Newtonsoft.Serialization { + + /// + /// Custom implementation of for converting property names to . + /// + public class TextCasingNamingStrategy : NamingStrategy { + + /// + /// Gets the casing the be used. + /// + public TextCasing Casing { get; } + + /// + /// Initializes a new instance based on the specified . + /// + /// The casing the be used. + public TextCasingNamingStrategy(TextCasing casing) { + Casing = casing; + } + + /// + /// Resolves the specified property name. + /// + /// The property name to resolve. + /// The resolved property name. + protected override string ResolvePropertyName(string name) { + return StringUtils.ToCasing(name, Casing); + } + + } + +} \ No newline at end of file diff --git a/src/UnitTestProject1/Json/Newtonsoft/NamingStrategyTests.cs b/src/UnitTestProject1/Json/Newtonsoft/NamingStrategyTests.cs new file mode 100644 index 0000000..5a67d11 --- /dev/null +++ b/src/UnitTestProject1/Json/Newtonsoft/NamingStrategyTests.cs @@ -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; + } + + } + + } + +} \ No newline at end of file diff --git a/src/UnitTestProject1/UnitTestProject1.csproj b/src/UnitTestProject1/UnitTestProject1.csproj index 484d21d..919222a 100644 --- a/src/UnitTestProject1/UnitTestProject1.csproj +++ b/src/UnitTestProject1/UnitTestProject1.csproj @@ -92,6 +92,7 @@ +