2015-11-16 107 views
1

我正在开发Windows 10 UWP应用程序,似乎无法摆脱此错误: “System.Runtime.Serialization类型的异常。 SerializationException'发生在mscorlib.ni.dll中,但未在用户代码中处理“parse.com:SerializationException使用“__type”属性反序列化JSON对象

我正在使用Rest API从Parse和实例化对象上的Data Store检索值。 这里是我的课是什么样子

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输出是什么样子:

{ 
"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" 
    } 
] 
} 

错误消息的详细信息如下: 附加信息:元素“:像”包含数据':File'数据合同。反序列化器不知道映射到此合约的任何类型。将与'File'相对应的类型添加到已知类型列表中 - 例如,使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表中。

回答

1

你的问题是,你正在使用的DataContractJsonSerializer反序列化JSON的,并"__type"是串行器一保留财产。它用于识别派生类型的多态类型。从docs

Preserving Type Information

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)...

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

Data members called "__type" are forbidden due to potential conflict with the type hint.

因此,你不能手动这个属性添加到类,并将它转换正确。

你可以,但是,充分利用多态性的序列化的处理,以读取和写入"__type"自动,通过定义下,您的Image类型是预期的类型的子类图像信息的类层次结构。为清晰起见,我们将其重命名为FileImage

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"值和属性数据(例如嵌入式Base64编码图像)发送JSON数据,你现在可以轻松地修改您的数据模型,以增加额外的子类来ImageBase

+0

非常感谢,它似乎已经解决了错误,现在编译程序。将DataContext(特别是名称和URL)绑定到类时,如何访问类Result和类FileImage的成员? –

+0

@JasonBourne - “图片”属性仍然存在并且可公开访问。它将底层的'ImageBase'属性转换为'FileImage'并返回。所以你应该能够绑定到那个。 – dbc

+0

太棒了!感谢dbc。 –

相关问题