Skip to content

Commit

Permalink
Remove DatabindCodec pretty mapper (eclipse-vertx#5059)
Browse files Browse the repository at this point in the history
* Remove DatabindCodec pretty mapper

This change relieves users from configuring two object mappers if they have customizations.

For Vert.x, it is not less efficient to use a writer with pretty printer when encoding json.

Signed-off-by: Thomas Segismont <[email protected]>

* Add Databind pretty printing test

Signed-off-by: Thomas Segismont <[email protected]>

---------

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont authored Jan 10, 2024
1 parent 4b8cac3 commit 313b271
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
33 changes: 14 additions & 19 deletions src/main/java/io/vertx/core/json/jackson/DatabindCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +25,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.List;
import java.util.Map;

Expand All @@ -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();
Expand All @@ -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);
}

/**
Expand All @@ -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> T fromValue(Object json, Class<T> clazz) {
T value = DatabindCodec.mapper.convertValue(json, clazz);
Expand Down Expand Up @@ -158,8 +143,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 @@ -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());
}
Expand Down
26 changes: 19 additions & 7 deletions src/test/java/io/vertx/core/json/JacksonDatabindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 313b271

Please sign in to comment.