The business rules are simple. We have a method that takes a JObject as a parm. Convert it to a c# poco.
The json needs to represent a single object.
No arrays allowed. If you need to do it three times call the method three times.
So for example this would be valid json:
{
"CustomerId": 669616948,
"FirstName": "ERIC",
"LastName": "TEST2",
"BirthYear": 0,
"BirthMonth": 0,
"CustomerState": 0,
"LegalAddressState": null,
"Username": "ERIC2222"
}
this would not:
{
"Participants": [
{
"CustomerId": 669616948,
"FirstName": "ERIC",
"LastName": "TEST2",
"BirthYear": 0,
"BirthMonth": 0,
"CustomerState": 0,
"LegalAddressState": null,
"Username": "ERIC2222"
}
]
}
Currently this throws an exception when it tries to convert to a poco and while we can handle the exception I was looking for a way to detect if the JObject contained an array and gracefully exit.
So the json above is just a representation of what the JObject would look like but it IS a JObject.
The best I have been able to come up with is a measly string check.
JObject.ToString().Contains("[")
Any ideas on how to do an array check. If I could somehow get it to a JToken then I could do this (temp is of type JToken):
temp.Type == JTokenType.Array
TIA
As requested here is the conversion. payload is a JObject.
var customer = payload.ToObject<Customer>(_serializer);