Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A ArduinoJson
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 24
    • Issues 24
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Benoît Blanchon
  • ArduinoJson
  • Issues
  • #453
Closed
Open
Issue created Feb 19, 2017 by Administrator@rootContributor

Wrong deserialisation of float values with exponential notation

Created by: 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

Assignee
Assign to
Time tracking