Added ability to specify custom json encoder

Note: There seems to be some race condition somewhere. This is triggered by
running all tests (not just a few).