Implement JSON encoding.