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

volatile float serialized as int #1557

Closed
stormtroober opened this issue May 13, 2021 · 2 comments
Closed

volatile float serialized as int #1557

stormtroober opened this issue May 13, 2021 · 2 comments
Labels
bug v6 ArduinoJson 6
Milestone

Comments

@stormtroober
Copy link

stormtroober commented May 13, 2021

With the last update of the library, i get floats variable serialized as ints variable.
I'm using a ESP32.

This is the code you need:

volatile float distance;
volatile int state;

StaticJsonDocument<128> doc;

   doc["type"] = "data";
   doc["state"] = state;
   Serial.println(distance);
   doc["distance"] = distance;
   serializeJson(doc, msg);
   doc.clear();
   Serial.println(msg);

From the prints i get:
5.74
{"type":"data","state":2,"distance":5}

I don't get this issue using a non volatile variable.
Bye

@bblanchon bblanchon changed the title Float Serialized as Int volatile float serialized as int May 14, 2021
@bblanchon bblanchon added the bug label May 14, 2021
@bblanchon
Copy link
Owner

Hi @aleshark87,

Thank you very much for reporting this issue 👍
I'll fix the bug in branch 6.x very soon.

As a workaround, you can cast to float:

doc["distance"] = (float)distance;

Best regards,
Benoit

@bblanchon bblanchon added this to the v6.18.1 milestone May 25, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 25, 2021
@bblanchon
Copy link
Owner

The fix is included in ArduinoJson 6.18.1

@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants