haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Float to int conversion when stringifying and parsing Json.

Open qlambert-pro opened this issue 1 year ago • 3 comments

The json resulting from pretty printing a float value that is exactly an integer doesn't include any period. As a result, when parsing the json again, the same value is interpreted as an integer.

The following example illustrate the problem, but does not involve json pretty-printing or parsing.

class Main {
    static function main() {
        var v : Float = 1.0;
        trace(v);
    }
}

It would be convenient if the pretty-printer used when stringifying json always included a float signifier.

qlambert-pro avatar Feb 01 '24 09:02 qlambert-pro

Oh yes I want to break this in Haxe 5. I hate how floats randomly are printed as ints just because the decimal happens to be 0. This is going to require changes to all target run-times, but it shouldn't be too bad.

Simn avatar Feb 01 '24 09:02 Simn

As per the linked PR, we can't fix this in the general case.

Could you provide the example that involves JSON, because that's a different case.

Simn avatar Feb 09 '24 08:02 Simn

Sorry for the delay:

typedef Data = {
        var field : Float;
}

class Main {

        static public function main() {
                var d : Data = { field : 2 };

                var s = haxe.Json.stringify(d, "\t");

                sys.io.File.saveContent("data.json", s);
        }
}

qlambert-pro avatar Feb 28 '24 18:02 qlambert-pro