squix78 / json-streaming-parser

Arduino library for parsing potentially huge json streams on devices with scarce memory
MIT License
208 stars 88 forks source link

Can not parse recursive #12

Open bcatalin opened 7 years ago

bcatalin commented 7 years ago

Hi Daniel,

I have an issue with the stream parser. I am trying to parse each line from a file in SPIFFS and is only working for the first line.

#include <FS.h> 
#include "JsonStreamingParser.h"
#include "JsonListener.h"
#include "ExampleParser.h"

JsonStreamingParser parser;
ExampleListener listener;
float temp_t1 = 33.45;
float temp_t2 = -127.00;

void setup() {
  Serial.begin(115200);
  Serial.println(String(ESP.getFreeHeap()));
  SPIFFS.begin();
  parser.setListener(&listener);
  /* //run this code once just have the file !!!!!!!!!!!!!!!!!!!
  File unsent = SPIFFS.open("/unsent.json", "a+");
  if (!unsent) {
    Serial.println("failed to open file for writing :-(");
    return; //==============>
  }

  for(int i =0; i< 10; i++)
  {
   char temp1[10];
   char temp2[10];
   sprintf(temp1,"%s", String(temp_t1).c_str());
   sprintf(temp2,"%s", String(temp_t2).c_str()); 
   Serial.printf("SIZE :%d\n", unsent.size());
   char strTemp[64];
   if(unsent.size() == 0)
     sprintf(strTemp,"{\"t1\":\"%s\",\"t2\":\"%s\"}\n",temp1, temp2);
   else
     sprintf(strTemp,"{\"t1\":\"%s\",\"t2\":\"%s\"}\n",temp1, temp2);

   Serial.println(strTemp);
   unsent.print(strTemp);
  }
  unsent.close();//new CC 
*/  

  if(SPIFFS.exists("/unsent.json"))
  {
    Serial.println(F("I have unsent data"));
    File unsentFile = SPIFFS.open("/unsent.json","r");
    if(unsentFile)
    {
      Serial.printf("unsent.json size:%d",unsentFile.size()); Serial.println("");
      while(unsentFile.available())
      {
        String line = unsentFile.readStringUntil('\n');
        Serial.println(line); 
        char parse_line[200];
        line.toCharArray(parse_line, 200);
        for (int i = 0; i < sizeof( parse_line ); i++) {          
          parser.parse(parse_line[i]); 
        }
      }
    }
  }

  Serial.println(String(ESP.getFreeHeap()));
}

void loop() {
  // put your main code here, to run repeatedly:

}
alieslam commented 7 years ago

Hi bcatalin, Declare the JsonParser object and set the listener object too locally and it will parse json objects recursively(check bellow). I have managed to debug the problem and i found that if comes from the if statement where it checks the "state" variable and it won't complete the next object parsing since the last state was STATE_DONE -1 which then the parse method returns.

if(SPIFFS.exists("/unsent.json")) { JsonStreamingParser parser; parser.setListener(&listener); Serial.println(F("I have unsent data")); File unsentFile = SPIFFS.open("/unsent.json","r"); if(unsentFile) { Serial.printf("unsent.json size:%d",unsentFile.size()); Serial.println(""); while(unsentFile.available()) { String line = unsentFile.readStringUntil('\n'); Serial.println(line); char parse_line[200]; line.toCharArray(parse_line, 200); for (int i = 0; i < sizeof( parse_line ); i++) { parser.parse(parse_line[i]); } } } }