Search code examples
c#serializationservicestack.net-coreservicestack-text

ServiceStack JSON serializing to lower case on dotnet core?


I'm using ServiceStack to run a REST API and am running into issues serializing the response object. More specifically, when I call JsonSerializer.SerializeToString(.) on the response object all property names are serialized in lower case. I've tried fiddling with the params like JsConfig.EmitCamelCaseNames but that doesn't help. Any ideas?

See below for ServiceStack version info and screenshots.

"ServiceStack.Core": "1.0.32",
"ServiceStack.Kestrel": "1.0.32",

Response object to serialize: Object Definition

Serialized string: Serialized String

I believe this is specific to dotnet core because this was originally a .NET app that I've migrated to dotnet core. I've never seen this issue in my prior versions of the app. I know I could use the native serializer, but I think ServiceStack is faster (please let me know if I'm wrong).


Solution

  • The solution I used was listed here in the "Create Custom Scopes using String config" section. Below is a code sample that worked for me.

    List<GetReturnObject> results = RestUtils.GetReturnObjects();
    using (JsConfig.CreateScope("EmitCamelCaseNames:false"))
    {
        var s = JsonSerializer.SerializeToString(results);
    }