2

I'm using Json.Net to parse JSON into my app logic. The problem is that the external API that I get the JSON from sometimes has "null" items inside their lists. I would like to remove those "null" items from the list (or any other IEnumerable that might have that) at parse time. I believe the solution has to be using a JsonConverter but I was unable to get it working so far.

MyData data = new MyData();
Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings
{
  Converters = new List<JsonConverter>() { new TrimNullListValues() }
};
string jsonString = @"{""ListData"": [{""source"" : 10 , ""target"" : 20, ""Data"" : [{""source"" : 100 , ""target"" : 200}, null]}, null]}";
JsonConvert.PopulateObject(jsonString, data, settings);

MyData class is like this:

public class MyData {
  public class MyNestedData
  {
    public int Source;
    public int Target;
    public List<MyNestedData> Data;
  }

  public List<MyNestedData> ListData;
}

My JsonConverter (TrimNullListValues) is like this:

public class TrimNullListValues : JsonConverter {
  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  {
    serializer.Serialize(writer, value);
  }

  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  {
    // Don't really know what to do in here to remove unwanted values
    // From the IEnumerabes
  }

  public override bool CanConvert(Type objectType)
  {
    return objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(List<>);
  }
}
2
  • See if it helps to you stackoverflow.com/questions/16455837/… Commented Jun 27, 2016 at 13:32
  • No that doesn't work for me :( It would work if the whole list was null but not for values inside the list. Thx anyway Commented Jun 27, 2016 at 13:50

1 Answer 1

2

You could try something like this:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    JArray array = JArray.Load(reader);
    foreach (JToken item in array.ToList())
    {
        if (item.Type == JTokenType.Null)
            item.Remove();
    }
    object list = Activator.CreateInstance(objectType);
    serializer.Populate(array.CreateReader(), list);
    return list;
}

Fiddle: https://dotnetfiddle.net/SESCfZ

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works fine for that example. But if i have a list inside "MyNestedData" with some null values those are not removed. dotnetfiddle.net/ojnMWd
Oh, your structure is recursive? You probably should have mentioned that in the question. I've updated my answer.

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.