[Issue 13660] New: JSONValue encodes floats as integers

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Oct 26 03:04:18 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13660

          Issue ID: 13660
           Summary: JSONValue encodes floats as integers
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: Phobos
          Assignee: nobody at puremagic.com
          Reporter: tomerfiliba at gmail.com

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.

--


More information about the Digitalmars-d-bugs mailing list