2011-06-23 46 views
2

我試圖使用JSON.net庫反序列化GeoJSON。基於「type」屬性值,每個要素的幾何組成部分可以有許多不同的類型。使用JSON.net將JSON反序列化爲.net基類

我需要這個以GeoJSON的幾何分量反序列化爲幾何對象模型像這樣:

public abstract class Geometry { ... } 

public class Point : Geometry { ... } 

public class LineString : Geometry { ... } 

public class Polygon : Geometry { ... } 

所以基於所述「type」屬性的值,這將反序列化爲相應的.NET混凝土類型,但通過其基本幾何類訪問。

是否JSON.net庫提供類似於WCF中KnownTypeAttributeXmlElementAttribute在XML序列化,讓我反序列化JSON的基類與一組已知的派生類的東西嗎?

回答

4

文檔here顯示了這個例子:

[JsonObject(MemberSerialization.OptIn)] 
    public class Person 
    { 
     // "John Smith" 
     [JsonProperty] 
     public string Name { get; set; } 

     // "2000-12-15T22:11:03" 
     [JsonProperty] 
     [JsonConverter(typeof(IsoDateTimeConverter))] 
     public DateTime BirthDate { get; set; } 

     // new Date(976918263055) 
     [JsonProperty] 
     [JsonConverter(typeof(JavaScriptDateTimeConverter))] 
     public DateTime LastModified { get; set; } 

     // not serialized 
     public string Department { get; set; } 
    } 
+0

這是所有關於JsonConverter因爲我發現。 – jumpinjackie