Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 773
user1991179 Avatar asked Jun 29 '17 02:06

user1991179


1 Answers

This behavior is documented in the .NET Core Release Notes:

In addition to running flawlessly on .NET Core we’re also actively striving to find how we can best integrate with and leverage the surrounding .NET Core ecosystem and have made several changes to that end:

CamelCase

The JSON and JSV Text serializers are following .NET Core’s default convention to use camelCase properties by default. This can be reverted back to PascalCase with:

SetConfig(new HostConfig { UseCamelCase = false })

We also agree with this default, .NET Core seems to be centered around embracing the surrounding developer ecosystem where .NET’s default PascalCase protrudes in a sea of camelCase and snake_case JSON APIs. This won’t have an impact on .NET Service Clients or Text Serialization which supports case-insensitive properties, however Ajax and JS clients will need to be updated to use matching properties. You can use ss-utils normalize() methods to help with handling both conventions by recursively normalizing and converting all properties to lowercase.

Custom adhoc JSON Serialization

The above will use CamelCase for your ServiceStack Services, if you just need to to serialize an adhoc object to JSON you can wrap it in a configuration object to override the global settings, e.g:

using (JsConfig.With(new Config { TextCase = TextCase.PascalCase }))
{
    var json = results.ToJson();
}
like image 150
mythz Avatar answered Oct 01 '22 19:10

mythz