Comments (5)
Hi @scottchiefbaker,
JsonObject
is a reference to an object in a JsonDocument
.
In other words, JsonObject
doesn't hold any data but points to the data in the JsonDocument
.
To establish this link from the JsonObject
to the JsonDocument
, you must assign the reference to something that exists in the document; otherwise, the reference will be unbound.
In your case, last_obj
is unbound because it is never assigned.
Sure, the program calls JsonObject::set()
, but it's a no-op because the reference is unbound.
One solution could be to assign last_obj
, like so:
- last_obj.set(obj);
+ last_obj = obj;
But I suspect that caching the reference is not what you are after.
Instead, you probably expect to save the content of the object pointed by obj
.
In that case, the solution is to use a JsonDocument
instead:
- static JsonObject last_obj;
+ static JsonDocument last_obj;
Best regards,
Benoit
from arduinojson.
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:
no matching function for call to 'ArduinoJson::V703L1::JsonObject::set(ArduinoJson::V703L1::JsonDocument&)'
I also tried using equals assignment and I get:
no match for 'operator=' (operand types are 'ArduinoJson::V703L1::JsonObject' and 'ArduinoJson::V703L1::JsonDocument')
I think we're on the right track, but I don't know how to get the data back into the original JsonObject
from arduinojson.
Indeed, JsonObject::set()
only accepts JsonObjectConst
, and there is no implicit conversion between JsonDocument
and JsonObjectConst
. I should probably improve that, but I never noticed it because very few people use JsonObject::set()
.
You can workaround this error by explicitly converting the JsonDocument
to a JsonObject
:
- obj.set(last_obj);
+ obj.set(last_obj.as<JsonObject>());
Alternatively, you could refactor your program so that foobar()
returns a JsonDocument
:
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 String
from foobar
and then use serialized()
to insert it in the main document.
from arduinojson.
Aha! This is exactly what I needed. I didn't realize you could put a JsonDocument
into another JsonDocument
. That really simplifies things. With this method I was able to include "cached": true
as well.
There is a minor typo in your code above with last_obj
so here is my full working code for posterity in case someone else has a similar problem:
#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.
from arduinojson.
You're welcome, @scottchiefbaker!
Thank you for using ArduinoJson.
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.