Add JSON API

Also fixed some other small stuff. Requires jiffy for encoding JSON.