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

Using default value problem (x=json["Test"] | x;) #675

Closed
ttlappalainen opened this issue Feb 5, 2018 · 6 comments
Closed

Using default value problem (x=json["Test"] | x;) #675

ttlappalainen opened this issue Feb 5, 2018 · 6 comments
Labels
bug v5 ArduinoJson 5

Comments

@ttlappalainen
Copy link

I have problem with using default value. I have on json: "Test": 1

  const char *strTest;
  double v=5;
  v=jsonProperty[strTest] | v;

Works fine and returns 1, but

  uint8_t v=5;
  v=jsonProperty[strTest] | v;

returns 5 allways. Last works fine without | v

@bblanchon
Copy link
Owner

Hi @ttlappalainen,

I cannot reproduce the bug.
See demo: https://wandbox.org/permlink/Q0FLElI8oJ9nuThD

Please provide an MCVE.

Regards,
Benoit

@ttlappalainen
Copy link
Author

Now I am not sure is this bug or feature. With below code first returns wrong and second right. Also I expect that the reason is that you when I am using existing data, it has internally double object.

#include <Arduino.h>
#include <ArduinoJson.h>

void setup() {
  Serial.begin(115200);
  delay(300);

  Serial.println("Startup");

  DynamicJsonBuffer jsonBuffer(1000);
  jsonBuffer.clear();
  JsonObject& jsonControl=jsonBuffer.createObject();
  double a=100;
  jsonControl["Test"]=a;

  jsonControl.prettyPrintTo(Serial);

  uint8_t v=jsonControl["Test"] | 5;
  
  Serial.println();
  Serial.print("Read value:"); Serial.println(v);

  char buf[1000];
  jsonControl.prettyPrintTo(buf,1000);
  DynamicJsonBuffer jsonBuffer2(1000);
  JsonObject& jsonControl2=jsonBuffer2.parseObject(buf);
  v=jsonControl2["Test"] | v;
  Serial.println();
  Serial.print("Read value:"); Serial.println(v);
}

void loop() {
}

@bblanchon
Copy link
Owner

DynamicJsonBuffer jsonBuffer(1000);
char buf[1000];
DynamicJsonBuffer jsonBuffer2(1000);

Are you sure that there is enough RAM on your micro-controller?

@ttlappalainen
Copy link
Author

Teensy 3.5 RAM 196 kB. Totally with flash more than on my first computer:)

@bblanchon
Copy link
Owner

OK, now I see it.

jsonControl["Test"] is a double so when you try to extract a uint8_t, it returns the default value.

To me, it's a bug because it's not consistent with:

uint8_t v = jsonControl2["Test"]; // return 100 as expected

@bblanchon bblanchon added the bug label Feb 8, 2018
@ttlappalainen
Copy link
Author

OK - thanks. Meanwhile I can go around problem with
jsonControl["Test"]=(uint8_t)a;
where it is possible.

bblanchon added a commit that referenced this issue Feb 9, 2018
bblanchon added a commit that referenced this issue Feb 9, 2018
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