Reputation: 2785
I'm using an API that among other things have these two responses:
{
"enrollDeviceErrorResponse": {
"errorCode": "GRX-1056",
"errorMessage": "DEP Reseller ID missing.
Enter a valid DEP Reseller ID and resubmit your request."
}
}
or
{
"enrollDeviceErrorResponse": [
{
"errorCode": "GRX-1056",
"errorMessage": "DEP Reseller ID missing.
Enter a valid DEP Reseller ID and resubmit your request."
},
{
"errorCode": "DEP-ERR-3003",
"errorMessage": "Order information missing. T
he transaction needs to have one or more valid orders.
Enter valid orders and resubmit your request."
},
{
"errorCode": "DEP-ERR-3001",
"errorMessage": "Transaction ID missing.
Enter a valid transaction ID and resubmit your request."
}
]
}
I've created some classes that can deserialize into these responses:
public class EnrollDeviceErrorRoot
{
public EnrollDeviceErrorRoot()
{
this.EnrollDeviceErrorResponse = new EnrollDeviceErrorResponse();
}
public EnrollDeviceErrorResponse EnrollDeviceErrorResponse { get; set; }
}
public class EnrollDeviceErrorRoot
{
public EnrollDeviceErrorRoot()
{
this.EnrollDeviceErrorResponse = new EnrollDeviceErrorResponse();
}
public EnrollDeviceErrorResponse EnrollDeviceErrorResponse { get; set; }
}
public class EnrollDeviceErrorResponse
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
}
I am having a hard time coming up with a nice way to determine which one of my classes I should use depending on the response. I currently have this failing code:
var multipleErrors = JsonConvert.DeserializeObject<EnrollDeviceErrorsRoot>(response);
if (multipleErrors.EnrollDeviceErrorResponse != null && multipleErrors.EnrollDeviceErrorResponse.Any())
{
this.StatusCode = multipleErrors.EnrollDeviceErrorResponse.Select(x => x.ErrorCode).Aggregate((a, b) => a + ", " + b);
this.StatusMessage = multipleErrors.EnrollDeviceErrorResponse.Select(x => x.ErrorMessage).Aggregate((a, b) => a + Environment.NewLine + b);
this.HasErrors = true;
return;
}
var singleError = JsonConvert.DeserializeObject<EnrollDeviceErrorRoot>(response);
if (!string.IsNullOrEmpty(singleError.EnrollDeviceErrorResponse.ErrorCode))
{
this.StatusCode = singleError.EnrollDeviceErrorResponse.ErrorCode;
this.StatusMessage = singleError.EnrollDeviceErrorResponse.ErrorMessage;
this.HasErrors = true;
return;
}
Error from code above:
Newtonsoft.Json.JsonSerializationException : Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Atea.Dep.Core.Service.Models.EnrollDeviceErrorResponse]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer,
not a collection type like an array or List<T>) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
How are other people handling this deserializations from responses when they don't know what they are getting back from the server? I could of course just look at the raw response and determine that way but are there other cleaner ways to do this? I Can't change the API, I have no idea why there just couldn't be one response with a list with one or more errors.
Upvotes: 0
Views: 1161
Reputation: 7973
You can use something like this:
var output;
dynamic jObject = JsonConvert.DeserializeObject (outputAPI);
bool isArray = jObj.enrollDeviceErrorResponse.Type == JTokenType.Array;
bool isObject = jObj.enrollDeviceErrorResponse.Type == JTokenType.Object;
if(isObject)
output = JsonConvert.DeserializeObject<EnrollDeviceErrorResponse>(outputAPI);
//else you will use the other class to deserialize the object
Upvotes: 1
Reputation: 1579
You can use
JObject.Parse(response)["enrollDeviceErrorResponse"].Type
to determine the type of the response. In 1st case it will be obect, in 2nd - array. Then it will be easy to continue with proper deserialization.
Upvotes: 2