2015-11-16 63 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。 –

相關問題