HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.14k stars 653 forks source link

Float to int conversion when stringifying and parsing Json. #11532

Open qlambert-pro opened 7 months ago

qlambert-pro commented 7 months ago

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.

Simn commented 7 months ago

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 commented 7 months ago

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.

qlambert-pro commented 6 months ago

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);
        }
}