Forráskód Böngészése

use Type.getInstanceFields() for instaces of classes in JsonPrinter (fixes #6801)

Alexander Kuzmenko 7 éve
szülő
commit
a0941c4aa1

+ 1 - 0
extra/CHANGES.txt

@@ -8,6 +8,7 @@
 	all : added column to StackItem.FilePos (#6665)
 	all : added `-D warn-var-shadowing`
 	all : added haxe.Log.formatOutput (#6738)
+	all : fixed haxe.format.JsonPrinter for instances of classes to make it produce consistent result across targets (#6801)
 	js : added js.Syntax class for generating unsupported JavaScript syntax in a type-safe analyzer-friendly way
 	js : added js.Map and js.Set and js.JsIterator extern definitions (ES6)
 	hl : added hl.Format.digest, use it for native Sha1/Md5

+ 0 - 6
std/haxe/format/JsonPrinter.hx

@@ -122,11 +122,7 @@ class JsonPrinter {
 				var v : Date = v;
 				quote(v.toString());
 			} else
-				#if flash
 				classString(v);
-				#else
-				objString(v);
-				#end
 		case TEnum(_):
 			var i : Dynamic = Type.enumIndex(v);
 			add(i);
@@ -154,11 +150,9 @@ class JsonPrinter {
 		#end
 	}
 
-	#if flash
 	function classString ( v : Dynamic ) {
 		fieldsString(v,Type.getInstanceFields(Type.getClass(v)));
 	}
-	#end
 
 	inline function objString( v : Dynamic ) {
 		fieldsString(v,Reflect.fields(v));

+ 45 - 0
tests/unit/src/unit/issues/Issue6801.hx

@@ -0,0 +1,45 @@
+package unit.issues;
+
+import haxe.Json;
+import haxe.format.JsonPrinter;
+
+class Issue6801 extends unit.Test {
+#if !as3
+	function test() {
+		var o = new Child();
+        var json = haxe.format.JsonPrinter.print(o);
+        trace(json);
+
+        var expected = {c:'hello', c2:true, p:1, p2:0};
+        var actual = Json.parse(json);
+        eq(Reflect.fields(expected).length, Reflect.fields(actual).length);
+        eq(expected.c, actual.c);
+        eq(expected.c2, actual.c2);
+        eq(expected.p, actual.p);
+        eq(expected.p2, actual.p2);
+	}
+#end
+}
+
+@:keep
+private class Parent {
+    public var p:Int = 1;    
+    public function new() {}
+}
+
+@:keep
+private class Child extends Parent{
+    var c:String = 'hello';
+    
+    public var prop(get,set):Int;
+    function get_prop() return 0;
+    function set_prop(v) return v;
+
+    public var c2(default,set):Bool = true;
+    function set_c2(v) return c2 = v;
+
+    @:isVar var p2(get,set):Int = 0;
+    function get_p2() return p2;
+    function set_p2(v) return p2 = v;
+
+}