ArduinoJSON: How to fix 1 or 0 being printed instead of true/false
arduinojson_boolean_issue.cpp
volatile bool value = true;
DynamicJsonDocument json(1024);
json["ok"] = value;
serializeJson(json, Serial);This will print {"ok": 1} instead of {"ok": true} due to value being declared volatile (it works with just bool value, it does not work with volatile bool value).
In order to force {"ok": true}, just case value to bool:
arduinojson_boolean_cast.cpp
json["ok"] = (bool)value;Full example
arduinojson_full_example.cpp
volatile bool value = true;
DynamicJsonDocument json(1024);
json["ok"] = (bool)value;
serializeJson(json, Serial);Check out similar posts by category:
Arduino, Embedded, PlatformIO
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow