www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13660] New: JSONValue encodes floats as integers

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 puremagic.com
          Reporter: tomerfiliba 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.

--
Oct 26 2014