JSON parser fixes and new testcases

Addresses point 1 and 2 in #563 but left 3 due to the risk of
regression.
Merge pull request #594 from mrange/master

JSON parser fixes and new testcases