ServiceStack JSON serializes to lowercase on dotnet core?
I am using ServiceStack to run REST API and serialize response object. More specifically, when I call JsonSerializer.SerializeToString(.)
on the response object, all property names are serialized to lowercase. I've tried playing with type parameters JsConfig.EmitCamelCaseNames
but it doesn't help. Any ideas?
See ServiceStack version information and screenshots below.
"ServiceStack.Core": "1.0.32",
"ServiceStack.Kestrel": "1.0.32",
I believe this belongs to the dotnet core because it was originally a .NET application that I ported to the dotnet core. I have never seen this problem in my previous versions of the app. I know I can use a custom serializer, but I find ServiceStack faster (please let me know if I'm wrong).
source to share
This behavior is described in the .NET Core release notes :
In addition to running flawlessly in .NET Core, we've also been actively trying to find ways to best integrate with and make the most of the .NET Core ecosystem, and have made several changes to that end:
CamelCase
JSON and JSV Text serializers follow the standard .NET Cores convention for using camelCase properties by default. This can be brought back to PascalCase with:
SetConfig(new HostConfig { UseCamelCase = false })
We also agree with this default. .NET Core seems to be centered around reaching out to the surrounding developer ecosystem, where the default .NET PascalCase standard juts out into the sea of camelCase and snake_case JSON APIs. This will not affect .NET service clients or text serialization that supports case-insensitive properties, however Ajax and JS clients must be updated to use the appropriate properties. You can use the ss-utils normalize () methods to help handle 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 serialize the adhoc object to JSON, you can wrap it in a config object to override the global settings, like this:
using (JsConfig.With(new Config { TextCase = TextCase.PascalCase }))
{
var json = results.ToJson();
}
source to share
The solution I used was pointed out here under "Create custom scopes using String configuration". Below is some sample code that worked for me.
List<GetReturnObject> results = RestUtils.GetReturnObjects();
using (JsConfig.CreateScope("EmitCamelCaseNames:false"))
{
var s = JsonSerializer.SerializeToString(results);
}
source to share