We should try and reuse JSONOutputSerializer code for this. I also think serialization should belong in its own file.