Taken from https://github.com/postgis/postgis/pull/173, tested with json-c 0.12 and 0.13. Similar diff from bket@