I am working on in Arduino IDE 2.3.4 using ArduinoJson library 6.21.5 and might be considered a newbie in C/C++ .
To better structure my code, I am trying to encapsulate conversion of a struct to JSON in a function. However, whatever I try, it does not seem to accept a JsonObject as parameter.
Here is the relevant sample code:
#include <ArduinoJson.h>
void checkTypesAreThere() {
JsonObject jObj;
jObj["sensor_id"] = "1";
}
void convertConfigToJson1(JsonObject jObj) {
// empty for now
}
void convertConfigToJson2(JsonObject& jObj) {
// empty for now
}
void convertConfigToJson2(JsonObject* jObj) {
// empty for now
}
Trying to compile this, causes errors:
/JSON.ino:8:6: error: variable or field 'convertConfigToJson1' declared void
8 | void convertConfigToJson1(JsonObject jObj) {
/JSON.ino:8:27: error: 'JsonObject' was not declared in this scope
8 | void convertConfigToJson1(JsonObject jObj) {
/JSON.ino:12:6: error: variable or field 'convertConfigToJson2' declared void
12 | void convertConfigToJson2(JsonObject& jObj) {
/JSON.ino:12:27: error: 'JsonObject' was not declared in this scope
12 | void convertConfigToJson2(JsonObject& jObj) {
/JSON.ino:12:39: error: 'jObj' was not declared in this scope
12 | void convertConfigToJson2(JsonObject& jObj) {
/JSON.ino:16:6: error: variable or field 'convertConfigToJson3' declared void
16 | void convertConfigToJson3(JsonObject* jObj) {
/JSON.ino:16:27: error: 'JsonObject' was not declared in this scope
16 | void convertConfigToJson3(JsonObject* jObj) {
/JSON.ino:16:39: error: 'jObj' was not declared in this scope
16 | void convertConfigToJson3(JsonObject* jObj) {
Note: I had originally been using ArdunioJson v7.3 and the error messages are the very same
Thanks in advance for any help! I am utterly clueless at this point
That's not the point (and I am not sure it's generally true either - afaik the JsonDocument is a reference to either a JsonObject or JsonArray, dependent on usage). I am not getting some error at runtime because I try to create a JsonObject. Neither am I getting a compile time error that tells me I cannot create a JsonObject (as done in the checkTypesAreThere function).
The issue are the 3 different convertConfigToJson functions, where I try to pass in a JsonObject as parameter, that cause errors at compile time already
Which board are you compiling for? I do not get any error messages when compiling for an UNO with ArduinoJson 7.3.0 in Arduino IDE 2.3.4 or 1.8.19, only warnings that the function argument is unused.
The sense of the program code I posted is to allow people willing and able to help, to read relevant parts (and only those) and at the same to to reproduce (or, in this case, conclude they actually cannot reproduce) the issue. And as you can see, it totally fulfilled it's purpose. What would another 500 lines of code help? They make it only harder to spot possible syntax issues or whatever (which I have not been able to rule out due to my lack of experience with C/C++).
Anyway, in case someone stumbles into something similar at some point, I by now can add that I got it to compile (on ESP8266). In an effort of refactoring and cleaning up code, I refactored the original procedural approach to an object oriented one. While it would not compile the above functions, it would all of a sudden compile those when being functions of a class. So