瀏覽代碼

fix JsonPrinter empty parent class

closes #11560
Simon Krajewski 1 年之前
父節點
當前提交
4463066539
共有 2 個文件被更改,包括 31 次插入9 次删除
  1. 8 9
      std/haxe/format/JsonPrinter.hx
  2. 23 0
      tests/unit/src/unit/issues/Issue11560.hx

+ 8 - 9
std/haxe/format/JsonPrinter.hx

@@ -165,16 +165,15 @@ class JsonPrinter {
 	function fieldsString(v:Dynamic, fields:Array<String>) {
 		addChar('{'.code);
 		var len = fields.length;
-		var last = len - 1;
-		var first = true;
+		var empty = true;
 		for (i in 0...len) {
 			var f = fields[i];
 			var value = Reflect.field(v, f);
 			if (Reflect.isFunction(value))
 				continue;
-			if (first) {
+			if (empty) {
 				nind++;
-				first = false;
+				empty = false;
 			} else
 				addChar(','.code);
 			newl();
@@ -184,11 +183,11 @@ class JsonPrinter {
 			if (pretty)
 				addChar(' '.code);
 			write(f, value);
-			if (i == last) {
-				nind--;
-				newl();
-				ipad();
-			}
+		}
+		if (!empty) {
+			nind--;
+			newl();
+			ipad();
 		}
 		addChar('}'.code);
 	}

+ 23 - 0
tests/unit/src/unit/issues/Issue11560.hx

@@ -0,0 +1,23 @@
+package unit.issues;
+
+@:keep
+private class ParentClass {
+	// no field variable, the issue does not reproduce when there is one
+	function anyFunc() {}
+
+	public function new() {}
+}
+
+@:keep
+private class ChildClass extends ParentClass {
+	var anyVar:String;
+}
+
+class Issue11560 extends Test {
+	function test() {
+		var c = new ChildClass();
+
+		var json = haxe.Json.stringify(c, "\t");
+		eq('{\n\t"anyVar": null\n}', json);
+	}
+}