change write to apply JSON.stringify only to objects