Home > database >  How to define JSON attribute on model binding using ServiceStack
How to define JSON attribute on model binding using ServiceStack

Time:09-01

I am developing a custom module for a 3rd party application that is using ServiceStack for API calls.

The problem is that the JSON response is using snake case for keys and my Class using Dotnet standard PascalCase for defining properties.

I don't want to rename my class properties instead of renaming I want to use ServiceStack alternate option. I check there documentation but didn't find what I want.

like if use NewtonJSON serializer we define property like below:

[JsonProperty("order_number")]
public int OrderNumber { get; set; }

what is the alternative to the above in ServiceStack?

CodePudding user response:

You can use .NET Data Contract attributes as seen in this answer, e.g:

[DataContract]
public class MyModel
{
    [DataMember(Name="order_number")]
    public int OrderNumber { get; set; }
}

Alternatively you can avoid aliasing every property if you configure ServiceStack.Text to de/serialize snake_case, e.g:

JsConfig.Init(new ServiceStack.Text.Config {
    TextCase = TextCase.SnakeCase,
});
  • Related