JSON parser fixes and new testcases

Addresses point 1 and 2 in #563 but left 3 due to the risk of
regression.