Bug 13660 – JSONValue encodes floats as integers

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-10-26T10:04:18Z
Last change time
2020-03-21T03:56:34Z
Assigned to
No Owner
Creator
Tomer Filiba (weka)

Comments

Comment #0 by tomer — 2014-10-26T10:04:18Z
When JSON-encoding a float denoting a whole number, the number is encoded as an integer. Consider the following example: void main() { auto jv1 = JSONValue(4.0); auto textual = jv1.toString(); auto jv2 = parseJSON(textual); writeln(jv1.type); // FLOAT writeln(textual); // "4" writeln(jv2.type); // INTEGER } This is due to the following code in std.json.toJSON: case JSON_TYPE.FLOAT: json.put(to!string(value.store.floating)); break; to!string(4.0) returns "4", disregarding the fact it was a float. A simple fix would be to append ".0" if the string representation does not contain a dot. This currently breaks my code, where I expect a value to be a floating point number, but I have to try getting it either as an integer or a float.
Comment #1 by b2.temp — 2016-12-30T08:48:03Z
There's a pull for the most recent dup so it's more simple to close this one. *** This issue has been marked as a duplicate of issue 16432 ***