2014-02-22 72 views
0

我的類定義如下反序列化 - 指定默認值,以空值參數

public class HotelRoomResponse 
{ 
    public string rateCode { get; set; } 
    public string rateDescription { get; set; } 
    public RoomType RoomType { get; set; } 
    public string supplierType { get; set; } 
    public int propertyId { get; set; } 
    public BedTypes BedTypes { get; set; } 
    public string smokingPreferences { get; set; } 
    public int rateOccupancyPerRoom { get; set; } 
    public int quotedOccupancy { get; set; } 
    public int minGuestAge { get; set; } 
    public RateInfos RateInfos { get; set; } 
    public ValueAdds ValueAdds { get; set; } 
    public string deepLink { get; set; } 
    public RoomImages RoomImages { get; set; } 
} 


public class ValueAdd 
{ 
    [JsonProperty(PropertyName = "@id")] 
    public string id { get; set; } 
    public string description { get; set; } 
} 

public class ValueAdds 
{ 
    private string valueaddsize="0"; 
    [JsonProperty(PropertyName = "size")] 
    [DefaultValue("0")] 
    public string size 
    { 
     get 
     { 
      return !string.IsNullOrEmpty(valueaddsize) 
         ? valueaddsize 
         : ((DefaultValueAttribute) 
          (TypeDescriptor.GetProperties(this)["size"].Attributes[ 
           typeof(DefaultValueAttribute)])).Value.ToString(); 
     } 
     set { valueaddsize = value; } 
    } 
    [JsonConverter(typeof(ObjectToArrayConverter<ValueAdd>))] 
    public List<ValueAdd> ValueAdd { get; set; } 
} 


public class RoomImage 
{ 
    //// private string useurl = "~/no-picture-available.jpg"; 
    // [DefaultValue("~/no-picture-available.jpg")] 
    // public string url { get; set; } 

    private string useurl="~/no-picture-available.jpg"; 
    [DefaultValue("~/no-picture-available.jpg")] 
    public string url 
    { 
     get 
     { 
      return !string.IsNullOrWhiteSpace(useurl) 
         ? useurl 
         : ((DefaultValueAttribute) 
          (TypeDescriptor.GetProperties(this)["url"].Attributes[ 
           typeof(DefaultValueAttribute)])).Value.ToString(); 
     } 
     set { useurl = value; } 
    } 
} 

public class RoomImages 
{ 
    public string size { get; set; } 
    public RoomImage RoomImage { get; set; } 
} 

而在反序列化過程中的幾個參數,每次丟失即幾家酒店會產生RoomImagesValueAdds和幾個酒店不會。

我的問題是反序列化過程後,而不是發送空值,我嘗試設置默認值,但這些值不會反映後反序列化。如何將默認值設置爲RoomImages值爲「無圖像網址」,ValueAdds爲「無增值」?

回答

0

Json.Net將忽略json中不存在的屬性,但我認爲你在談論case-2(下面)。然後使用NullValueHandling枚舉就足夠了。

//Case 1 (Json doesn't contain the property) 
var image = JsonConvert.DeserializeObject<RoomImage>("{}"); 
Console.WriteLine(image.Url); //<-- ~/no-picture-available.jpg 

//Case 2 (Property is explicitly set to null) 
var settings = new JsonSerializerSettings() {NullValueHandling = NullValueHandling.Ignore }; 
image = JsonConvert.DeserializeObject<RoomImage>("{\"Url\":null}", settings); 
Console.WriteLine(image.Url); //<-- ~/no-picture-available.jpg 

public class RoomImage 
{ 
    string _url = "~/no-picture-available.jpg"; 
    public string Url { get { return _url; } set { _url = value; } } 
} 
+0

而在反序列化過程中幾個參數值空值(公共RoomImages RoomImages {獲取;集;})在HotelRoomResponse即每一次每一個人的酒店會產生客房圖片和價值增加和一些酒店會不是。 –

+0

@ Ramakrishna.p對不起,我不明白你在說什麼。如何嘗試超越技術,看看你得到了什麼? –

+0

我是DeSerializing HotelRoomResponse類不是每個單獨的類 –