haxe
haxe copied to clipboard
Float to int conversion when stringifying and parsing Json.
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.
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.
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.
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);
}
}