Repository navigation
Arduino JSON breaks starting with v 3.9.0 on Arduino #350
Description
Activity
Hi,
Can you please try with this branch: https://github.com/ESP32Async/ESPAsyncWebServer/tree/issue-346 ?
I fixed 2 bugs today that are not yet merged. See #349- No, unfortunately it did not resolve the issue – GOOD NEWS is that I did find out the problem and how to solve it. Your new ESPAsyncWebserver.h checks for the ArduinoJson.h include file and if it doesn’t find it, it doesn’t compile. The solution was able to be resolved in 1 of 2 ways: 1 – Add #include <ArduinoJson.h> to your ESPAsyncWebServer.h file before ‘#if __has_include("ArduinoJson.h")’ (line 21) 2 – Move #include <ArduinoJson.h> (in main .ino program) above #include < ESPAsyncWebServer.h> - For Example: #include <WiFi.h> #include <Preferences.h> #include <ArduinoJson.h> //Must be BEFORE ESPASsyncWebServer from version 9.x and higher #include <ESPAsyncWebServer.h> I think you should add an else block to include ArduinoJson.h if not found. You code doesn’t handle automatic inclusion if it doesn’t find it before its loaded. Every instance of trying to create my: ‘StaticJsonDocument’ fails: D:\Users\Jeff\DocMove\Arduino\MyProgram.ino:1420:3: error: 'StaticJsonDocument' was not declared in this scope StaticJsonDocument<512> doc; ^~~~~~~~~~~~~~~~~~ End of the compilation attempt: Compilation error: 'StaticJsonDocument' was not declared in this scope
I think you should add an else block to include ArduinoJson.h if not found. You code doesn’t handle automatic inclusion if it doesn’t find it before its loaded.
That's more your IDE (Arduino IDE) that is not correctly working. If you use a real development environment like platformio on vscode, it works.
Arduino IDE is parsing the .ino file to build its own compilation path based on the headers it finds and tries to match that with your common libraries fodler. That's a hack they do to facilitate non-dev people's life, but that's ugly and requires to know how it works.
If you were using a real dev. env, it would correctly handle transitive dependencies with the library.json file (something that is not possible in arduino ide), and it would also build a correct compile path based on the sources and libraries in the project. Then, the macro
#if __has_include("ArduinoJson.h")would correctly work.https://www.geeksforgeeks.org/cpp/__has_include-in-cpp-17/
The __has_include is a special operator to work with preprocessor directives that allow you to check if a particular header file is available for inclusion in your program. It is a conditional compilation feature introduced in C++17 that helps you write portable code that can handle different environments or platforms.
Arduino IDE not being a real dev. environment, there are many basic rules it does not adhere to, like this one sadly.
So Option 2 is the right one for you.... You need to help your poor IDE to correctly work.
Reacted by jfandl325 and Nomake WanAlso have a look at the examples in the project, especially this one:
https://github.com/ESP32Async/ESPAsyncWebServer/blob/main/examples/Json/Json.ino
you will see that ArduinoJson.h is not even included in the ino file. And it compiles.
But compilation works with platformio because it is a correct c++ dev environment that correctly setups a build environment.
As a note, the Arduino IDE by default sets everything to C++11, which is why
__has_includedoes not work (as it was introduced in C++17). There are several ways to override that should you so choose, see this discussion: https://forum.arduino.cc/t/which-version-of-c-is-currently-supported/1285868/16In the Arduino core for ESP32 we set to
-std=gnu++2aand use__has_includeon many places. So far it works without issues. As long as your sketch includes ArduinoJson, then__has_include("ArduinoJson.h")should work as expected.Reacted by Nomake Wan
Platform
ESP32
IDE / Tooling
Arduino (IDE/CLI)
What happened?
My code was running fine, then I upgraded from 3.8.0 to 3.9.1 and all my "StaticJsonDocument<512> doc;"
would not compile any longer. I have #include <ArduinoJson.h> in my code.
I reverted back to 3.8.0 and everything works once again. I reinstalled 3.9.1 and it was broken again, then removed it and tested with 3.9.0 and had the same problem. It isn't until I put 3.8.0 back in that the issue goes away.
Stack Trace
I have the arduino nano so I have no debug option with this board.
Minimal Reproductible Example (MRE)
#include <ArduinoJson.h>
StaticJsonDocument<256> doc;
doc["modeTime"] = readModeTime;
doc["tempTarget"] = readTempTarget;
I confirm that: