-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced new "TextCasingNamingStrategy" class
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/Skybrud.Essentials/Json/Newtonsoft/Serialization/TextCasingNamingStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
src/UnitTestProject1/Json/Newtonsoft/NamingStrategyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters