3

I am talking to an webservice that that can return the following JSON structure:

{
    "foo": {
        "bar": "hello world"
    }
}

Foo is optional, but instead of the value NULL, we get the following:

{
    "foo": []
}

An empty array. I used an (ugly) work around for this by using the following properties in my model:

public dynamic Foo { get; set; }
public FooModel FooObject {
    get
    {
        if(Foo == null || Foo.GetType().IsArray)
        {
            return null;
        }
        return ((JObject)Foo).ToObject<FooModel>();
    }
}

This works for this single property. But the webservice does this for all objects that are NULL. I'm looking for a generic solution that ignores all empty arrays when deserialization. (or something else)

I haven't been able to find a solution. I tried looking into using an custom JsonConverter and ContractResolver.

Edwin Stoteler
  • 1,218
  • 1
  • 10
  • 25
  • Have you looked here for ideas: https://stackoverflow.com/a/18997172/226781 – asherber Jun 05 '18 at 14:30
  • @asherber Yea I have seen it, it would be a possible solution, but it would require to change all my properties in my models to arrays/lists and put attributes on them all. Wouldn't be much better than my current workaround. – Edwin Stoteler Jun 05 '18 at 14:41
  • Another possibly related question: [Deserialize JSON when type can be different](https://stackoverflow.com/q/29449641/3744182). But if you say you have to do it for *every possible* property including those with primitive values, the accepted answer may not work. – dbc Jun 05 '18 at 20:47
  • 1
    Alternatively, pre-load your JSON into a `JToken` and use `RemoveEmptyArrayProperties` from [Woo commerce json to Dataset or datatable](https://stackoverflow.com/a/38527790/3744182). – dbc Jun 05 '18 at 21:10
  • @dbc, the RemoveEmptyArrayProperties worked! Thx. will you answer or shall I create a Community Wiki answer? – Edwin Stoteler Jun 06 '18 at 06:17
  • @EdwinStoteler - [Woo commerce json to Dataset or datatable](https://stackoverflow.com/q/38520792/3744182) has a bunch of different things going on while your question is very straightforward so it probably should get its own answer. Will write one up. – dbc Jun 06 '18 at 08:09

1 Answers1

1

One simple way do avoid the problem is to pre-load the JSON into a JToken then remove all empty array properties using the extension method RemoveEmptyArrayProperties from this answer to Woo commerce json to Dataset or datatable:

public static class JsonExtensions
{
    public static TJToken RemoveEmptyArrayProperties<TJToken>(this TJToken root) where TJToken : JToken
    {
        var container = root as JContainer;
        if (container == null)
            return root;
        var query = container.DescendantsAndSelf()
            .OfType<JProperty>()
            .Where(p => p.Value is JArray && ((JArray)p.Value).Count == 0);
        foreach (var property in query.ToList())
        {
            property.Remove();
        }

        return root;
    }
}

Given the method, you can pre-process your JSON string as follows:

var root = JObject.Parse(jsonString)
    .RemoveEmptyArrayProperties()
    .ToObject<RootObject>();

Sample working .Net fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340