Fix a bug with JSON responses.
merge 4.0 branch to master. fix conflicts.