Json Serializer that converts property names to snake_case in the response.
SnakeCase.JsonNet is now .net standard! It's available via NuGet:
Install-Package SnakeCase.JsonNet
SnakeCase.JsonNet.Portable is available via NuGet:
Install-Package SnakeCase.JsonNet.Portable
Note that the Portable version will be deprecated in future versions since the project now supports .net standard.
// We'll try and serialize this!
class TestObject
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
// Using the serializer directly
private string TestSerializer(TestObject obj)
{
var serializer = new SnakeCaseJsonSerializer();
using (var sw = new StringWriter())
{
using (var jw = new JsonTextWriter(sw))
{
serializer.Serialize(jw, obj);
}
return sw.ToString();
}
}
// Or you can just inject the SnakeCaseContractResolver
private string TestContractResolver(TestObject obj)
{
var result = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
{
ContractResolver = new SnakeCaseContractResolver()
});
return result;
}
These functions should serialize this object:
var obj = new TestObject
{
Title = "Mr",
FirstName = "John",
LastName = "Smith"
};
To this JSON object:
{
"title": "Mr",
"first_name": "John",
"last_name": "Smith"
}