瀏覽代碼

check for infinite numbers when encoding Floats to JSon

Simon Krajewski 12 年之前
父節點
當前提交
a11876cd18
共有 3 個文件被更改,包括 11 次插入7 次删除
  1. 7 6
      std/Math.hx
  2. 1 1
      std/haxe/Json.hx
  3. 3 0
      tests/unit/TestMisc.hx

+ 7 - 6
std/Math.hx

@@ -32,6 +32,7 @@ extern class Math
 		
 		
 		For example, this is the result of -1.0 / 0.0.
 		For example, this is the result of -1.0 / 0.0.
 		
 		
+		Operations with NEGATIVE_INFINITY as an operand may result in
 		Operations with NEGATIVE_INFINITY as an operand may result in
 		Operations with NEGATIVE_INFINITY as an operand may result in
 		NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
 		NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
 		see ...
 		see ...
@@ -50,8 +51,8 @@ extern class Math
 		see ...
 		see ...
 	
 	
 		If this constant is converted to an Int, e.g. through Std.int(), the
 		If this constant is converted to an Int, e.g. through Std.int(), the
-		result is unspecified.		
-	**/	
+		result is unspecified.
+	**/
 	static var POSITIVE_INFINITY(default,null) : Float;
 	static var POSITIVE_INFINITY(default,null) : Float;
 
 
 	/**
 	/**
@@ -64,7 +65,7 @@ extern class Math
 		All further operations with NaN as an operand will result in NaN.
 		All further operations with NaN as an operand will result in NaN.
 		
 		
 		If this constant is converted to an Int, e.g. through Std.int(), the
 		If this constant is converted to an Int, e.g. through Std.int(), the
-		result is unspecified.		
+		result is unspecified.
 	**/
 	**/
 	static var NaN(default, null) : Float;
 	static var NaN(default, null) : Float;
 
 
@@ -100,7 +101,7 @@ extern class Math
 		If [a] or [b] are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
 		If [a] or [b] are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
 		
 		
 		If [a] and [b] are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
 		If [a] and [b] are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
-	**/	
+	**/
 	static function max(a:Float, b:Float):Float;
 	static function max(a:Float, b:Float):Float;
 	
 	
 	/**
 	/**
@@ -118,7 +119,7 @@ extern class Math
 		The unit of [v] is radians.
 		The unit of [v] is radians.
 		
 		
 		If [v] is NaN or infinite, the result is NaN.
 		If [v] is NaN or infinite, the result is NaN.
-	**/	
+	**/
 	static function cos(v:Float):Float;
 	static function cos(v:Float):Float;
 	
 	
 	// TODO
 	// TODO
@@ -190,7 +191,7 @@ extern class Math
 		Returns the smallest Int value that is not less than [v].
 		Returns the smallest Int value that is not less than [v].
 		
 		
 		TODO: need spec
 		TODO: need spec
-	**/	
+	**/
 	static function ceil(v:Float):Int;
 	static function ceil(v:Float):Int;
 	
 	
 	/**
 	/**

+ 1 - 1
std/haxe/Json.hx

@@ -102,7 +102,7 @@ class Json {
 		case TInt:
 		case TInt:
 			add(v);
 			add(v);
 		case TFloat:
 		case TFloat:
-			add(v+1==v ? null : v);
+			add(Math.isFinite(v) ? v : 'null');
 		case TFunction:
 		case TFunction:
 			add('"<fun>"');
 			add('"<fun>"');
 		case TClass(c):
 		case TClass(c):

+ 3 - 0
tests/unit/TestMisc.hx

@@ -562,6 +562,9 @@ class TestMisc extends Test {
 
 
 		eq( haxe.Json.parse('"\\u00E9"'), "é" );
 		eq( haxe.Json.parse('"\\u00E9"'), "é" );
 
 
+		eq(haxe.Json.stringify(Math.POSITIVE_INFINITY), "null");
+		eq(haxe.Json.stringify(Math.NEGATIVE_INFINITY), "null");
+		eq(haxe.Json.stringify(Math.NaN), "null");
 	}
 	}
 
 
 	function testConstructorsOpts() {
 	function testConstructorsOpts() {