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
Is there a way to cache a JsonObject between function calls? #2067
Comments
Hi @scottchiefbaker,
To establish this link from the In your case, One solution could be to assign - last_obj.set(obj);
+ last_obj = obj; But I suspect that caching the reference is not what you are after. - static JsonObject last_obj;
+ static JsonDocument last_obj; Best regards, |
You are correct, I want the content of the object. When I change my static variable to a Document like you mention I now get an error:
I also tried using equals assignment and I get:
I think we're on the right track, but I don't know how to get the data back into the original |
Indeed, You can workaround this error by explicitly converting the - obj.set(last_obj);
+ obj.set(last_obj.as<JsonObject>()); Alternatively, you could refactor your program so that JsonDocument foobar() {
static JsonDocument doc;
static uint32_t last_hit = 0;
bool too_soon = (millis() - last_hit) < 5000;
if (!too_soon) {
doc["name"] = "Jason Doolis";
doc["color"] = "Red";
last_hit = millis();
}
return last_obj;
}
void loop() {
JsonDocument json;
json["test"] = foobar();
serializeJson(json, Serial);
Serial.println();
delay(2000);
} Sure, it produces one or two additional copies, but it's a thousand times more readable. Another alternative would be to return a |
Aha! This is exactly what I needed. I didn't realize you could put a There is a minor typo in your code above with #include "ArduinoJson.h"
void setup() {
Serial.begin(115200);
}
JsonDocument foobar() {
static JsonDocument doc;
static uint32_t last_hit = 0;
int32_t diff = millis() - last_hit;
bool too_soon = abs(diff) < 5000;
if (last_hit && too_soon) {
doc["cached"] = true;
} else {
doc.clear();
doc["name"] = "Jason Doolis";
doc["time"] = millis();
last_hit = millis();
}
return doc;
}
void loop() {
JsonDocument json;
json["test"] = foobar();
serializeJson(json, Serial);
Serial.println();
delay(2000);
} Thank you for your assistance on this. And thank you for an AMAZING library. ArduinoJson is a masterpiece. |
You're welcome, @scottchiefbaker! |
I have Googled, looked in closed issues, and I even hit up ChatGPT and I cannot find an answer to what I assume is a simple request.
Describe the issue
I need a way to cache a JsonObject between function calls to reuse if a request is made too quickly
Environment
Here is the environment that I'm using':
arduino-cli
v0.35.3Reproduction
Here is a small snippet that demonstrate the problem.
Expected output:
The JSON uses the previous iteration as a cache
Actual output:
When I call this function every two seconds I see this:
Clearly it's getting the correct data the first time, but when it comes back and triggers the
too_soon
it's not seeing the data from the previous iteration inlast_obj
. Bonus points if I could append"cached": true
to the object after realize we're in thetoo_soon
if statement.The text was updated successfully, but these errors were encountered: