{"action":"created","issue":{"url":"https://api.github.com/repos/zserge/jsmn/issues/26","labels_url":"https://api.github.com/repos/zserge/jsmn/issues/26/labels{/name}","comments_url":"https://api.github.com/repos/zserge/jsmn/issues/26/comments","events_url":"https://api.github.com/repos/zserge/jsmn/issues/26/events","html_url":"https://github.com/zserge/jsmn/issues/26","id":109784263,"number":26,"title":"Create \"all-in-one\" function to allocate memory and parse","user":{"login":"zserge","id":1911760,"avatar_url":"https://avatars.githubusercontent.com/u/1911760?v=3","gravatar_id":"","url":"https://api.github.com/users/zserge","html_url":"https://github.com/zserge","followers_url":"https://api.github.com/users/zserge/followers","following_url":"https://api.github.com/users/zserge/following{/other_user}","gists_url":"https://api.github.com/users/zserge/gists{/gist_id}","starred_url":"https://api.github.com/users/zserge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zserge/subscriptions","organizations_url":"https://api.github.com/users/zserge/orgs","repos_url":"https://api.github.com/users/zserge/repos","events_url":"https://api.github.com/users/zserge/events{/privacy}","received_events_url":"https://api.github.com/users/zserge/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-10-05T12:19:08Z","updated_at":"2015-10-05T12:19:09Z","closed_at":null,"body":"To make it a bit easier to use the tow-stage-parsing method a helper-function could be added. It is not reasonable for everyone to call the init-function jsmn_init() a second time (to reset the position).\nAgain, the functions name is not sensful (you could chose one) but I think the function it self should be correct (if not, please tell me then I can fix it in my own code ;-))  \n\n\tjsmntok_t *jsmn_parseV2(const char *js, size_t len, unsigned int *num_tokens)\n\t{\n\t\tif (num_tokens)\n\t\t{\n\t\t\t*num_tokens = 0;\n\t\t}\n\t\t\n\t\tjsmn_parser parser;\n\t\tjsmn_init(&parser);\n\t\t\n\t\tint ret = jsmn_parse(&parser, js, len, NULL, 0);\n\t\tif (ret <= 0)\n\t\t{\n\t\t\treturn NULL;\n\t\t}\n\t\t\n\t\tjsmntok_t *tokens = (jsmntok_t *)calloc(ret, sizeof(jsmntok_t));\n\t\tif (!tokens)\n\t\t{\n\t\t\treturn NULL;\n\t\t}\n\t\t\n\t\tjsmn_init(&parser);\t// reset it\n\t\t\n\t\tret = jsmn_parse(&parser, js, len, tokens, ret);\n\t\tif (ret <= 0)\n\t\t{\n\t\t\tfree(tokens);\n\t\t\t\n\t\t\treturn NULL;\n\t\t}\n\t\t\n\t\tif (num_tokens)\n\t\t{\n\t\t\t*num_tokens = ret;\n\t\t}\n\t\t\n\t\treturn tokens;\n\t}\n\nthis function could be used like this:\n\n\tjsmntok_t *tokens = jsmn_parseV2(json_data, json_length, NULL);\n\tif (tokens)\n\t{\n\t\t//work with tokens\n\t\t\n\t\tfree(tokens);\n\t}\n\nor like this:\n\n\tunsigned int token_count;\n\tjsmntok_t *tokens = jsmn_parseV2(json_data, json_length, &token_count);\n\tif (tokens && token_count)\n\t{\n\t\t// work with token_count tokens\n\t\t\n\t\tfree(tokens);\n\t}\n\n----------------------------------------\n- Bitbucket: https://bitbucket.org/zserge/jsmn/issue/25\n- Originally reported by: Gabriel Gritsch\n- Originally created at: 2014-02-21T13:26:04.744\n"},"comment":{"url":"https://api.github.com/repos/zserge/jsmn/issues/comments/145510079","html_url":"https://github.com/zserge/jsmn/issues/26#issuecomment-145510079","issue_url":"https://api.github.com/repos/zserge/jsmn/issues/26","id":145510079,"user":{"login":"zserge","id":1911760,"avatar_url":"https://avatars.githubusercontent.com/u/1911760?v=3","gravatar_id":"","url":"https://api.github.com/users/zserge","html_url":"https://github.com/zserge","followers_url":"https://api.github.com/users/zserge/followers","following_url":"https://api.github.com/users/zserge/following{/other_user}","gists_url":"https://api.github.com/users/zserge/gists{/gist_id}","starred_url":"https://api.github.com/users/zserge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zserge/subscriptions","organizations_url":"https://api.github.com/users/zserge/orgs","repos_url":"https://api.github.com/users/zserge/repos","events_url":"https://api.github.com/users/zserge/events{/privacy}","received_events_url":"https://api.github.com/users/zserge/received_events","type":"User","site_admin":false},"created_at":"2015-10-05T12:19:09Z","updated_at":"2015-10-05T12:19:09Z","body":"jsmn is too small for this\n\n----------------------------------------\nOriginal comment by: Serge Zaitsev\n"}}