Closed dzlonline closed 3 years ago
I am not able to reproduce the problem. Could you share the code in which you encounter the problem?
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.
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 .
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.
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 .
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.