本文介绍了使用DataContractJsonSerializer,JSON字符串的反序列化到C#对象有列表和放大器;接口性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它调用第三方REST服务交流#.dotNet项目的工作。

I am working on a c#.dotNet project which invokes a 3rd party REST service.

样例类结构:

[Serializable]
[DataContract(Name = "MainClass")]
[KnownType(typeof(Class1))]
[KnownType(typeof(Class2))]
public class MainClass : IMainInterface
{

    public MainClass()
    {
        Value2 = new List<IClass2>();
    }

    [DataMember(Name = "class")]
    public IClass1 Value1 { get; set; }

    [DataMember(Name = "classes")]
    public List<IClass2> Value2 { get; set; }

}

[Serializable]
[Export(typeof(IClass1))]
[ExportMetadata("IClass1", "Class1")]
[DataContract(Name = "class1")]
public class Class1 : IClass1
{
    [ImportingConstructor]
    public Class1()
    {

    }

    [DataMember(Name = "prop1")]
    public string Prop1 { get; set; }

    [DataMember(Name = "prop2")]
    public string Prop2 { get; set; }

    [DataMember(Name = "prop3")]
    public string Prop3 { get; set; }

}

[Serializable]
[Export(typeof(IClass2))]
[ExportMetadata("IClass2", "Class2")]
[DataContract]
public class Class2 : IClass2
{
    [ImportingConstructor]
    public Class2()
    { }


    [DataMember(Name = "propA")]
    public string PropA { get; set; }

    [DataMember(Name = "propB")]
    public string PropB { get; set; }

    [DataMember(Name = "propC")]
    public string PropC { get; set; }
}

public interface IMainInterface
{
    IClass1 Value1 { get; set; }

    List<IClass2> Value2 { get; set; }
}

public interface IClass1
{
    string Prop1 { get; set; }

    string Prop2 { get; set; }

    string Prop3 { get; set; }
}

public interface IClass2
{
    string PropA { get; set; }

    string PropB { get; set; }

    string PropC { get; set; }
}



Json_String1 :(类型提示)

{
"class":
    {"__type":"class1:#WpfApplication1","prop1":"TestVal0","prop2":"TestVal2","prop3":"TestVal3"},
"classes":
    [
        {"__type":"Class2:#WpfApplication1","propA":"A","propB":"B","propC":"C"},
        {"__type":"Class2:#WpfApplication1","propA":"X","propB":"Y","propC":"Z"},
        {"__type":"Class2:#WpfApplication1","propA":"1","propB":"2","propC":"3"}
    ]
}

Json_String2 :(无类型提示)

{
"class":
    {"prop1":"TestVal0","prop2":"TestVal2","prop3":"TestVal3"},
"classes":
    [
        {"propA":"A","propB":"B","propC":"C"},
        {"propA":"X","propB":"Y","propC":"Z"},
        {"propA":"1","propB":"2","propC":"3"}
    ]
}

因此,对于给定的类结构,如果我生成JSON使用 MainClass 的对象) > DataContractJsonSerializer ,我得到的 Json_String1 如果我直接反序列化,它工作正常。

So, for given class structure if I generate json (of object of MainClass) using DataContractJsonSerializer, I am getting Json_String1 and if i directly deserialize, it works fine.

而为,而查询婷数据,响应的 Json_String2 (W / O型提示)。因此,反序列化,我得到下面的错误。

Whereas as while GETting data, response is Json_String2 ( w/o type hint). Hence, while deserializing I get following error.

InvalidCastException的是未处理。无法转换类型
'System.Object的对象键入'WpfApplication1.IClass2。

现在,我手动必须通过添加类型提示修改上述JSON(字符串操作),成功地反序列化。

Now, I manually have to modify above json (string manipulation) by adding type hint, to deserialize it successfully.

问题1)我怎样才能避免反序列化此JSON字符串操作?

问题2)我怎么能不类型的提示创建JSON?

修改
1.增加 IMainInterface 这是由 MainClass 实施。
2的dotNet框架4

edit :1. Added IMainInterface which is implemented by MainClass.2. dotNet Framework 4

推荐答案

由于没有你的类实际上是多态的,有可用的一对夫妇的解决方案使用的.Net内置的类库:

Since none of your classes are actually polymorphic, there are a couple solutions available that use .Net built-in class libraries:

解决方案1:的JavaScriptSerializer 解决方案

Solution 1: JavaScriptSerializer Solution

