Skip to content

Commit

Permalink
ObjectMapper configuration should apply when pretty printing
Browse files Browse the repository at this point in the history
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 <tsegismont@gmail.com>
  • Loading branch information
tsegismont committed Feb 5, 2024
1 parent 0eeba1a commit 563912d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/main/java/io/vertx/core/json/jackson/DatabindCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,28 @@
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class DatabindCodec extends JacksonCodec {

private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper prettyMapper = new ObjectMapper();
private static final AtomicReference<ObjectMapper> prettyMapper = new AtomicReference<>();

static {
initialize();
initialize(mapper, false);
}

private static void initialize() {
private static void initialize(ObjectMapper om, boolean prettyPrint) {
// Non-standard JSON but we allow C style comments in our JSON
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);

prettyMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
prettyMapper.configure(SerializationFeature.INDENT_OUTPUT, true);

om.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
if (prettyPrint) {
om.configure(SerializationFeature.INDENT_OUTPUT, true);
}
VertxModule module = new VertxModule();
mapper.registerModule(module);
prettyMapper.registerModule(module);
om.registerModule(module);
}

/**
Expand All @@ -65,7 +64,13 @@ public static ObjectMapper mapper() {
*/
@Deprecated
public static ObjectMapper prettyMapper() {
return prettyMapper;
ObjectMapper pm = prettyMapper.get();
if (pm != null) {
return pm;
}
pm = new ObjectMapper();
initialize(pm, true);
return prettyMapper.compareAndSet(null, pm) ? pm : prettyMapper.get();
}

@Override
Expand Down Expand Up @@ -157,8 +162,13 @@ private static <T> T fromParser(JsonParser parser, TypeReference<T> type) throws
@Override
public String toString(Object object, boolean pretty) throws EncodeException {
try {
ObjectMapper mapper = pretty ? DatabindCodec.prettyMapper : DatabindCodec.mapper;
return mapper.writeValueAsString(object);
String result;
if (pretty) {
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
} else {
result = mapper.writeValueAsString(object);
}
return result;
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
}
Expand All @@ -167,8 +177,13 @@ public String toString(Object object, boolean pretty) throws EncodeException {
@Override
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
try {
ObjectMapper mapper = pretty ? DatabindCodec.prettyMapper : DatabindCodec.mapper;
return Buffer.buffer(mapper.writeValueAsBytes(object));
byte[] result;
if (pretty) {
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(object);
} else {
result = mapper.writeValueAsBytes(object);
}
return Buffer.buffer(result);
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/vertx/core/json/JacksonDatabindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.vertx.core.ThreadingModel;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.jackson.DatabindCodec;
import io.vertx.core.json.jackson.JacksonCodec;
Expand Down Expand Up @@ -108,4 +111,18 @@ private static class Pojo {
@JsonProperty
byte[] bytes;
}

@Test
public void testObjectMapperConfigAppliesToPrettyPrinting() {
ObjectMapper om = DatabindCodec.mapper();
SerializationConfig sc = om.getSerializationConfig();
assertNotNull(sc);
try {
om.setConfig(sc.with(SerializationFeature.WRITE_ENUMS_USING_INDEX));
ThreadingModel vt = ThreadingModel.VIRTUAL_THREAD;
assertEquals(Json.encode(vt), Json.encodePrettily(vt));
} finally {
om.setConfig(sc);
}
}
}

0 comments on commit 563912d

Please sign in to comment.