1. Serge Zaitsev
  2. jsmn
  3. Issues
Issue #32 new

Incorrect parsing of JSON string when JSMN_PARENT_LINKS switch is enabled

Uranuz CatDog
created an issue

When using this library for parsing JSON on STM32F4 microcontroller with JSMN_PARENT_LINKS I get unexpected structure of jsmntok_t array. It is not corresponding to my input JSON string:

{"cmd":"mo_init_q","qb": [1.1, 1.2, 1.3],"qe": [2.1, 2.2, 2.3],"some": [{"lib":"some.a", "src": "some.c", "ver": 100},{"lib":"another.a", "src": "another.c", "ver": 50}]}

And it also contains incorrect type properties. May be they have not been set at all and contain some "garbage".

The following text is data of token array from the debugger: {{type = JSMN_OBJECT, start = 0, end = 172, size = 8, parent = 3}, {type = JSMN_ARRAY, start = 5, end = 0, size = 3, parent = 8}, {type = 17, start = 0, end = 3, size = 20, parent = 22}, {type = JSMN_PRIMITIVE, start = 2, end = 25, size = 40, parent = 3}, {type = JSMN_PRIMITIVE, start = 26, end = 29, size = 0, parent = 0}, {type = 31, start = 34, end = 0, size = 0, parent = 36}, {type = 39, start = 0, end = 3, size = 42, parent = 44}, {type = JSMN_PRIMITIVE, start = 2, end = 47, size = 62, parent = 3}, {type = JSMN_PRIMITIVE, start = 48, end = 51, size = 0, parent = 0}, {type = 53, start = 56, end = 0, size = 0, parent = 58}, {type = 61, start = 0, end = 3, size = 64, parent = 68}, {type = JSMN_PRIMITIVE, start = 2, end = 71, size = 171, parent = 2}, {type = JSMN_OBJECT, start = 72, end = 118, size = 6, parent = 3}, {type = 74, start = 77, end = 0, size = 3, parent = 81}, {type = 87, start = 0, end = 3, size = 91, parent = 94}, {type = JSMN_PRIMITIVE, start = 3, end = 98, size = 104, parent = 0}, {type = JSMN_STRING, start = 108, end = 111, size = 0, parent = 0}, {type = 114, start = 117, end = 0, size = 1, parent = 119}, {type = 170, start = 6, end = 3, size = 121, parent = 124}, {type = JSMN_PRIMITIVE, start = 3, end = 128, size = 137, parent = 0}, {type = JSMN_STRING, start = 141, end = 144, size = 0, parent = 3}, {type = 148, start = 157, end = 0, size = 3, parent = 161}, {type = 164, start = 0, end = 0, size = 167, parent = 169}, {type = JSMN_PRIMITIVE, start = 0, end = 0, size = 0, parent = 0} <repeats 77 times>}

There is also screeshoot of debugger info from my toolchain in annex

Comments (3)

  1. Serge Zaitsev repo owner

    From the debugger output I can tell that the number of tokens was estimated correctly, but only the root token was filled in normally (e.g. Object type, 8 child nodes). The others are filled with garbage.

    I wonder if you can reproduce it and if you can do step-by step debugging? Since it's the 2nd token that fails to parse - it won't take much time. I tried parsing your JSON with various jsmn build options - and they all worked fine, so I can't really reproduce it (linux/amd64).

    Also, I wonder if JSMN_PARENT_LINKS has any affect on this issue - can you reproduce it without that option?

    Finally, please note that some changes to jsmn code has happened recently - it now can tell object keys from values and thus the node tree structure is now different.

  2. Dirk Nerinckx

    I have exactly the same problem. I used the jsmn code on an ARM7 and it works fine when I don't use JSMN_PARENT_LINKS. On the first try with a relatively simple JSON object, I also got garbage in the tokens when defining JSMN_PARENT_LINKS.

  3. Dirk Nerinckx

    I made the code of jsmn MISRA compliant which was a tremendous job because there were tons of violations. Surprisingly enough, the problem with the JSMN_PARENT_LINKS was gone after this. I don't have the time to find out what went wrong with the original code when using JSMN_PARENT_LINKS.

  4. Log in to comment