Skip to content

Commit 4c88099

Browse files
committed
#81 support indention in dynamic codegen, part 7: enum
1 parent 1f920d8 commit 4c88099

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,21 @@ public static String getTypeName(Type fieldType) {
318318
}
319319
}
320320
public static CodegenResult genEnum(Class clazz) {
321+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
321322
CodegenResult ctx = new CodegenResult();
322323
ctx.append(String.format("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {", clazz.getCanonicalName()));
323324
ctx.append("if (obj == null) { stream.writeNull(); return; }");
324-
ctx.buffer('"');
325+
if (noIndention) {
326+
ctx.buffer('"');
327+
} else {
328+
ctx.append("stream.write('\"');");
329+
}
325330
ctx.append("stream.writeRaw(obj.toString());");
326-
ctx.buffer('"');
331+
if (noIndention) {
332+
ctx.buffer('"');
333+
} else {
334+
ctx.append("stream.write('\"');");
335+
}
327336
ctx.append("}");
328337
return ctx;
329338
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public void test_enum() throws IOException {
9696
stream.writeVal(obj);
9797
stream.close();
9898
assertEquals("{'field1':'HELLO'}".replace('\'', '"'), baos.toString());
99+
Config cfg = new Config.Builder()
100+
.encodingMode(EncodingMode.DYNAMIC_MODE)
101+
.indentionStep(2)
102+
.build();
103+
assertEquals("{\n" +
104+
" \"field1\": \"HELLO\"\n" +
105+
"}", JsonStream.serialize(cfg, obj));
99106
}
100107

101108
public static class TestObject6 {

0 commit comments

Comments
 (0)