fix toJSON method to allow attributes to be undefined