瀏覽代碼

Properly skip the first object field that is a function when encoding object in JsonPrinter (closes #2955).

Also add tests for haxe.format.JsonPrinter/Parser so we can specify it's behaviour further.
Dan Korostelev 11 年之前
父節點
當前提交
7658b3633e
共有 4 個文件被更改,包括 107 次插入51 次删除
  1. 5 4
      std/haxe/format/JsonPrinter.hx
  2. 1 0
      tests/unit/Test.hx
  3. 101 0
      tests/unit/TestJson.hx
  4. 0 47
      tests/unit/TestMisc.hx

+ 5 - 4
std/haxe/format/JsonPrinter.hx

@@ -13,7 +13,7 @@ class JsonPrinter {
 	var indent:String;
 	var pretty:Bool;
 	var nind:Int;
-	
+
 	function new(replacer:Dynamic -> Dynamic -> Dynamic, space:String) {
 		this.replacer = replacer;
 		this.indent = space;
@@ -28,11 +28,11 @@ class JsonPrinter {
 		buf = new StringBuf();
 		#end
 	}
-	
+
 	inline function ipad ():Void {
 		if (pretty) add(StringTools.lpad('', indent, nind * indent.length));
 	}
-	
+
 	inline function newl ():Void {
 		if (pretty) addChar('\n'.code);
 	}
@@ -129,11 +129,12 @@ class JsonPrinter {
 		addChar('{'.code);
 		var len = fields.length;
 		var last = len - 1;
+		var first = true;
 		for( i in 0...len ) {
 			var f = fields[i];
 			var value = Reflect.field(v,f);
 			if( Reflect.isFunction(value) ) continue;
-			if( i > 0 ) addChar(','.code) else nind++;
+			if( first ) { nind++; first = false; } else addChar(','.code);
 			newl();
 			ipad();
 			quote(f);

+ 1 - 0
tests/unit/Test.hx

@@ -257,6 +257,7 @@ class Test #if swf_mark implements mt.Protect #end {
 			new TestEReg(),
 			new TestXML(),
 			new TestMisc(),
+			new TestJson(),
 			new TestResource(),
 			new TestInt64(),
 			new TestReflect(),

+ 101 - 0
tests/unit/TestJson.hx

@@ -0,0 +1,101 @@
+package unit;
+
+class TestJson extends Test {
+
+    function testNativeJson() {
+        var str = haxe.Json.stringify( { x : -4500, y : 1.456, a : ["hello", "wor'\"\n\t\rd"] } );
+        str = str.substr(1, str.length - 2); // remove {}
+        var parts = str.split(",");
+        t( parts.remove('"x":-4500') );
+        t( parts.remove('"y":1.456') );
+        t( parts.remove('"a":["hello"') );
+        t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
+        eq( parts.join("#"), "" );
+
+        // no support for regexps
+        #if flash8
+        return;
+        #end
+
+        function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.Json.parse(haxe.Json.stringify(v)),v);
+        function deepId(v:Dynamic) {
+            var str = haxe.Json.stringify(v);
+            eq(haxe.Json.stringify(haxe.Json.parse(str)), str);
+        }
+
+        id(true);
+        id(false);
+        id(null);
+        id(0);
+        id(145);
+        id( -145 );
+        id(0.15461);
+        id( -485.15461);
+        id( 1e10 );
+        id( -1e-10 );
+        id( "" );
+        id( "hello" );
+        id( "he\n\r\t\\\\llo");
+
+        deepId( {field: 4} );
+        deepId( { test: { nested: null }} );
+        var mix : Array<Dynamic> = [1, 2, 3, "str"];
+        deepId( {array: mix} );
+
+        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");
+    }
+
+    // TODO: test pretty-printing (also with objects with skipped function fields!)
+    function testHaxeJson() {
+        var str = haxe.format.JsonPrinter.print( { x : -4500, y : 1.456, a : ["hello", "wor'\"\n\t\rd"], b : function() {} } );
+        str = str.substr(1, str.length - 2); // remove {}
+        var parts = str.split(",");
+        t( parts.remove('"x":-4500') );
+        t( parts.remove('"y":1.456') );
+        t( parts.remove('"a":["hello"') );
+        t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
+        eq( parts.join("#"), "" );
+
+        // no support for regexps
+        #if flash8
+        return;
+        #end
+
+        function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.format.JsonParser.parse(haxe.format.JsonPrinter.print(v)),v);
+        function deepId(v:Dynamic) {
+            var str = haxe.format.JsonPrinter.print(v);
+            eq(haxe.format.JsonPrinter.print(haxe.format.JsonParser.parse(str)), str);
+        }
+
+        id(true);
+        id(false);
+        id(null);
+        id(0);
+        id(145);
+        id( -145 );
+        id(0.15461);
+        id( -485.15461);
+        id( 1e10 );
+        id( -1e-10 );
+        id( "" );
+        id( "hello" );
+        id( "he\n\r\t\\\\llo");
+
+        deepId( {field: 4} );
+        deepId( { test: { nested: null }} );
+        var mix : Array<Dynamic> = [1, 2, 3, "str"];
+        deepId( {array: mix} );
+
+        eq( haxe.format.JsonParser.parse('"\\u00E9"'), "é" );
+
+        eq(haxe.format.JsonPrinter.print(Math.POSITIVE_INFINITY), "null");
+        eq(haxe.format.JsonPrinter.print(Math.NEGATIVE_INFINITY), "null");
+        eq(haxe.format.JsonPrinter.print(Math.NaN), "null");
+        eq(haxe.format.JsonPrinter.print(function() {}), "\"<fun>\"");
+        eq(haxe.format.JsonPrinter.print({a: function() {}, b: 1}), "{\"b\":1}");
+    }
+}

+ 0 - 47
tests/unit/TestMisc.hx

@@ -533,53 +533,6 @@ class TestMisc extends Test {
 	}
 	#end
 
-	function testJSon() {
-		var str = haxe.Json.stringify( { x : -4500, y : 1.456, a : ["hello", "wor'\"\n\t\rd"] } );
-		str = str.substr(1, str.length - 2); // remove {}
-		var parts = str.split(",");
-		t( parts.remove('"x":-4500') );
-		t( parts.remove('"y":1.456') );
-		t( parts.remove('"a":["hello"') );
-		t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
-		eq( parts.join("#"), "" );
-
-		// no support for regexps
-		#if flash8
-		return;
-		#end
-
-		function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.Json.parse(haxe.Json.stringify(v)),v);
-		function deepId(v:Dynamic) {
-			var str = haxe.Json.stringify(v);
-			eq(haxe.Json.stringify(haxe.Json.parse(str)), str);
-		}
-
-		id(true);
-		id(false);
-		id(null);
-		id(0);
-		id(145);
-		id( -145 );
-		id(0.15461);
-		id( -485.15461);
-		id( 1e10 );
-		id( -1e-10 );
-		id( "" );
-		id( "hello" );
-		id( "he\n\r\t\\\\llo");
-
-		deepId( {field: 4} );
-		deepId( { test: { nested: null }} );
-		var mix : Array<Dynamic> = [1, 2, 3, "str"];
-		deepId( {array: mix} );
-
-		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() {
 		var b = new BaseConstrOpt();
 		eq(b.s, "test");