16

RestSharp's built-in JSON serializer serializes all of an object's properties, even if they're null or otherwise the default value. How can I make it skip these properties?

Bryan
  • 1,431
  • 1
  • 17
  • 22
  • 2
    Json.NET has such a parameter. `JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore` ... dunno about RestSharp – thenewseattle Nov 15 '13 at 17:20

3 Answers3

10

An alternative, you can use other json libraries (json.net, servicestack.text, etc.) which support ignoring null values to serialize it first:

RestRequest request = new RestRequest();
...
string jsonString = ThirdPartySerialization(jsonObject);
request.AddParameter("application/json", jsonString, ParameterType.RequestBody);
leo1987
  • 131
  • 1
  • 6
8

You can use a custom IJsonSerializerStrategy together with the default SimpleJson JSON serializer to ignore null values.

The easiest way to do it is to extend the PocoJsonSerializerStrategy like below.

public class IgnoreNullValuesJsonSerializerStrategy : PocoJsonSerializerStrategy
{
    protected override bool TrySerializeUnknownTypes(object input, out object output)
    {
        bool returnValue = base.TrySerializeUnknownTypes(input, out output);

        if (output is IDictionary<string, object> obj)
        {
            output = obj.Where(o => o.Value != null).ToDictionary(o => o.Key, o => o.Value);
        }

        return returnValue;
    }
}

And then use it as the default serializer strategy.

SimpleJson.CurrentJsonSerializerStrategy = new IgnoreNullValuesJsonSerializerStrategy();
Conyc
  • 460
  • 1
  • 4
  • 10
  • Thanks for this, but not currently working with current version of RestSharp as of at least 106.1.0, currently waiting on issue: https://github.com/restsharp/RestSharp/issues/1018 Hopefully once that is resolved this will work again or at least something very similar. – James Eby Dec 08 '17 at 23:07
  • @JamesEby that is incorrect. This solution still works and is not related to the issue you mentioned. The solution I posted configures the serializer strategy directly on the static SimpleJson class that RestSharp uses and is thus not affected by the limited configuration options in RestSharp. It is even mentioned in the official documentation that it is possible to customize the serializer strategy like this (at the bottom of this page): https://github.com/restsharp/RestSharp/wiki/Deserialization – Conyc Dec 13 '17 at 13:40
  • 1
    What I am saying is that static class does not exist in the code anymore, or at least It appeared to have been removed. I could find no such SimpleJson configuration class in RestSharp as of the latest version. If I am wrong and you still see it please let me know. – James Eby Dec 13 '17 at 14:34
  • 2
    @JamesEby it is still there, but it seems like its namespace has changed in 106.1.0. In earlier versions, it was in the `RestSharp` namespace, while it seems to be in the `SimpleJson` namespace now. I'll update my answer to reflect this. Thanks for pointing it out! – Conyc Dec 13 '17 at 15:20
  • SimpleJson has moved back to the RestSharp namespace. – Conyc May 14 '20 at 12:43
0

Here is a link to a version that has been modified to ignore null values. You just need to set the serializer options to ignore nulls.

Restsharp that ignores null values

George
  • 430
  • 1
  • 4
  • 16