JSON.NET,XmlSerializer和“指定的"财产 [英] JSON.NET, XmlSerializer and "Specified" property

查看:143
本文介绍了JSON.NET,XmlSerializer和“指定的"财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST服务,该服务以JSON和XML作为输入,并对具有反序列化内容的扩展服务进行SOAP调用.用于反序列化的类是从SOAP服务的wsdl自动生成的.在有XML请求的情况下,我使用XmlSerializer,并且我想将Newton JSON.NET JsonSerializer用于JSON.

I have a REST service which takes JSON and XML as input and does a SOAP call to an extenal service with the deserialized content. The classes which are used for deserialization are auto-generated from the wsdl of the SOAP service. I use the XmlSerializer in case of a XML request and I want to use the Newton JSON.NET JsonSerializer for JSON.

现在,我有一个问题,WSDL生成的类包含可选原子值(例如bool,int等)的"Specified"属性.这由XmlSerializer(后者将属性相应地设置为公开的XML)处理,而不是由Newton JSON.NET序列化器处理.我不想强迫调用者将XXXSpecified元素添加到JSON字符串中.

Now I have the problem, that the WSDL generated classes contain the "Specified" property for optional atomar values (such as bool, int etc.). This is handled by the XmlSerializer (who sets the property accordingly to the reveived XML) but not by the Newton JSON.NET Serializer. I don't want to force the caller to add the XXXSpecified elements to the JSON string.

如何使用JSON.NET序列化程序处理指定"属性?

How can I treat the "Specified" properties with the JSON.NET serializer?

推荐答案

我的解决方案:

class MyContractResolver : DefaultContractResolver
{
    private JsonObjectContract objectContract = null;

    public override JsonContract ResolveContract(Type type)
    {
        JsonContract contract = base.ResolveContract(type);
        objectContract = contract as JsonObjectContract;
        return contract;
    }

    public void RemoveProperty(string name)
    {
        if (objectContract != null)
        {
            objectContract.Properties.Remove(objectContract.Properties.First(
                 p => p.PropertyName == name));
        }
    }

    public void AtomarOptinalType(string name, bool specified)
    {
        if (objectContract != null)
        {
            if (!specified)
            {
                JsonProperty prop = objectContract.Properties.First(
                     p => p.PropertyName == name);
                objectContract.Properties.Remove(prop);
            }

            RemoveProperty(name + "Specified");
        }
    }
}

,然后扩展生成的类:

public partial class MyGeneratedClass
{
    [OnDeserializing]
    internal void OnDeserializingMethod(StreamingContext context)
    {
        this.PropertyChanged += 
            new System.ComponentModel.PropertyChangedEventHandler(
                 MyGeneratedClass_PropertyChanged);
    }

    [OnSerializing]
    internal void OnSerializingMethod(StreamingContext context)
    {
        MyContractResolver cr = context.Context as MyContractResolver;

        if (cr != null)
        {
            cr.AtomarOptinalType("MyAtomarOptionalProperty",
                 this.MyAtomarOptionalPropertySpecified);
        }
    }

    void MyGeneratedClass_PropertyChanged(object sender, 
          System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "MyAtomarOptionalProperty")
        {
            this.MyAtomarOptionalPropertySpecified = true;
        }
    }
}

这篇关于JSON.NET,XmlSerializer和“指定的"财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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