8000 Wrong deserialisation of float values with exponential notation · Issue #453 · bblanchon/ArduinoJson · GitHub
[go: up one dir, main page]

Skip to content
Wrong deserialisation of float values with exponential notation #453
@moodgorning

Description

@moodgorning

Hi again,
I'm experiencing trouble deserialising floating point values. If I am serialising values bigger than 1000 they get encoded in the "1.0e3" notation, but if I read them from the serialised json, it ignores the exponent and reads it as 1.0.
See this example code, 11000.13465 gets serialised to 1.100013e4 but read back as 1.100013:

#include <ArduinoJson.h>

void setup()
{
}

float value = 11000.13465;

void readJson(String json){
	const size_t BUFFER_SIZE = JSON_OBJECT_SIZE(1) + 190;
    StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
    JsonObject& root = jsonBuffer.parseObject(json);

    if (!root.success())
    {
      Serial.println("parseObject() failed ");
      Serial.println(jsonBuffer.size());
      Serial.println(root.success());
      return;
    }
    if(root.containsKey("value")){
      value = root["value"].as<float>();
    }else{
      Serial.println("this should never happen");
    }
    Serial.println("value: " + String(value));
}

String serializeValues(){
    const size_t bufferSize =  JSON_OBJECT_SIZE(1) + 190;
    StaticJsonBuffer<bufferSize> jsonBuffer;
    JsonObject& root = jsonBuffer.createObject();
    root["value"] = double_with_n_digits(value,6);
    String json;
    root.printTo(json);
    Serial.println(json);
   return json;
}

void loop()
{
	readJson(serializeValues());
	delay(2000);
}

Program output:

{"value":1.100013e4}
value: 1.10

Expected would have been
value: 11000.13
It doesn't matter whether I use as() or not. I'm running this on an esp8266-12F using platformIo and the arduino esp8266 SDK
Any help is much appreciated
Cheers
Max

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv5ArduinoJson 5

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0