Fix toJson method on null attributes

When the model has some attribute set to null, the exception was
raised "cannot access property toJson of null." This piece of code
fixes the issue.