/SnakeCase.JsonNet

Json Serializer that converts property names to snake_case in the response.

Primary LanguageC#MIT LicenseMIT

SnakeCase.JsonNet

Json Serializer that converts property names to snake_case in the response.

Join the chat at https://gitter.im/mrstebo/SnakeCase.JsonNet Build status

SnakeCase.JsonNet MyGet Prerelease NuGet Version

SnakeCase.JsonNet.Portable MyGet Prerelease NuGet Version

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.

Examples

// 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"
}