D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13660 - JSONValue encodes floats as integers
Summary: JSONValue encodes floats as integers
Status: RESOLVED DUPLICATE of issue 16432
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-10-26 10:04 UTC by Tomer Filiba (weka)
Modified: 2020-03-21 03:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomer Filiba (weka) 2014-10-26 10:04:18 UTC
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 basile-z 2016-12-30 08:48:03 UTC
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 ***