1. Serge Zaitsev
  2. jsmn
  3. Issues
Issue #25 wontfix

Create "all-in-one" function to allocate memory and parse

Gabriel Gritsch
created an issue

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). Again, 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 ;-))

jsmntok_t *jsmn_parseV2(const char *js, size_t len, unsigned int *num_tokens)
{
    if (num_tokens)
    {
        *num_tokens = 0;
    }

    jsmn_parser parser;
    jsmn_init(&parser);

    int ret = jsmn_parse(&parser, js, len, NULL, 0);
    if (ret <= 0)
    {
        return NULL;
    }

    jsmntok_t *tokens = (jsmntok_t *)calloc(ret, sizeof(jsmntok_t));
    if (!tokens)
    {
        return NULL;
    }

    jsmn_init(&parser); // reset it

    ret = jsmn_parse(&parser, js, len, tokens, ret);
    if (ret <= 0)
    {
        free(tokens);

        return NULL;
    }

    if (num_tokens)
    {
        *num_tokens = ret;
    }

    return tokens;
}

this function could be used like this:

jsmntok_t *tokens = jsmn_parseV2(json_data, json_length, NULL);
if (tokens)
{
    //work with tokens

    free(tokens);
}

or like this:

unsigned int token_count;
jsmntok_t *tokens = jsmn_parseV2(json_data, json_length, &token_count);
if (tokens && token_count)
{
    // work with token_count tokens

    free(tokens);
}

Comments (5)

  1. Serge Zaitsev repo owner

    It's unlikely to be included into jsmn library, since one of the key features of jsmn is zero dependencies, not even a libc. That makes it possible to use jsmn on tiny 8-bit microcontrollers like AVR (that's from where it all has started). Your code is using calloc(), which is ok for modern OSes, but would cause problems for running on bare hardware.

    However, I think that such snippets of code should be included into documentation (or maybe wiki?) to save new users with the same problems from reinventing the wheel. The code itself looks good to me.

    Another example of code that won't be included into jsmn is reducing the tokens during incremental parse, posted by some clever guy long time ago to the issue tracker. It is also very useful for some cases, but uses memmove(), which is part of the libc. And it's more like a handy helper function, rather than an essential part of the minimalistic parser.

  2. Gabriel Gritsch reporter

    ok, that sounds reasonable.

    does it make sense to add some code that may help to the documentation or to another *.c file (compare token values, extract string/number token values, code like above and so on)

  3. bitjuli

    jsmn_parseV2 uses the return value of jsmn_parse as the number of elements for calloc but the jsmn_parse code I have returns error values (<0) or 0 for success.

  4. Log in to comment