Comments (6)
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.
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.
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.
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.
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 String
s.
Best regards,
Benoit
from arduinojson.
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)
- Filtering large arrays of objects HOT 4
- deserializeJson from HTTP result in EmptyInput error HOT 3
- best way to create a sub document HOT 6
- Callback-based filter HOT 1
- Adding entry in JsonObject using File object causes corrupted JSON HOT 1
- Initial serializing HOT 7
- `doc[var]` doesn't work when `var` contains a string
- JsonArray.add(JsonObject) adds partial JsonObject when the JsonDocument is full HOT 6
- ArduinoJson.h: No such file or directory HOT 3
- JsonVariant tests false for both a value that doesn't exist and for a value that is zero HOT 7
- How do i create JSON array of objects using ArduinoJson library. HOT 1
- Identifying JsonVariant type of a deserialized document HOT 4
- Example code results in compiler error: no match for 'operator+=' HOT 3
- Passing JsonDocument by reference HOT 4
- In Memory Deserialization HOT 4
- v7 equivalent of validateJson HOT 2
- conversion from ‘ArduinoJson::V704HB42::detail::enable_if<true, ArduinoJson::V704HB42::JsonVariantConst>::type’ {aka ‘ArduinoJson::V704HB42::JsonVariantConst’} to ‘ArduinoJson::V704HB42::JsonDocument’ is ambiguous HOT 1
- Assertion `poolIndex < count_' failed HOT 1
- Maximum string length of 13 characters HOT 2
- V6 Documentation ArduinoJson PubSubClient with StreamUtils HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinojson.