Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define JSON attribute on model binding using ServiceStack

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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,
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading