ExecuteAsync<T> vs. JsonConvert.DeserializeObject

Dec 31, 2010 at 6:48 AM

Hi,

I got stuck on this one today, managed to get it working but don't really understand why so I'm posting here for some feedback.  Given a class defn of:

  [JsonObject(MemberSerialization.OptIn)]
  [DataContract]
  public class NeighbourhoodStats
  {
    public NeighbourhoodStats()
    {
      HouseTypeSummary = new Dictionary<stringint>();
    }

    [DataMember]
    [JsonProperty]
    public string YearIndex { getset; }
    [DataMember]
    [JsonProperty]
    public string QuarterIndex { getset; }
    [DataMember]
    [JsonProperty]
    public string MonthIndex { getset; }
    [DataMember]
    [JsonProperty]
    public string RegionName { getset; }
    [DataMember]
    [JsonProperty]
    public string HomeCategoryName { getset; }
    [DataMember]
    [JsonProperty]
    public int NoBedrooms { getset; }
    [DataMember]
    [JsonProperty]
    public int OneBedroom { getset; }
    [DataMember]
    [JsonProperty]
    public int TwoBedroom { getset; }
    [DataMember]
    [JsonProperty]
    public int ThreeBedroom { getset; }
    [DataMember]
    [JsonProperty]
    public int FourBedroom { getset; }
    [DataMember]
    [JsonProperty]
    public int FivePlusBedroom { getset; }
    [DataMember]
    [JsonProperty]
    public int AverageNumberOfBathroom { getset; }
    [DataMember]
    [JsonProperty]
    public int PriceLessThan250K { getset; }
    [DataMember]
    [JsonProperty]
    public int Price250kTo350k { getset; }
    [DataMember]
    [JsonProperty]
    public int Price350kTo500k { getset; }
    [DataMember]
    [JsonProperty]
    public int Price500kTo750k { getset; }
    [DataMember]
    [JsonProperty]
    public int Price750kTo1MM { getset; }
    [DataMember]
    [JsonProperty]
    public int PriceGreaterThan1MM { getset; }
    [DataMember]
    [JsonProperty]
    public int NumberofHousesSold { getset; }
    [DataMember]
    [JsonProperty]
    public double MedianPrice { getset; }
    [DataMember]
    [JsonProperty]
    public double AveragePrice { getset; }

    [DataMember]
    [JsonProperty]
    public Dictionary<stringint> HouseTypeSummary { getset; }
  }

And JSON from a rest service of:

{"YearIndex":"2010","QuarterIndex":null,"MonthIndex":null,"RegionName":"The Neighbourhood","HomeCategoryName":"ALL","NoBedrooms":0,"OneBedroom":0,"TwoBedroom":6,"ThreeBedroom":25,"FourBedroom":10,"FivePlusBedroom":0,"AverageNumberOfBathroom":3,"PriceLessThan250K":0,"Price250kTo350k":2,"Price350kTo500k":7,"Price500kTo750k":22,"Price750kTo1MM":11,"PriceGreaterThan1MM":0,"NumberofHousesSold":41,"MedianPrice":0,"AveragePrice":640402.2195,"HouseTypeSummary":{"Semi-Detached":20,"Detached":18,"Townhouse":2,"Condo Apartment":1}}

Why does:

        RestClient.ExecuteAsync<NeighbourhoodStats>(request, (response) =>
        {
          ChartDataEventArgs args = new ChartDataEventArgs(response.Data);
          StatLoadingComplete(this, args);
        });   

Fail to load the Dictionary HouseTypeSummary BUT...
        RestClient.ExecuteAsync(request, (response) =>
        {
          NeighbourhoodStats s = JsonConvert.DeserializeObject<NeighbourhoodStats>(response.Content);          
          ChartDataEventArgs args = new ChartDataEventArgs(s);
          StatLoadingComplete(this, args);
        });      
this code properly populates all fields from NeighbourhoodStats???