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

user1991179
  • 573
  • 3
  • 8
  • 25

3 Answers3

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();
}
Community
  • 1
  • 1
mythz
  • 141,670
  • 29
  • 246
  • 390
  • 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. – user1991179 Jun 30 '17 at 00:34
  • @user1991179 Are you referring to JSON emitted in ServiceStack Services? Or are you using ServiceStack's JSON Serializer outside of ServiceStack? – mythz Jun 30 '17 at 01:48
  • 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. – user1991179 Jun 30 '17 at 02:05
  • @user1991179 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? – mythz Jun 30 '17 at 02:13
  • I am calling it within Services, then setting up AppHost as follows: public AppHost() : base("project_name", typeof(Services).GetAssembly()) { } Does it matter though? – user1991179 Jun 30 '17 at 02:16
  • @user1991179 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. – mythz Jun 30 '17 at 02:25
1

This is obsolete:

JsConfig.With(emitCamelCaseNames: false)

Instead, use:

JsConfig.With(new ServiceStack.Text.Config { TextCase = TextCase.PascalCase })
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Bashar Abu Shamaa
  • 1,998
  • 2
  • 21
  • 36
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);
}
user1991179
  • 573
  • 3
  • 8
  • 25