Skip to content

Commit 563912d

Browse files
committed
ObjectMapper configuration should apply when pretty printing
Since the pretty mapper is deprecated, any customization of the object mapper should be used when pretty printing with Json.encodePrettily. And then it is no longer necessary to create the pretty mapper config by default, so we can lazily create it. Signed-off-by: Thomas Segismont <[email protected]>
1 parent 0eeba1a commit 563912d

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,28 @@
2727
import java.io.InputStream;
2828
import java.util.List;
2929
import java.util.Map;
30+
import java.util.concurrent.atomic.AtomicReference;
3031

3132
/**
3233
* @author <a href="mailto:[email protected]">Julien Viet</a>
3334
*/
3435
public class DatabindCodec extends JacksonCodec {
3536

3637
private static final ObjectMapper mapper = new ObjectMapper();
37-
private static final ObjectMapper prettyMapper = new ObjectMapper();
38+
private static final AtomicReference<ObjectMapper> prettyMapper = new AtomicReference<>();
3839

3940
static {
40-
initialize();
41+
initialize(mapper, false);
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
/**
@@ -65,7 +64,13 @@ public static ObjectMapper mapper() {
6564
*/
6665
@Deprecated
6766
public static ObjectMapper prettyMapper() {
68-
return prettyMapper;
67+
ObjectMapper pm = prettyMapper.get();
68+
if (pm != null) {
69+
return pm;
70+
}
71+
pm = new ObjectMapper();
72+
initialize(pm, true);
73+
return prettyMapper.compareAndSet(null, pm) ? pm : prettyMapper.get();
6974
}
7075

7176
@Override
@@ -157,8 +162,13 @@ private static <T> T fromParser(JsonParser parser, TypeReference<T> type) throws
157162
@Override
158163
public String toString(Object object, boolean pretty) throws EncodeException {
159164
try {
160-
ObjectMapper mapper = pretty ? DatabindCodec.prettyMapper : DatabindCodec.mapper;
161-
return mapper.writeValueAsString(object);
165+
String result;
166+
if (pretty) {
167+
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
168+
} else {
169+
result = mapper.writeValueAsString(object);
170+
}
171+
return result;
162172
} catch (Exception e) {
163173
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
164174
}
@@ -167,8 +177,13 @@ public String toString(Object object, boolean pretty) throws EncodeException {
167177
@Override
168178
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
169179
try {
170-
ObjectMapper mapper = pretty ? DatabindCodec.prettyMapper : DatabindCodec.mapper;
171-
return Buffer.buffer(mapper.writeValueAsBytes(object));
180+
byte[] result;
181+
if (pretty) {
182+
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(object);
183+
} else {
184+
result = mapper.writeValueAsBytes(object);
185+
}
186+
return Buffer.buffer(result);
172187
} catch (Exception e) {
173188
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
174189
}

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

Lines changed: 17 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,18 @@ 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+
assertEquals(Json.encode(vt), Json.encodePrettily(vt));
124+
} finally {
125+
om.setConfig(sc);
126+
}
127+
}
111128
}

0 commit comments

Comments
 (0)