Skip to content

Commit

Permalink
fix JsonPrinter empty parent class
Browse files Browse the repository at this point in the history
closes #11560
  • Loading branch information
Simn authored and kLabz committed Feb 18, 2024
1 parent 693de89 commit 30f8b1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
17 changes: 8 additions & 9 deletions std/haxe/format/JsonPrinter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/src/unit/issues/Issue11560.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 30f8b1f

Please sign in to comment.