微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何使用Json.Net将JsonProperty属性分配给DLL内的类的属性?

我在DLL中有一个类,它没有标记DataContract,JsonProperty等.现在我想将类的实例序列化为 JSON对象,并缩短C#属性名称.

例如,该课程是:

public class Foo
{
    public string SomeLengthyCSharpPropertyName { get; set; }
}

I wonder if I Could create a mapping between the C# names and the json names. I cannot directly add the DataContract,JsonProperty attributes like below. Is there any workaround?

[DataContract]
public class Foo
{
    [JsonProperty("s")]
    public string SomeLengthyCSharpPropertyName { get; set; }
}

我倾向于不创建另一个具有相同但JsonProperty装饰属性的类,并将属性复制到新类,然后序列化.

解决方法

您可以使用成员的覆盖属性字典创建自己的自定义 ContractResolver,然后覆盖 CreateProperty()并将覆盖应用于基类返回的 JsonProperty

public class JsonPropertyOverride
{
    public string PropertyName { get; set; }

    public bool? Ignored { get; set; }

    // Others as required from http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm
    // Changing all value type properties to nullables.
}

public class OverrideContractResolver : DefaultContractResolver
{
    readonly Dictionary<MemberInfo,JsonPropertyOverride> overrides; // A private copy for thread safety.

    public OverrideContractResolver(IDictionary<MemberInfo,JsonPropertyOverride> overrides)
        : base()
    {
        if (overrides == null)
            throw new ArgumentNullException();
        this.overrides = overrides.ToDictionary(p => p.Key,p => p.Value);
    }

    protected override JsonProperty CreateProperty(MemberInfo member,MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member,memberSerialization);
        if (property != null)
        {
            JsonPropertyOverride attr;
            if (overrides.TryGetValue(member,out attr))
            {
                if (attr.PropertyName != null)
                    property.PropertyName = ResolvePropertyName(attr.PropertyName);
                if (attr.Ignored != null)
                    property.Ignored = attr.Ignored.Value;
            }
        }
        return property;
    }
}

如果您愿意,也可以从CamelCasePropertyNamesContractResolver继承.

然后使用它像:

public class Foo
{
    public string SomeLengthyCSharpPropertyName { get; set; }

    public string DefaultNotIgnored { get; set; }

    [JsonIgnore]
    public string DefaultIgnored { get; set; }
}

public class TestClass
{
    public static void test()
    {
        var foo = new Foo { SomeLengthyCSharpPropertyName = "SomeLengthyCSharpPropertyName",DefaultIgnored = "DefaultIgnored",DefaultNotIgnored = "DefaultNotIgnored" };

        var resolver = new OverrideContractResolver(new Dictionary<MemberInfo,JsonPropertyOverride> { 
            { typeof(Foo).GetProperty("SomeLengthyCSharpPropertyName"),new JsonPropertyOverride { PropertyName = "c"  } },{ typeof(Foo).GetProperty("DefaultNotIgnored"),new JsonPropertyOverride { Ignored = true  } },{ typeof(Foo).GetProperty("DefaultIgnored"),new JsonPropertyOverride { Ignored = false  } },});
        var settings = new JsonSerializerSettings { ContractResolver = resolver };

        var json = JsonConvert.SerializeObject(foo,settings); // Outputs {"c":"SomeLengthyCSharpPropertyName","DefaultIgnored":"DefaultIgnored"}
        Debug.WriteLine(json);

        var expectedJson = @"{ ""c"": ""SomeLengthyCSharpPropertyName"",""DefaultIgnored"": ""DefaultIgnored"" }";
        var ok = JToken.deepequals(JToken.Parse(json),JToken.Parse(expectedJson));
        Debug.Assert(ok); // No assert

        var foo2 = JsonConvert.DeserializeObject<Foo>(json,settings);

        var ok2 = foo2.DefaultIgnored == foo.DefaultIgnored && foo2.someLengthyCSharpPropertyName == foo.someLengthyCSharpPropertyName;
        Debug.Assert(ok2); // No assert
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