Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DatabindCodec pretty mapper #5059

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 4 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
Loading