properly handle null values in toJSON