This is code from https://github.com/udp/json-parser (by James McLaughlin et al.), with some small modifications.