Skip to content

Commit 7d8caf4

Browse files
committed
#81 #84 support indention in dynamic codegen, part 2: empty object
1 parent 9a5d983 commit 7d8caf4

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static CodegenResult genObject(ClassInfo classInfo) {
4141
if (supportBuffer) {
4242
ctx.buffer('}');
4343
} else {
44-
ctx.append("stream.writeObjectEnd();");
44+
ctx.append("if (notFirst) { stream.writeObjectEnd(); } else { stream.write('}'); }");
4545
}
4646
} else {
4747
ctx.buffer("{}");
@@ -139,10 +139,13 @@ private static int appendComma(CodegenResult ctx, int notFirst) {
139139
if (supportBuffer) {
140140
ctx.append("if (notFirst) { stream.write(','); } else { notFirst = true; }");
141141
} else {
142-
ctx.append("if (notFirst) { stream.writeMore(); } else { notFirst = true; }");
142+
ctx.append("if (notFirst) { stream.writeMore(); } else { stream.writeIndention(); notFirst = true; }");
143143
}
144144
} else { // this is the first, do not write comma
145145
notFirst = 1;
146+
if (!supportBuffer) {
147+
ctx.append("stream.writeIndention();");
148+
}
146149
}
147150
return notFirst;
148151
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class JsonStream extends OutputStream {
1111

1212
public Config configCache;
13-
private int indention = 0;
13+
int indention = 0;
1414
private OutputStream out;
1515
byte buf[];
1616
int count;
@@ -294,7 +294,7 @@ public final void writeMore() throws IOException {
294294
writeIndention();
295295
}
296296

297-
private void writeIndention() throws IOException {
297+
public void writeIndention() throws IOException {
298298
writeIndention(0);
299299
}
300300

@@ -321,7 +321,6 @@ public final void writeObjectStart() throws IOException {
321321
int indentionStep = currentConfig().indentionStep();
322322
indention += indentionStep;
323323
write('{');
324-
writeIndention();
325324
}
326325

327326
public final void writeObjectField(String field) throws IOException {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static JsonStream borrowJsonStream() {
2727

2828
public static void returnJsonStream(JsonStream jsonStream) {
2929
jsonStream.configCache = null;
30+
jsonStream.indention = 0;
3031
if (slot1.get() == null) {
3132
slot1.set(jsonStream);
3233
return;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void encode(Object obj, JsonStream stream) throws IOException {
4040
if (notFirst) {
4141
stream.writeMore();
4242
} else {
43+
stream.writeIndention();
4344
notFirst = true;
4445
}
4546
if (mapKeyEncoder == null) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,19 @@ private void enocde_(Object obj, JsonStream stream) throws Exception {
9595
unwrapper.method.invoke(obj, stream);
9696
}
9797
}
98-
stream.writeObjectEnd();
98+
if (notFirst) {
99+
stream.writeObjectEnd();
100+
} else {
101+
stream.write('}');
102+
}
99103
}
100104

101105
private boolean writeEncodeTo(JsonStream stream, boolean notFirst, EncodeTo encodeTo, Object val) throws IOException {
102106
if (!(encodeTo.binding.shouldOmitNull && val == null)) {
103107
if (notFirst) {
104108
stream.writeMore();
105109
} else {
110+
stream.writeIndention();
106111
notFirst = true;
107112
}
108113
stream.writeObjectField(encodeTo.toName);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,22 @@ public void test_indention() {
303303
" \"field3\": null\n" +
304304
"}", output);
305305
}
306+
307+
public static class TestObject15 {
308+
public Integer i1;
309+
public Integer i2;
310+
}
311+
312+
public void test_indention_with_empty_object() {
313+
Config config = JsoniterSpi.getCurrentConfig().copyBuilder()
314+
.indentionStep(2)
315+
.encodingMode(EncodingMode.REFLECTION_MODE)
316+
.build();
317+
assertEquals("{}", JsonStream.serialize(config, new TestObject15()));
318+
config = JsoniterSpi.getCurrentConfig().copyBuilder()
319+
.indentionStep(2)
320+
.encodingMode(EncodingMode.DYNAMIC_MODE)
321+
.build();
322+
assertEquals("{}", JsonStream.serialize(config, new TestObject15()));
323+
}
306324
}

0 commit comments

Comments
 (0)