Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is<T>() returns wrong value, only with bool #214

Closed
marvinroger opened this issue Jan 31, 2016 · 5 comments
Closed

is<T>() returns wrong value, only with bool #214

marvinroger opened this issue Jan 31, 2016 · 5 comments
Labels
bug v5 ArduinoJson 5

Comments

@marvinroger
Copy link
Contributor

I am using an ESP8266 (NodeMCU 1.0) with the following sketch:

#include <ArduinoJson.h>

const char* json = "{\"ota\": {\"enabled\": true}}";

void setup() {
  Serial.begin(115200);
  Serial.println();

  StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(1)> jsonBuffer;
  JsonObject& parsedJson = jsonBuffer.parseObject(json);
  if (parsedJson["ota"]["enabled"].is<bool>()) {
    Serial.println("ota.enabled is a bool");
  } else {
    Serial.println("ota.enabled is not a bool");
  }
}

void loop() {
  delay(1000);
}

This prints ota.enabled is not a bool, but it obviously is.
Note I tested with const char*, unsigned long, JsonObject& and it works well, the problem only appears with bool.

@bblanchon bblanchon added the bug label Jan 31, 2016
bblanchon added a commit that referenced this issue Jan 31, 2016
@bblanchon
Copy link
Owner

The bug is real, but the code sample is wrong.
The StaticJsonBuffer is too small to hold a copy of the input.
Remember: JsonBuffer will have to make a copy if the input is not mutable, ie if it's a const char* or a String.

Here is the fixed code:

#include <ArduinoJson.h>

char json[] = "{\"ota\": {\"enabled\": true}}";

void setup() {
  Serial.begin(115200);
  Serial.println();

  StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(1)> jsonBuffer;
  JsonObject& parsedJson = jsonBuffer.parseObject(json);
  if (parsedJson["ota"]["enabled"].is<bool>()) {
    Serial.println("ota.enabled is a bool");
  } else {
    Serial.println("ota.enabled is not a bool");
  }
}

void loop() {
  delay(1000);
}

@marvinroger
Copy link
Contributor Author

Right, my C++ background is a bit limited! But in my code I'm actually using mutable strings.

@bblanchon
Copy link
Owner

I fixed the bug, I just need to merge and make a new release

@marvinroger
Copy link
Contributor Author

Great, thanks for the quick fix.

@bblanchon
Copy link
Owner

Fix in version 5.0.8

Repository owner locked and limited conversation to collaborators Sep 21, 2018
@bblanchon bblanchon added the v5 ArduinoJson 5 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants