c++jsonparsingndjsonarduinojson

How to retrieve multiple NDJSON objects from the same file using ArduinoJson?


I am trying to use ArduinoJson to parse Google's quickdraw dataset, which contains .ndjson files with multiple objects inside. I figured how to retrieve the first of the objects in the file using the following simple code:

DeserializationError deserialization_error = ArduinoJson::deserializeJson(doc, as_cstr);
if (deserialization_error) {
    printf("deserializeJson() failed: %s\n", deserialization_error.c_str());
}

However, this only parses the first object in the ndjson file.

According to the website, I get the sense that something else should happen automatically:

NDJSON, JSON Lines
When parsing a JSON document from an input stream, ArduinoJson stops reading as soon as the document ends (e.g., at the closing brace).

This feature allows to read JSON documents one after the other; for example, it allows to read line-delimited formats like NDJSON or JSON Lines.

{"event":"add_to_cart"}
{"event":"purchase"}

Is there some way to get the byte length of the parsed object to I can continue using the cstring to parse consecutive objects? I did print out the cstring and it does contain the entirety of the ndjson file.


Solution

  • I found it.

    just call multiple times:

      DeserializationError error = deserializeJson(doc, sceneFile);
    

    or:

      deserializeJson(docline1, sceneFile);
    
      deserializeJson(docline2, sceneFile);
    
      deserializeJson(docline3, sceneFile);