Skip to content

Commit 1f347c9

Browse files
committed
#81 fix missing notFirst
1 parent a16c6f0 commit 1f347c9

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/main/java/com/jsoniter/output/CodegenImplObject.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ public static CodegenResult genObject(ClassInfo classInfo) {
4141
if (noIndention) {
4242
ctx.buffer('}');
4343
} else {
44-
ctx.append("if (notFirst) { stream.writeObjectEnd(); } else { stream.write('}'); }");
44+
if (notFirst == 1) { // definitely not first
45+
ctx.append("stream.writeObjectEnd();");
46+
} else if (notFirst == 2) { // // maybe not first, previous field is omitNull
47+
ctx.append("if (notFirst) { stream.writeObjectEnd(); } else { stream.write('}'); }");
48+
} else { // this is the first
49+
ctx.append("stream.write('}');");
50+
}
4551
}
4652
} else {
4753
ctx.buffer("{}");

src/test/java/com/jsoniter/output/TestObject.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,19 @@ public void test_indention_with_empty_object() {
321321
.build();
322322
assertEquals("{}", JsonStream.serialize(config, new TestObject15()));
323323
}
324+
325+
public static class TestObject16 {
326+
@JsonProperty(omitNull = false)
327+
public Integer i;
328+
}
329+
330+
public void test_missing_notFirst() {
331+
Config cfg = JsoniterSpi.getCurrentConfig().copyBuilder()
332+
.indentionStep(2)
333+
.encodingMode(EncodingMode.DYNAMIC_MODE)
334+
.build();
335+
assertEquals("{\n" +
336+
" \"i\": null\n" +
337+
"}", JsonStream.serialize(cfg, new TestObject16()));
338+
}
324339
}

0 commit comments

Comments
 (0)