improve json encoder and add tests