ServiceStack JSON serializing to lower case on dotnet core?
Asked Answered
O

3

5

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

Odonnell answered 29/6, 2017 at 2:16 Comment(0)
O
0

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);
}
Odonnell answered 30/6, 2017 at 0:38 Comment(1)
FYI see my updated answer for a more succinct Typed version.Living
L
14

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();
}
Living answered 29/6, 2017 at 2:20 Comment(6)
Thanks for this. I couldn't get the solution you posted to work, but I found a link in your documentation that took me to the solution that worked for me. I'll post the solution below for persistence.Odonnell
@Odonnell Are you referring to JSON emitted in ServiceStack Services? Or are you using ServiceStack's JSON Serializer outside of ServiceStack?Living
I'm using it in Services and this (both your solution and the other one below) work well. Should there be a different solution for each? Curious to hear your thoughts because I would've expected the serializer to be a standalone library and always be used as one.Odonnell
@Odonnell I'm still not clear if you're using the JsonSerializer in the same project as a configured ServiceStack AppHost? Where are you executing the JsonSerializer that's Serializing JSON? E.g in your Service, AppHost, outside of ServiceStack?Living
I am calling it within Services, then setting up AppHost as follows: public AppHost() : base("project_name", typeof(Services).GetAssembly()) { } Does it matter though?Odonnell
@Odonnell I just want to know if you're using it in the same project where ServiceStack is configured with SetConfig(new HostConfig { UseCamelCase = false }) in your AppHost.Configure()? If so it shouldn't generate camelCase names when using the stand-alone JsonSerialization. If you can put together a small stand-alone example of the issue (e.g. on GitHub) I can take a look.Living
L
1

This is obsolete:

JsConfig.With(emitCamelCaseNames: false)

Instead, use:

JsConfig.With(new ServiceStack.Text.Config { TextCase = TextCase.PascalCase })
Louls answered 17/10, 2019 at 7:48 Comment(0)
O
0

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);
}
Odonnell answered 30/6, 2017 at 0:38 Comment(1)
FYI see my updated answer for a more succinct Typed version.Living

© 2022 - 2024 — McMap. All rights reserved.