的可以很容易地通过使用的。然而,的它不允许重新映射字段和属性名的,因此你的属性名的必须在JSON的名称相匹配您希望处理的。以下转炉的伎俩:

JavaScriptSerializer makes it easy to remap interfaces to classes during deserialization by using a JavaScriptConverter. However, it does not allow field and property names to be remapped, thus your property names must match the names in the JSON you wish to process. The following converter does the trick:

public class InterfaceToClassConverter<TInterface, TClass> : JavaScriptConverter where TClass : class, TInterface
{
    public InterfaceToClassConverter()
    {
        if (typeof(TInterface) == typeof(TClass))
            throw new ArgumentException(string.Format("{0} and {1} must not be the same type", typeof(TInterface).FullName, typeof(TClass).FullName)); // Or else you would get infinite recursion!
        if (!typeof(TInterface).IsInterface)
            throw new ArgumentException(string.Format("{0} must be an interface", typeof(TInterface).FullName));
        if (typeof(TClass).IsInterface)
            throw new ArgumentException(string.Format("{0} must be a class not an interface", typeof(TClass).FullName));
    }

    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (type == typeof(TInterface))
            return serializer.ConvertToType<TClass>(dictionary);
        return null;
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override IEnumerable<Type> SupportedTypes
    {
        get
        {
            // For an interface-valued property such as "IFoo  Foo { getl set; },
            // When serializing, JavaScriptSerializer knows the actual concrete type being serialized -- which is never an interface.
            // When deserializing, JavaScriptSerializer only knows the expected type, which is an interface.  Thus by returning
            // only typeof(TInterface), we ensure this converter will only be called during deserialization, not serialization.
            return new[] { typeof(TInterface) };
        }
    }
}

和使用它像:

public interface IMainInterface
{
    IClass1 @class { get; set; } // NOTICE ALL PROPERTIES WERE RENAMED TO MATCH THE JSON NAMES.
    List<IClass2> classes { get; set; }
}

[Serializable]
[DataContract(Name = "MainClass")]
[KnownType(typeof(Class1))]
[KnownType(typeof(Class2))]
public class MainClass : IMainInterface
{
    public MainClass()
    {
        classes = new List<IClass2>();
    }

    [DataMember(Name = "class")]
    public IClass1 @class { get; set; }

    [DataMember(Name = "classes")]
    public List<IClass2> classes { get; set; }
}

[Serializable]
[DataContract(Name = "class1")]
public class Class1 : IClass1
{
    public Class1() {}

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

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

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

}

[Serializable]
[DataContract]
public class Class2 : IClass2
{
    public Class2() { }

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

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

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

public interface IClass1
{
    string prop1 { get; set; }
    string prop2 { get; set; }
    string prop3 { get; set; }
}

public interface IClass2
{
    string propA { get; set; }
    string propB { get; set; }
    string propC { get; set; }
}

public static class TestJavaScriptConverter
{
    public static void Test()
    {
        string json = @"
            {
            ""class"":
                {""prop1"":""TestVal0"",""prop2"":""TestVal2"",""prop3"":""TestVal3""},
            ""classes"":
                [
                    {""propA"":""A"",""propB"":""B"",""propC"":""C""},
                    {""propA"":""X"",""propB"":""Y"",""propC"":""Z""},
                    {""propA"":""1"",""propB"":""2"",""propC"":""3""}
                ]
            }";

        var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new JavaScriptConverter[] { new InterfaceToClassConverter<IClass1, Class1>(), new InterfaceToClassConverter<IClass2, Class2>() });
        var main1 = serializer.Deserialize<MainClass>(json);
        var json2 = serializer.Serialize(main1);
        Debug.WriteLine(json2);
        var main2 = serializer.Deserialize<MainClass>(json2);

        Debug.Assert(main1.@class.ToStringWithReflection() == main2.@class.ToStringWithReflection()); // No assert
        Debug.Assert(main1.classes.Select(c => c.ToStringWithReflection()).SequenceEqual(main2.classes.Select(c => c.ToStringWithReflection()))); // no assert
    }
}



解决方案2: DataContractJsonSerializer 解决方案

Solution 2: DataContractJsonSerializer Solution

WCF和的DataContractSerializer 作品的只有的混凝土类型和不序列化接口。因此,如果你想使用这些序列化,你必须在内部使用具体的类,并将其呈现给外界的接口,比如:

WCF and its DataContractSerializers work only with concrete types and do not serialize interfaces. Thus, if you wish to use these serializers you must use concrete classes internally and present them to the outside world as interfaces, for instance:

