在C#中覆盖Json属性名称 [英] Overwrite Json property name in c#

查看:413
本文介绍了在C#中覆盖Json属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下字段的课程.这些属性用于在需要调用外部rest API方法时序列化为json对象.

I have a class with following fields. Those properties are used to serialize as json object when it needs to call a external rest API method.

public class Customer
    {
        [JsonProperty(PropertyName = "email")]
        public string Email { get; set; }

        [JsonProperty(PropertyName = "prop[listId]")]
        public string Test{ get; set; }

        // there are lot of properties 
    }

在属性名称Test中,外部API服务调用需要执行以下操作,例如遵循json归档名称格式.

In the property name Test ,external API service call required some thing like following json filed name format.

prop[7]

在我的情况下,可以根据诸如test,dev和prod之类的环境来更改7.因此,我正在寻找一种将listId值移动到app.config中的方法.

In my case this 7 can be changed according the environment like test,dev and prod.So what I'm looking for a way to move that listId value into app.config .

我已尝试按照以下步骤进行操作,但不允许这样做.对于listIdValue,如果为其分配常数值,它将起作用.

I have tried to do it as follow but it is not allowed to do that.for the listIdValue if assign the constant value it will work.

     private string listIdValue = ConfigurationManager.AppSettings["ListIdValue"];

     [JsonProperty(PropertyName = "prop["+listIdValue +"]")]
     public string Test{ get; set; }

推荐答案

您将必须覆盖DefaultContractResolver并实现您自己的机制来提供PropertyName(以JSON).我将提供完整的示例代码,以显示运行时生成的PropertyName的反序列化和序列化.当前,它会将Test字段修改为Test5(在所有型号中).您应该实现自己的机制(使用属性,保留名称,表等).

You'll have to override DefaultContractResolver and implement your own mechanism to provide the PropertyName (in JSON). I will provide a full example code to show deserialization and serialization with a runtime generated PropertyName. Currently, it modifies the Test field to Test5 (in all models). You should implement your own mechanism (using an attribute, a reserved name, a table or whatever.

class Program
{
    static void Main(string[] args)
    {
        var customer = new Customer() {Email = "asd@asd.com", Test = "asdasd"};
        var a = Serialize(customer, false);
        var b = Serialize(customer, true);
        Console.WriteLine(a);
        Console.WriteLine(b);

        var desA = Deserialize<Customer>(a, false);
        var desB = Deserialize<Customer>(b, true);

        Console.WriteLine("TestA: {0}", desA.Test);
        Console.WriteLine("TestB: {0}", desB.Test);

    }

    static string Serialize(object obj, bool newNames)
    {
        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Formatting = Formatting.Indented;
        if (newNames)
        {
            settings.ContractResolver = new CustomNamesContractResolver();
        }

        return JsonConvert.SerializeObject(obj, settings);
    }
    static T Deserialize<T>(string text, bool newNames)
    {
        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Formatting = Formatting.Indented;
        if (newNames)
        {
            settings.ContractResolver = new CustomNamesContractResolver();
        }

        return JsonConvert.DeserializeObject<T>(text, settings);
    }
}
class CustomNamesContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)
    {
        // Let the base class create all the JsonProperties 
        // using the short names
        IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);

        // Now inspect each property and replace the 
        // short name with the real property name
        foreach (JsonProperty prop in list)
        {
            if (prop.UnderlyingName == "Test") //change this to your implementation!
                prop.PropertyName = "Test" + 5;

        }

        return list;
    }
}

public class Customer
{
    [JsonProperty(PropertyName = "email")]
    public string Email { get; set; }

    public string Test { get; set; }

}

输出:

{
  "email": "asd@asd.com",
  "Test": "asdasd"
}
{
  "email": "asd@asd.com",
  "Test5": "asdasd"
}
TestA: asdasd
TestB: asdasd

如您所见,

如您所见,当我们使用Serialize(..., false)时,该字段的名称为Test;当我们使用Serialize(..., true)时,该字段的名称为Test5.这也适用于反序列化.

As you see, when we use Serialize(..., false) - the field's name is Test and when we use Serialize(..., true) - the field's name is Test5, as expected. This also works for deserialization.

我已将此答案用作答案: https://stackoverflow.com/a/20639697/773879

I have used this answer as insperation for my answer: https://stackoverflow.com/a/20639697/773879

这篇关于在C#中覆盖Json属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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