Skip to content

Commit 3ab73e0

Browse files
authored
Merge pull request #5094 from tsegismont/no-pretty-mapper-internally
ObjectMapper configuration should apply when pretty printing
2 parents 0eeba1a + 1da42d4 commit 3ab73e0

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/main/java/io/vertx/core/json/jackson/DatabindCodec.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,18 @@ public class DatabindCodec extends JacksonCodec {
3737
private static final ObjectMapper prettyMapper = new ObjectMapper();
3838

3939
static {
40-
initialize();
40+
initialize(mapper, false);
41+
initialize(prettyMapper, true);
4142
}
4243

43-
private static void initialize() {
44+
private static void initialize(ObjectMapper om, boolean prettyPrint) {
4445
// Non-standard JSON but we allow C style comments in our JSON
45-
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
46-
47-
prettyMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
48-
prettyMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
49-
46+
om.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
47+
if (prettyPrint) {
48+
om.configure(SerializationFeature.INDENT_OUTPUT, true);
49+
}
5050
VertxModule module = new VertxModule();
51-
mapper.registerModule(module);
52-
prettyMapper.registerModule(module);
51+
om.registerModule(module);
5352
}
5453

5554
/**
@@ -157,8 +156,13 @@ private static <T> T fromParser(JsonParser parser, TypeReference<T> type) throws
157156
@Override
158157
public String toString(Object object, boolean pretty) throws EncodeException {
159158
try {
160-
ObjectMapper mapper = pretty ? DatabindCodec.prettyMapper : DatabindCodec.mapper;
161-
return mapper.writeValueAsString(object);
159+
String result;
160+
if (pretty) {
161+
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
162+
} else {
163+
result = mapper.writeValueAsString(object);
164+
}
165+
return result;
162166
} catch (Exception e) {
163167
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
164168
}
@@ -167,8 +171,13 @@ public String toString(Object object, boolean pretty) throws EncodeException {
167171
@Override
168172
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
169173
try {
170-
ObjectMapper mapper = pretty ? DatabindCodec.prettyMapper : DatabindCodec.mapper;
171-
return Buffer.buffer(mapper.writeValueAsBytes(object));
174+
byte[] result;
175+
if (pretty) {
176+
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(object);
177+
} else {
178+
result = mapper.writeValueAsBytes(object);
179+
}
180+
return Buffer.buffer(result);
172181
} catch (Exception e) {
173182
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
174183
}

src/test/java/io/vertx/core/json/JacksonDatabindTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import com.fasterxml.jackson.annotation.JsonProperty;
1515
import com.fasterxml.jackson.core.type.TypeReference;
1616
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.fasterxml.jackson.databind.SerializationConfig;
18+
import com.fasterxml.jackson.databind.SerializationFeature;
19+
import io.vertx.core.ThreadingModel;
1720
import io.vertx.core.buffer.Buffer;
1821
import io.vertx.core.json.jackson.DatabindCodec;
1922
import io.vertx.core.json.jackson.JacksonCodec;
@@ -108,4 +111,20 @@ private static class Pojo {
108111
@JsonProperty
109112
byte[] bytes;
110113
}
114+
115+
@Test
116+
public void testObjectMapperConfigAppliesToPrettyPrinting() {
117+
ObjectMapper om = DatabindCodec.mapper();
118+
SerializationConfig sc = om.getSerializationConfig();
119+
assertNotNull(sc);
120+
try {
121+
om.setConfig(sc.with(SerializationFeature.WRITE_ENUMS_USING_INDEX));
122+
ThreadingModel vt = ThreadingModel.VIRTUAL_THREAD;
123+
String expected = String.valueOf(vt.ordinal());
124+
assertEquals(expected, Json.encodePrettily(vt));
125+
assertEquals(expected, Json.encode(vt));
126+
} finally {
127+
om.setConfig(sc);
128+
}
129+
}
111130
}

0 commit comments

Comments
 (0)