zserge / jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
MIT License
3.64k stars 778 forks source link

Add option to parse single objects at a time #196

Open dominickpastore opened 4 years ago

dominickpastore commented 4 years ago

This pull request is built on top of PR #194 and PR #195 (thus the commits for those branches are showing as well). If those PRs are not accepted but there is interest in this one, I can rebase this branch onto master.

This PR allows parsing one object at a time:

Normally, jsmn parses the entire input string. If multiple objects are present, it will parse all of them consecutively into the tokens array. If the last object is incomplete, jsmn returns JSMN_ERROR_PART, even if there were one or more complete objects before it. This can make parsing a stream of JSON objects difficult. The input reader must ensure the input buffer passed to jsmn ends on an object boundary.

This PR adds a new macro, JSMN_SINGLE, to provide a solution to this. When defined, jsmn will only parse one object at a time. Once it has parsed a complete object, it returns immediately, ignoring the rest of the input string. The parser state will be reinitialized, so to parse the next object, simply advance the input buffer pointer ahead by tokens[0].end and call jsmn_parse() again.

dominickpastore commented 4 years ago

Apologize for the history rewrite. Rebased onto the latest changes from the pull requests this was built on top of.