ArduinoJSON: How to fix 1 or 0 being printed instead of true/false
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
:
json["ok"] = (bool)value;
Full example
volatile bool value = true;
DynamicJsonDocument json(1024);
json["ok"] = (bool)value;
serializeJson(json, Serial);