public interface IMainInterface
{
    IClass1 Value1 { get; set; }
    IList<IClass2> Value2 { get; set; }
}

[Serializable]
[DataContract(Name = "MainClass")]
public class MainClass : IMainInterface
{
    [DataMember(Name = "class")]
    Class1 RealValue1 { get; set; }

    [DataMember(Name = "classes")]
    private List<Class2> RealList2 { get; set; }

    IList<IClass2> list2Proxy; // can't be readonly because the DataContactJsonSerializer does not call the default constructor.

    private IList<IClass2> List2Proxy
    {
        get
        {
            if (list2Proxy == null)
                Interlocked.CompareExchange(ref list2Proxy, new ConvertingList<Class2, IClass2>(() => this.RealList2, c => c, ToClass), null);
            return list2Proxy;
        }
    }

    Class2 ToClass(IClass2 iClass)
    {
        // REWRITE TO FIT YOUR NEEDS
        return (Class2)iClass;
    }

    Class1 ToClass(IClass1 iClass)
    {
        // REWRITE TO FIT YOUR NEEDS
        return (Class1)iClass;
    }

    public MainClass()
    {
        RealList2 = new List<Class2>();
    }

    [IgnoreDataMember]
    public IClass1 Value1
    {
        get
        {
            return RealValue1;
        }
        set
        {
            RealValue1 = ToClass(value);
        }
    }

    [IgnoreDataMember]
    public IList<IClass2> Value2
    {
        get
        {
            return List2Proxy;
        }
        set
        {
            if (value == null)
            {
                RealList2.Clear();
                return;
            }
            if (List2Proxy == value)
                return;
            RealList2 = value.Select<IClass2, Class2>(ToClass).ToList();
        }
    }
}

public class ConvertingList<TIn, TOut> : IList<TOut>
{
    readonly Func<IList<TIn>> getList;
    readonly Func<TIn, TOut> toOuter;
    readonly Func<TOut, TIn> toInner;

    public ConvertingList(Func<IList<TIn>> getList, Func<TIn, TOut> toOuter, Func<TOut, TIn> toInner)
    {
        if (getList == null || toOuter == null || toInner == null)
            throw new ArgumentNullException();
        this.getList = getList;
        this.toOuter = toOuter;
        this.toInner = toInner;
    }

    IList<TIn> List { get { return getList(); } }

    TIn ToInner(TOut outer) { return toInner(outer); }

    TOut ToOuter(TIn inner) { return toOuter(inner); }

    #region IList<TOut> Members

    public int IndexOf(TOut item)
    {
        return List.IndexOf(toInner(item));
    }

    public void Insert(int index, TOut item)
    {
        List.Insert(index, ToInner(item));
    }

    public void RemoveAt(int index)
    {
        List.RemoveAt(index);
    }

    public TOut this[int index]
    {
        get
        {
            return ToOuter(List[index]);
        }
        set
        {
            List[index] = ToInner(value);
        }
    }

    #endregion

    #region ICollection<TOut> Members

    public void Add(TOut item)
    {
        List.Add(ToInner(item));
    }

    public void Clear()
    {
        List.Clear();
    }

    public bool Contains(TOut item)
    {
        return List.Contains(ToInner(item));
    }

    public void CopyTo(TOut[] array, int arrayIndex)
    {
        foreach (var item in this)
            array[arrayIndex++] = item;
    }

    public int Count
    {
        get { return List.Count; }
    }

    public bool IsReadOnly
    {
        get { return List.IsReadOnly; }
    }

    public bool Remove(TOut item)
    {
        return List.Remove(ToInner(item));
    }

    #endregion

    #region IEnumerable<TOut> Members

    public IEnumerator<TOut> GetEnumerator()
    {
        foreach (var item in List)
            yield return ToOuter(item);
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #endregion
}

请注意如果主叫方试图设置一个 IClass1 IClass2 这是不是一个真正的 1级类2 ,一个 InvalidCastException的将被抛出。因此,这给隐藏接口实现无真正保持实现私有的外观。

Note that if a caller attempts to set an IClass1 or IClass2 which is not an actual Class1 or Class2, an InvalidCastException will be thrown. Thus this gives the appearance of hiding interface implementations without truly keeping the implementations private.

这篇关于使用DataContractJsonSerializer,JSON字符串的反序列化到C#对象有列表和放大器;接口性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:01