ServiceStack JSON 在 dotnet 核心上序列化为小写?
ServiceStack JSON serializing to lower case on dotnet core?
我正在使用 ServiceStack 运行 REST API 并且 运行 正在处理序列化响应对象的问题。更具体地说,当我在响应对象上调用 JsonSerializer.SerializeToString(.)
时,所有 属性 名称都以小写形式序列化。我试过摆弄 JsConfig.EmitCamelCaseNames
之类的参数,但这无济于事。有任何想法吗?
请参阅下面的 ServiceStack 版本信息和屏幕截图。
"ServiceStack.Core": "1.0.32",
"ServiceStack.Kestrel": "1.0.32",
要序列化的响应对象:
序列化字符串:
我认为这是 dotnet core 特有的,因为这最初是我已迁移到 dotnet core 的 .NET 应用程序。我在以前的应用程序版本中从未见过这个问题。我知道我可以使用本机序列化程序,但我认为 ServiceStack 更快(如果我错了请告诉我)。
这种行为是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.
自定义临时JSON序列化
以上将为您的 ServiceStack 服务使用 CamelCase,如果您只需要将一个临时对象序列化为 JSON 您可以将其包装在配置对象中以覆盖全局设置,例如:
using (JsConfig.With(new Config { TextCase = TextCase.PascalCase }))
{
var json = results.ToJson();
}
我使用的解决方案列在 here 部分的 "Create Custom Scopes using String config" 中。以下是对我有用的代码示例。
List<GetReturnObject> results = RestUtils.GetReturnObjects();
using (JsConfig.CreateScope("EmitCamelCaseNames:false"))
{
var s = JsonSerializer.SerializeToString(results);
}
这是过时的:
JsConfig.With(emitCamelCaseNames: false)
改为使用:
JsConfig.With(new ServiceStack.Text.Config { TextCase = TextCase.PascalCase })
我正在使用 ServiceStack 运行 REST API 并且 运行 正在处理序列化响应对象的问题。更具体地说,当我在响应对象上调用 JsonSerializer.SerializeToString(.)
时,所有 属性 名称都以小写形式序列化。我试过摆弄 JsConfig.EmitCamelCaseNames
之类的参数,但这无济于事。有任何想法吗?
请参阅下面的 ServiceStack 版本信息和屏幕截图。
"ServiceStack.Core": "1.0.32",
"ServiceStack.Kestrel": "1.0.32",
要序列化的响应对象:
序列化字符串:
我认为这是 dotnet core 特有的,因为这最初是我已迁移到 dotnet core 的 .NET 应用程序。我在以前的应用程序版本中从未见过这个问题。我知道我可以使用本机序列化程序,但我认为 ServiceStack 更快(如果我错了请告诉我)。
这种行为是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.
自定义临时JSON序列化
以上将为您的 ServiceStack 服务使用 CamelCase,如果您只需要将一个临时对象序列化为 JSON 您可以将其包装在配置对象中以覆盖全局设置,例如:
using (JsConfig.With(new Config { TextCase = TextCase.PascalCase }))
{
var json = results.ToJson();
}
我使用的解决方案列在 here 部分的 "Create Custom Scopes using String config" 中。以下是对我有用的代码示例。
List<GetReturnObject> results = RestUtils.GetReturnObjects();
using (JsConfig.CreateScope("EmitCamelCaseNames:false"))
{
var s = JsonSerializer.SerializeToString(results);
}
这是过时的:
JsConfig.With(emitCamelCaseNames: false)
改为使用:
JsConfig.With(new ServiceStack.Text.Config { TextCase = TextCase.PascalCase })