RestSharp: Don’t Serialize null Properties

I’m working on building a .NET API for the DreamFactory Cloud Service, with the intent of creating a full ORM implementation for it. They use a pretty straightforward REST interface for the service, so I decided that I’d give RestSharp a try for the communications layer. As seems to be typical for me, almost immediately I ran into trouble. First the APIs weren’t abundantly clear. The responsiveness of the DreamFactory support team, however was fantastic, and we got past the documentation confusion.

The next problem I ran into was the API for creating tables. The DreamFactory service has, as you would expect, a boatload of parameters for each Field in a Table. Many of the fields are interdependent. For example you would only set the Scale property if the DataType was numeric, or the MaxLength only makes sense for strings.

[SerializeAs(Name = "field")]
internal class FieldDescriptor
{
    private string m_type;
    public string name { get; set; }
    public string label { get; set; }
    public string type { get; set; }
    public string db_type { get; set; }
    public int length { get; set; }
    public int precision { get; set; }
    public int scale { get; set; }
    public bool required { get; set; }
    public bool allow_null { get; set; }
    public bool fixed_length { get; set; }
    public bool supports_multibyte { get; set; }
    public bool auto_increment { get; set; }
    public bool is_primary_key { get; set; }
    public bool is_foreign_key { get; set; }
    public string ref_table { get; set; }
    public string ref_fields { get; set; }
    public string validation { get; set; }
}

Well RestSharp serializes *all* properties in your class to JSON before shipping it across the wire, and the service was not appreciative of me setting empty values (or worse, zeros for numerics and false for booleans).

I ended up forking the RestSharp code base and adding in a SerializerOptions class, and the plumbing for it, that allows you to ignore/skip serialization of null values. So I just changed the class definition to use nullable properties:

[SerializeAs(Name = "field")]
internal class FieldDescriptor
{
    private string m_type;
    public string name { get; set; }
    public string label { get; set; }
    public string type { get; set; }
    public string db_type { get; set; }
    public int? length { get; set; }
    public int? precision { get; set; }
    public int? scale { get; set; }
    public bool? required { get; set; }
    public bool? allow_null { get; set; }
    public bool? fixed_length { get; set; }
    public bool? supports_multibyte { get; set; }
    public bool? auto_increment { get; set; }
    public bool? is_primary_key { get; set; }
    public bool? is_foreign_key { get; set; }
    public string ref_table { get; set; }
    public string ref_fields { get; set; }
    public string validation { get; set; }
}

And then when you serialize, you simply set the option (it’s off by default so as to not change the existing default behavior) and go about serializing like normal.

// build up a request to create the table
var request = new RestRequest("/rest/schema", Method.POST);
request.AddHeader("X-DreamFactory-Application-Name", Session.ApplicationName);
request.AddHeader("X-DreamFactory-Session-Token", Session.ID);
request.RequestFormat = DataFormat.Json;
request.JsonSerializer.ContentType = "application/json; charset=utf-8";
request.JsonSerializer.Options = new SerializerOptions()
{
    SkipNullProperties = true
};
request.AddBody(tableDescriptor);

If you’re interested, I’ve pushed this back up to GitHub as pull request #404.

2 thoughts on “RestSharp: Don’t Serialize null Properties”

    1. I forked the code a while ago and just build with my own project files. I don’t think RestSharp officially supports the CF any longer.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s