覆盖 ServiceStack 中的字段名称反序列化 [英] Override field name deserialization in ServiceStack

查看:57
本文介绍了覆盖 ServiceStack 中的字段名称反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ServiceStack 反序列化一些 HTML 表单值,但不知道如何覆盖每个字段应该从中读取的值.

I'm using ServiceStack to deserialize some HTML form values but can't figure out how to override the value that each field should be read from.

例如,表单将值发布到 first_name,但我的 POCO 上的属性称为 FirstName.我将如何在 ServiceStack 中进行这样的映射

For example, the form posts a value to first_name but the property on my POCO is called FirstName. how would I do mapping like that in ServiceStack

推荐答案

ServiceStack Text 序列化程序支持 [DataMember] 别名,您可以在其中使用 Name 参数指定每个字段的别名,例如:

The ServiceStack Text serializers support [DataMember] aliases where you can use the Name parameter to specify what alias each field should be, e.g:

[DataContract]
public class Customer
{
    [DataMember(Name="first_name")]
    public string FirstName { get; set; }

    [DataMember(Name="last_name")]
    public string LastName { get; set; }
}

注意:一旦您将 [DataContract]/[DataMember] 属性添加到您的 DTO 中,该行为就会变为选择加入,您将在您想要序列化的每个属性上添加 [DataMember].

Note: Once you add [DataContract] / [DataMember] attributes to your DTOs then the behavior becomes opt-in and you will have add [DataMember] on each of the properties you want serialized.

您可以通过指定以下全局设置来指示 JSON 序列化遵循不同的约定:

You can instruct JSON serialization to follow a different convention by specifying the following global settings:

//Emit {"firstName":"first","lastName":"last"}
JsConfig.Init(new Config { TextCase = TextCase.CamelCase });

//Emit {"first_name":"first","last_name":"last"}
JsConfig.Init(new Config { TextCase = TextCase.SnakeCase });

这篇关于覆盖 ServiceStack 中的字段名称反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