Jason
Jason

Reputation: 463

Why is my json deserialization failing?

I have the following two objects (which I do not control and can not change):

[Serializable]
[DataContract]
public class AddressContactType : BaseModel
{
    public AddressContactType();

    [DataMember]
    public string AddressContactTypeName { get; set; }
}

[Serializable]
[DataContract]
public abstract class BaseModel
{
    protected BaseModel();

    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string NativePMSID { get; set; }
    [DataMember]
    public string PMCID { get; set; }
}

I am using RestClient to make a GET call to retrieve this data in JSON. The request succeeds. The returned JSON is:

[{"Id":0,"NativePMSID":"1","PMCID":"1020","AddressContactTypeName":"Home"},{"Id":0,"NativePMSID":"2","PMCID":"1020","AddressContactTypeName":"Apartment"},{"Id":0,"NativePMSID":"3","PMCID":"1020","AddressContactTypeName":"Vacation"},{"Id":0,"NativePMSID":"3","PMCID":"1020","AddressContactTypeName":"Other"}]

From that point I attempt to deserialize the data in three different ways.

My code:

var request = new RestRequest("AddressContactType", Method.GET);
        request.AddHeader("Accept", "application/json");
        request.AddParameter("PMCID", "1020");

        #region JSON Deserialization

        // ---- Attempt #1
        var response = client.Execute<AddressContactType>(request);

        // ---- Attempt #2
        var myResults = response.Content;

        var ms = new MemoryStream(Encoding.UTF8.GetBytes(myResults));
        var ser = new DataContractJsonSerializer(typeof(AddressContactType));
        var result = (AddressContactType)ser.ReadObject(ms);

        // ---- Attempt #3
        var jsonSettings = new JsonSerializerSettings()
        {
            Formatting = Formatting.Indented,
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
            DateTimeZoneHandling = DateTimeZoneHandling.Utc,
            PreserveReferencesHandling = PreserveReferencesHandling.Objects
        };

        var result2 = new AddressContactType();
        result2 = JsonConvert.DeserializeObject<AddressContactType>(new StreamReader(ms).ReadToEnd(), jsonSettings);

        #endregion

Under attempt 1, the RestClient attempt returns the error: "Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."

Under attempt 2, the object result is shown with the correct properties (Id, NativePMSID, PMCID and AddressContactTypeName) but they are all null and only one instance of each is shown.

Attempt 3 just returns a null value for result2.

Any suggestions?

Thanks.

Upvotes: 3

Views: 2942

Answers (1)

Jason
Jason

Reputation: 463

It appears the solution to my problem is:

        List<AddressContactType> myResults2;

        using (Stream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(myResults)))
        {
            myResults2 = JsonConvert.DeserializeObject<List<AddressContactType>>(new StreamReader(ms2).ReadToEnd());
        }

I was close with one of the previous steps, but this gave me a complete list.

Upvotes: 3

Related Questions