5

I'm requesting a booking information from my ASP.NET Web API depending on the booking number given by the user. My issue is, if the booking number does not exist the Web API is still returning an object but the values are null. How can I check if the returned JSON object is null?

HttpClient request:

 var response = await client.PostAsJsonAsync(strRequestUri, value);

if (response.IsSuccessStatusCode)
{
    string jsonMessage;
    using (Stream responseStream = await response.Content.ReadAsStreamAsync()) // put response content to stream
    {
        jsonMessage = new StreamReader(responseStream).ReadToEnd(); 
    }
    // I'm getting the error from here when I'm casting the json object to my return type.
    return (TOutput)JsonConvert.DeserializeObject(jsonMessage, typeof(TOutput)); // TOutput is a generic object
}

sample returned JSON object:

{
    "BookingRef": null,
    "City": null,
    "Company": null,
    "Country": null,
    "CustomerAddress": null,
    "CustomerFirstName": null,
    "CustomerPhoneNumber": null,
    "CustomerSurname": null,
    "Entrance": null
}
2
  • Why are you using a generic object instead of defining an object you can strongly use? Commented Jun 30, 2017 at 21:12
  • 1
    have you ever found solution for this? i am having the same problem. it throws on deseralization part as well for me Commented Sep 6, 2018 at 22:31

1 Answer 1

2

One option is to use late binding on the property:

var result = JsonConvert.DeserializeObject(jsonMessage, typeof(TOutput));
if (((dynamic)result).BookingRef == null)
{
    // Returning null - do whatever is appropriate
    return null;
}
else
{
    return (TOutput)result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Lars, I'm already getting the error when I do JsonConvert.DeserializeObject(jsonMessage, typeof(TOutput)). Also I forgot to mention that TOutput is a generic object
Hi @rcadaoas, I did assume that TOutput was most likely generic. :) Can you be specific about "the error" that you are getting? Is an exception being thrown by the DeserializeObject call? If the call is simply returning an object with a bunch of null values, then the approach above should still work. Otherwise, you can wrap the DeserializeObject call with a try-catch block.
Hi Lars. yes, the error "Null object cannot be converted to a value type" is being thrown by the DeserializeObject. and yes Im already wrapping it with a try catch block.
Okay, but now I'm confused. What is the "sample returned json object" that you posted? I assumed that was the result of the DeserializeObject call. If it's not, what is that JSON snippet showing?
While the above answers the question in the body, it does not address the error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.