Code Monkey home page Code Monkey logo

Comments (6)

chess-levin avatar chess-levin commented on May 24, 2024

Or does it make sense to use the JsonDocument object as a central container for my stations list?

So I would pass a reference to the object to my load/deserialize function, like this

void loadStations(const char *filename, JsonDocument& doc);

from arduinojson.

chess-levin avatar chess-levin commented on May 24, 2024

Hi @bblanchon!
Thanks for your comment - OMG. Got it.
This code works

  JsonArray arr = doc.as<JsonArray>();
  int count = arr.size();
  Serial.printf("arr.size()=%d \n", count);

Furthermore: Reading your FAQs (https://arduinojson.org/v7/faq/why-must-i-create-a-separate-config-object/) I understand it is not a good idea to use a `JsonDocument object as my permanent container for my stations array.

Again thanks for your support.

from arduinojson.

chess-levin avatar chess-levin commented on May 24, 2024

Hello @bblanchon!

Still got one open question. Do I have to copy the strings from the JsonDocument item["name"] and item["url"] or can I use the existing ones by using their address (like shown below)?

I'm now using these two global variables

stationInfo_t * stationInfo;  // array of structures
int countElements = -1;     // array_len

and changed my function int loadStations(const char *filename) using new to allocate memory for my array of this struct

typedef struct stationInfo_s {
  const char* name;
  const char* url;
} stationInfo_t;
int loadStations(const char *filename) {
  Serial.printf("Reading stations from: %s\n", filename); 
  
  File file = SPIFFS.open(filename);

  if(!file) {
    Serial.printf("ERROR: Failed to open: %s\n", filename);
    return countElements;
  } 

  Serial.printf("Successfully opened: %s (%d bytes)\n", filename, file.size());
  
  JsonDocument doc;

  DeserializationError error = deserializeJson(doc, file);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return countElements;
  }

  JsonArray arr = doc.as<JsonArray>();
  countElements = arr.size();
  Serial.printf("Successfully deserialized %d elements\n", countElements);

  stationInfo = new stationInfo_t[countElements];

  int i = 0;
  for (JsonObject item : doc.as<JsonArray>()) {    
    stationInfo[i].name = item["name"];
    stationInfo[i].url = item["url"];
    Serial.printf("%d\t %s\t %s\n", i, stationInfo[i].name, stationInfo[i].url);
    i++;
  }

  file.close();
  return countElements;
}

from arduinojson.

chess-levin avatar chess-levin commented on May 24, 2024

Answering my own question: Yes, I do have to duplicate the strings. I've used strndup to allocate memory and making a copy.

from arduinojson.

bblanchon avatar bblanchon commented on May 24, 2024

Hi @chess-levin,

I didn't find the time to tell you about as<JsonArray>(), but fortunately, you saw my commit for the ArduinoJson Troubleshooter.

About the configuration structure, I would avoid strdup() because it's a recipe for memory leaks.
Instead, I would use fixed-sized char arrays (as shown in JsonConfigFile.ino) or Strings.

Best regards,
Benoit

from arduinojson.

chess-levin avatar chess-levin commented on May 24, 2024

Thanks for your feedback. I've learned a lot yesterday ;-)
As I plan to read the presets just once (@ setup function) from the json-file and then hold them unchanged in memory, I think the risk of memory leaks is low.

Your library is a great piece of software. Thank you for all your work!!!

from arduinojson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.