Encoder for writing messages as JSON