I am reading in a list of objects from JSON using this call:
Rootobject userInfo = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText(strFileName));
But I get an exception Cannot deserialize the current JSON array
. If one of the arrays within one of the class objects is empty. As long as there is data everything works.
Here is an example of JSON that is tripping up the Deserializer:
This is normal type of data for the Venue object:
"venue": {
"venue_id": 696895,
"venue_name": "Blackfinn Ameripub",
"venue_slug": "blackfinn-ameripub",
"primary_category": "Food",
"parent_category_id": "4d4b7105d754a06374d81259",
"categories": {
"count": 1,
"items": [
{
"category_name": "American Restaurant",
"category_id": "4bf58dd8d48988d14e941735",
"is_primary": true
}
]
},
"is_verified": false
},
And here is what is causing the exception, an empty array:
"venue": [
],
I have tried using the JsonSerializerSettings
options including DefaultValueHandling
, NullValueHandling
and MissingMemberHandling
but none of them seem to prevent the error.
Any idea how to deserialize the JSON and just ignore any empty arrays within the data? I'd like this to handle any empty arrays not just the example above for the object Venue
.
New issue was found - 03/17/2018 <<
Hi, the converter below has been working perfectly but the server I am getting my json responses from threw another challenge. JSON.NET has had no problem retrieving this type of data:
"toasts": {
"total_count": 1,
"count": 1,
"auth_toast": false,
"items": [
{
"uid": 3250810,
"user": {
"uid": 3250810,
"account_type": "user",
"venue_details": [
],
"brewery_details": [
]
},
"like_id": 485242625,
"like_owner": false,
"created_at": "Wed, 07 Mar 2018 07:54:38 +0000"
}
]
},
Specifically the section that has venue_details. 99% of the responses come back with venue_details in this format:
"venue_details": [
],
But then I get this format suddenly:
"toasts": {
"total_count": 1,
"count": 1,
"auth_toast": false,
"items": [
{
"uid": 4765742,
"user": {
"uid": 4765742,
"account_type": "venue",
"venue_details": {
"venue_id": 4759473
},
"brewery_details": [
],
"user_link": "https://untappd.com/venue/4759473"
},
"like_id": 488655942,
"like_owner": false,
"created_at": "Fri, 16 Mar 2018 16:47:10 +0000"
}
]
},
Notice how venue_details now has a value and includes a venue_id.
So instead venue_details ends up looking like an object instead of an array. This ends up giving this exception:
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Object]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
In the converter code provided, that exception happens in this line with *s next to it:
public abstract class IgnoreUnexpectedArraysConverterBase : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var contract = serializer.ContractResolver.ResolveContract(objectType);
if (!(contract is JsonObjectContract))
{
throw new JsonSerializationException(string.Format("{0} is not a JSON object", objectType));
}
do
{
if (reader.TokenType == JsonToken.Null)
return null;
else if (reader.TokenType == JsonToken.Comment)
continue;
else if (reader.TokenType == JsonToken.StartArray)
{
var array = JArray.Load(reader);
if (array.Count > 0)
throw new JsonSerializationException(string.Format("Array was not empty."));
return existingValue ?? contract.DefaultCreator();
}
else if (reader.TokenType == JsonToken.StartObject)
{
// Prevent infinite recursion by using Populate()
existingValue = existingValue ?? contract.DefaultCreator();
*** serializer.Populate(reader, existingValue); ***
return existingValue;
Any ideas how to add this additional handling to account for a flip like this between the JSON returning an object instead of an array?
Thanks, Rick