Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not parse recursive #12

Open
bcatalin opened this issue Jun 10, 2017 · 1 comment
Open

Can not parse recursive #12

bcatalin opened this issue Jun 10, 2017 · 1 comment

Comments

@bcatalin
Copy link

bcatalin commented Jun 10, 2017

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
Copy link

alieslam commented Oct 18, 2017

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]); } } } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants