diff --git a/src/main/java/io/vertx/core/json/jackson/DatabindCodec.java b/src/main/java/io/vertx/core/json/jackson/DatabindCodec.java index 7b1360af53a..59cfa1f7240 100644 --- a/src/main/java/io/vertx/core/json/jackson/DatabindCodec.java +++ b/src/main/java/io/vertx/core/json/jackson/DatabindCodec.java @@ -15,8 +15,6 @@ import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.module.SimpleModule; import io.netty.buffer.ByteBufInputStream; import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.impl.BufferInternal; @@ -27,7 +25,6 @@ import java.io.IOException; import java.io.InputStream; -import java.time.Instant; import java.util.List; import java.util.Map; @@ -37,7 +34,6 @@ public class DatabindCodec extends JacksonCodec { private static final ObjectMapper mapper = new ObjectMapper(); - private static final ObjectMapper prettyMapper = new ObjectMapper(); static { initialize(); @@ -47,12 +43,8 @@ private static void initialize() { // 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); - VertxModule module = new VertxModule(); mapper.registerModule(module); - prettyMapper.registerModule(module); } /** @@ -62,13 +54,6 @@ public static ObjectMapper mapper() { return mapper; } - /** - * @return the {@link ObjectMapper} used for data binding configured for indenting output. - */ - public static ObjectMapper prettyMapper() { - return prettyMapper; - } - @Override public T fromValue(Object json, Class clazz) { T value = DatabindCodec.mapper.convertValue(json, clazz); @@ -158,8 +143,13 @@ private static T fromParser(JsonParser parser, TypeReference 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()); } @@ -168,8 +158,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()); } diff --git a/src/test/java/io/vertx/core/json/JacksonDatabindTest.java b/src/test/java/io/vertx/core/json/JacksonDatabindTest.java index 6774f4ead84..d08c11aa859 100644 --- a/src/test/java/io/vertx/core/json/JacksonDatabindTest.java +++ b/src/test/java/io/vertx/core/json/JacksonDatabindTest.java @@ -24,7 +24,10 @@ import java.io.IOException; import java.time.Instant; -import java.util.*; +import java.util.Base64; +import java.util.Collections; +import java.util.List; +import java.util.Map; import static io.vertx.core.json.impl.JsonUtil.BASE64_ENCODER; import static java.time.format.DateTimeFormatter.ISO_INSTANT; @@ -41,12 +44,6 @@ public void testGetMapper() { assertNotNull(mapper); } - @Test - public void testGetPrettyMapper() { - ObjectMapper mapper = DatabindCodec.prettyMapper(); - assertNotNull(mapper); - } - @Test public void testGenericDecoding() { Pojo original = new Pojo(); @@ -225,4 +222,19 @@ private static class Pojo { @JsonProperty byte[] bytes; } + + @Test + public void testPrettyPrinting() { + JsonObject jsonObject = new JsonObject() + .put("key1", "value1") + .put("key2", "value2") + .put("key3", "value3"); + + String compact = Json.encode(jsonObject); + String pretty = Json.encodePrettily(jsonObject); + + assertFalse(compact.equals(pretty)); + + assertEquals(jsonObject, Json.decodeValue(pretty)); + } }