Refactor JSON serialization