parse.com:SerializationException,以"__type"反序列化JSON对象财产 [英] parse.com: SerializationException deserializing JSON objects with "__type" property

查看:144
本文介绍了parse.com:SerializationException,以"__type"反序列化JSON对象财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Windows 10 UWP应用,似乎无法摆脱此错误: "mscorlib.ni.dll中发生了'System.Runtime.Serialization.SerializationException类型的异常,但未在用户代码中处理"

I'm developing a Windows 10 UWP app and can't seem to get rid of this error: "An exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.ni.dll but was not handled in user code"

我正在使用Rest API从Parse的数据存储中检索值并实例化对象. 这是我班级的样子

I'm using the Rest API to retrieve values from a Data Store on Parse and instantiate objects. Here's what my Class looks like

public class ImageTest
{
    public class Image
    {
        public string __type { get; set; }
        public string name { get; set; }
        public string url { get; set; }
    }

    public class Result
    {
        public string createdAt { get; set; }
        public Image image { get; set; }
        public string name { get; set; }
        public string objectId { get; set; }
        public string updatedAt { get; set; }
    }

    public class RootObject
    {
        public List<Result> results { get; set; }
    }
}

这是我的JSON输出:

Here's what my JSON output looks like:

{
"results": [
    {
        "createdAt": "2015-11-16T02:04:17.403Z",
        "image": {
            "__type": "File",
            "name": "stark.jpg",
            "url": "http://xyz.parse.com/stark.jpg"
        },
        "name": "Stark",
        "objectId": "2ypGrvkvg0",
        "updatedAt": "2015-11-16T02:04:23.121Z"
    },
    {
        "createdAt": "2015-11-16T02:04:31.409Z",
        "image": {
            "__type": "File",
            "name": "targaryen.jpg",
            "url": "http://xyz.parse.com/targaryen.jpg"
        },
        "name": "Targaryen",
        "objectId": "otgO3scX3k",
        "updatedAt": "2015-11-16T02:04:40.094Z"
    }
]
}

错误消息的详细信息如下: 附加信息:元素':image'包含':File'数据协定的数据.解串器不知道任何映射到该合同的类型.将与文件"相对应的类型添加到已知类型的列表中,例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型的列表中.

The details of the error message are as follows: Additional information: Element ':image' contains data of the ':File' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'File' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.

推荐答案

您的问题是您正在使用是此序列化程序的保留属性.它用于标识多态类型的派生类型.从文档:

Your problem is that you are using the DataContractJsonSerializer to deserialize your JSON, and "__type" is a reserved property for this serializer. It is used to identify derived types of polymorphic types. From the docs:

保留类型信息

要保留类型标识,当将复杂类型序列化为JSON时,可以添加类型提示",并且反序列化器会识别该提示并采取适当的措施. 类型提示"是密钥名称为"__type"(两个下划线,后跟单词"type")的JSON键/值对.该值是格式为"DataContractName:DataContractNamespace"的JSON字符串(名称中第一个冒号之前的任何内容)...

To preserve type identity, when serializing complex types to JSON a "type hint" can be added, and the deserializer recognizes the hint and acts appropriately. The "type hint" is a JSON key/value pair with the key name of "__type" (two underscores followed by the word "type"). The value is a JSON string of the form "DataContractName:DataContractNamespace" (anything up to the first colon is the name)...

类型提示与XML Schema Instance标准定义的xsi:type属性非常相似,并在对XML进行序列化/反序列化时使用.

The type hint is very similar to the xsi:type attribute defined by the XML Schema Instance standard and used when serializing/deserializing XML.

由于与类型提示的潜在冲突,因此禁止使用名为"__type"的数据成员.

因此,您不能手动将此属性添加到您的类中并使其正确翻译.

Thus you cannot manually add this property to your class and have it translate correctly.

但是,您可以通过定义图像信息的类层次结构来利用序列化程序对多态性的处理来自动读写"__type" ,其中Image类型是其中的子类.预期的类型.为了清楚起见,我们将其重命名为FileImage:

You can, however, leverage the serializer's handling of polymorphism to read and write the "__type" automatically, by defining a class hierarchy of image information in which your Image type is a subclass of the expected type. Let's rename it to FileImage for clarity:

public class ImageTest
{
    [DataContract(Namespace = "")]
    [KnownType(typeof(FileImage))]
    public abstract class ImageBase
    {
    }

    [DataContract(Name = "File", Namespace = "")]
    public sealed class FileImage : ImageBase
    {
        [DataMember(Name = "name")]
        public string name { get; set; }
        [DataMember(Name = "url")]
        public string url { get; set; }
    }

    [DataContract(Namespace = "")]
    public class Result
    {
        [DataMember]
        public string createdAt { get; set; }

        [IgnoreDataMember]
        public FileImage image { get { return imageBase as FileImage; } set { imageBase = value; } }

        [DataMember(Name = "image")] // Need not be public if DataMember is applied.
        ImageBase imageBase { get; set; }

        [DataMember]
        public string name { get; set; }

        [DataMember]
        public string objectId { get; set; }

        [DataMember]
        public string updatedAt { get; set; }
    }

    public class RootObject
    {
        public List<Result> results { get; set; }
    }
}

现在一切正常.

如果稍后发现服务器正在发送带有其他"__type"值和属性数据的JSON数据(例如,嵌入式Base64图像),您现在可以轻松地修改数据模型以向ImageBase添加其他子类.

If later you find that server is sending JSON data with additional "__type" values and property data (e.g. an embedded Base64 image) you can now easily modify your data model to add additional subclasses to ImageBase.

这篇关于parse.com:SerializationException,以"__type"反序列化JSON对象财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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