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

Error when parsing array elements #205

Closed dzlonline closed 3 years ago

dzlonline commented 3 years ago

Using jsmn code released on Jun 23, 2019

I have issues when certain array elements (primitives) E.g.:

{"key":[1000,2000]} -> Parses ok {"key":[10000,2000]} -> JSMN_ERROR_INVAL {"key":[1000,20000]} -> JSMN_ERROR_INVAL {"key":[10000]} -> JSMN_ERROR_INVAL {"key":[1000]} -> Parses ok

I suspect the number of characters in array elements has something to do with it. I have not defined any options.

pt300 commented 3 years ago

I am not able to reproduce the problem. Could you share the code in which you encounter the problem?

dzlonline commented 3 years ago

Hi, thanks for the quick reply. At the bottom is the code I use (Arduino). I have tried various implementations but I can not rule out that I do something wrong.

It seems that I encounter errors after the length of any array primitive has been above 4 characters.

Output:

Result: 5 Token type = 1 {"key1":[10000,20000]} Token type = 3 key1 Token type = 2 [10000,20000] Token type = 4 10000 Token type = 4 20000

============= Result: 5 Token type = 1 {"key2":[1000,2000]} Token type = 3 key2 Token type = 2 [1000,2000]} Token type = 4 1000, Token type = 4 000]}

============= Result: 5 Token type = 1 {"key3":[1000,2000]} Token type = 3 key3 Token type = 2 [1000,2000]} Token type = 4 1000, Token type = 4 000]}

============= Result: 5 Token type = 1 {"key4":[10000,20000]} Token type = 3 key4 Token type = 2 [10000,20000] Token type = 4 10000 Token type = 4 20000

Code:

include "jsmn.h"

define MAX_JSON_TOKENS 20

jsmn_parser jsonParser; jsmntok_t jsonTokens[MAX_JSON_TOKENS];

char buf[200];

char snip(char p, int s, int e) { int i = 0; p += s; while (s != e) { s++; buf[i++] = *p++; } buf[i] = 0; return buf; }

void checkParse(char* rxBuf) { int rxPtr = strlen(rxBuf); int r = jsmn_parse(&jsonParser, rxBuf , rxPtr, jsonTokens, MAX_JSON_TOKENS); Serial.println("============="); Serial.print("Result: "); Serial.println(r); if (r > 0) { for (int i = 0; i < r; i++) { Serial.print("Token type = "); Serial.print(jsonTokens[i].type); Serial.print(" "); Serial.println(snip(rxBuf, jsonTokens[i].start, jsonTokens[i].end)); } Serial.println("============="); } } void setup() { Serial.begin(115200); jsmn_init(&jsonParser);

/ checkParse("{\"key2\":[1000,2000]}"); //OK checkParse("{\"key3\":[1000,2000]}"); //OK checkParse("{\"key1\":[10000,20000]}"); //Fail checkParse("{\"key4\":[1000,2000]}"); //OK /

checkParse("{\"key1\":[10000,20000]}"); //OK checkParse("{\"key2\":[1000,2000]}"); //Parses but skewed output checkParse("{\"key3\":[1000,2000]}"); //Parses but skewed output checkParse("{\"key4\":[10000,20000]}"); //OK

}

void loop() {

}

On Fri, Mar 19, 2021 at 10:41 AM P4t @.***> wrote:

I am not able to reproduce the problem. Could you share the code in which you encounter the problem?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zserge/jsmn/issues/205#issuecomment-802693367, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7XCSHIBZU6YM4DJAVFDDDTEML53ANCNFSM4ZOMKQVQ .

pt300 commented 3 years ago

I think I see the issue. You do not reset the parser before trying to parse another JSON string. You can fix that by moving the line jsmn_init(&jsonParser); from setup() to beginning of checkParse(). Let me know if that helped fix the issue.

dzlonline commented 3 years ago

Thanks I missed that detail. Was not aware that the parser needed to be re-initialized.

Regards

Dzl

On Fri, Mar 19, 2021 at 1:06 PM P4t @.***> wrote:

I think I see the issue. You do not reset the parser before trying to parse another JSON string. You can fix that by moving the line jsmn_init(&jsonParser); from setup() to beginning of checkParse(). Let me know if that helped fix the issue.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zserge/jsmn/issues/205#issuecomment-802786513, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7XCSAUETWVZ7RA2NRGQL3TEM42RANCNFSM4ZOMKQVQ .