JsonParserGeneratorRK Appends random data

The code builds but the parser is failing to parse. The code is here and following is the JSON from console

	parser.clear();                            	//Clear the parser state
	String parserbeat = String(beat);
	parser.addString(parserbeat);                        //Add the string command, and parse it
	if (parser.parse())
	{
    	String thisNodeID;
    	if (!parser.getOuterValueByKey("DID", thisNodeID)) {Serial.println("failed to get nodeID");}
    	else Serial.println(thisNodeID);
    	String thisNom;
    	if (!parser.getOuterValueByKey("NOM", thisNom)) {Serial.println("failed to get isNominal");}
    	else Serial.println(thisNom);
    	float floatValue;
    	if (!parser.getOuterValueByKey("BV", floatValue)) {Serial.println("failed to get BV");}
    	else Serial.printlnf("%f",floatValue);
    	int thisSoC;
    	if (!parser.getOuterValueByKey("SOC", thisSoC)) {Serial.println("failed to get State of Charge");}
    	else Serial.printlnf("%i",thisSoC);
    	String thisPWR;
    	if (!parser.getOuterValueByKey("PWR", thisPWR)) {Serial.println("failed to get Powered");}
    	else Serial.println(thisPWR);
    	String thisCHG;
    	if (!parser.getOuterValueByKey("CHG", thisCHG)) {Serial.println("failed to get Charging");}
    	else Serial.println(thisCHG);
	    //bool boolValue;
	    //if (!parser.getOuterValueByKey("TST", boolValue)) {Serial.println("failed to get bool value");}
	}
	else
	{
		Serial.println("parsing failed heartbeat command");
	}

{“DID”:“e00fce688759d02e952****”,“NOM”:“T”,“BV”:“3.8”,“SOC”:“3”,“PWR”:“F”,“CHG”:“T”}

Any suggestions why?

Are these typographic double quotes of “normal” ones?

// typographic
{“DID”:“e00fce688759d02e952****”,“NOM”:“T”,“BV”:“3.8”,“SOC”:“3”,“PWR”:“F”,“CHG”:“T”}
// normal
{"DID":"e00fce688759d02e952****","NOM":"T","BV":"3.8","SOC":"3","PWR":"F","CHG":"T"}

Can you printout the original string your parser gets fed and post that in a ```text block?

It’s not necessary to make a copy of beat in parserbeat. Just pass beat directly to addString.

It may be worthwhile to print that out to Serial and make sure it’s correct. ScruffR’s comment also applies here.

I was just trying the conversion to a string object in case that was why it wasn’t working.

snprintf(buf, sizeof(buf), "{\"DID\":\"%s\",\"NOM\":\"%c\",\"BV\":\"%.1f\",\"SOC\":\"%i\",\"PWR\":\"%c\",\"CHG\":\"%c\"}", deviceNum, isNominal?'T':'F', voltage, SoC, isMainsPowered()?'T':'F', isBatteryCharging()?'T':'F');

This is a copy from the console (which appears like this) - I don’t believe it is a double quotes issue. Not least because the JSON is valid - the console can Prettify it OK.

{"DID":"e00fce688759d02e952****","NOM":"T","BV":"3.8","SOC":"3","PWR":"F","CHG":"T"}

1 Like

I’d double check that beat is actually the value you’re expecting.

I built a unit test to validate your data and it passes:

			// {"DID":"e00fce688759d02e952abcd","NOM":"T","BV":"3.8","SOC":"3","PWR":"F","CHG":"T"}
			JsonParser jp;
			String s;

			char *data = readTestData("test2g.json");

			jp.addString(data);
			free(data);

			bool bResult = jp.parse();
			assert(bResult);

			bResult = jp.getOuterValueByKey("DID", s);
			assert(bResult);
			assert(s == "e00fce688759d02e952abcd");


			bResult = jp.getOuterValueByKey("NOM", s);
			assert(bResult);
			assert(s == "T");

			float f;
			bResult = jp.getOuterValueByKey("BV", f);
			assert(bResult);
			assert(f > 3.7 && f < 3.9);

			int i;
			bResult = jp.getOuterValueByKey("SOC", i);
			assert(bResult);
			assert(i == 3);

			bResult = jp.getOuterValueByKey("PWR", s);
			assert(bResult);
			assert(s == "F");

			bResult = jp.getOuterValueByKey("CHG", s);
			assert(bResult);
			assert(s == "T");

In my program this is defined as JsonParserStatic<255, 10> parser;

Oops, sorry I didn’t notice that earlier. That’s the problem - there are more than 10 tokens in that string.

This works in the unit test:

JsonParserStatic<256,14> jp;

Solved. I just removed the Static<255, 10> and it worked.

So when the parser.parse result is false that is a sign that the setup is insufficient?

It means that parsing failed for some reason. Either the input string is corrupted, invalid, or the buffers are too small (when using the Static version).

3 Likes