Add JSON support.
Refactor implementation.