Skip to content

Commit

Permalink
Remove usage of Jackson TypeReference.
Browse files Browse the repository at this point in the history
Motivation:

Jackson's TypeReference is only supported by the DatabindCodec. A few methods declaring TypeReference are still available on JacksonCodec and JsonEvent, such methods should be removed because the lead to incomplete usage of JacksonCodec and will simply not work at all with potential other implementations of the JSON spi.

Changes:

Remove methods declaring TypeReference and update tests accordingly.
  • Loading branch information
vietj committed Jan 29, 2025
1 parent 1ee39ec commit 340492e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import io.netty.buffer.ByteBufInputStream;
Expand All @@ -30,8 +29,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
Expand Down Expand Up @@ -67,28 +64,16 @@ public <T> T fromString(String json, Class<T> clazz) throws DecodeException {
return fromParser(createParser(json), clazz);
}

public <T> T fromString(String str, TypeReference<T> typeRef) throws DecodeException {
return fromString(str, classTypeOf(typeRef));
}

@Override
public <T> T fromBuffer(Buffer json, Class<T> clazz) throws DecodeException {
return fromParser(createParser(json), clazz);
}

public <T> T fromBuffer(Buffer buf, TypeReference<T> typeRef) throws DecodeException {
return fromBuffer(buf, classTypeOf(typeRef));
}

@Override
public <T> T fromValue(Object json, Class<T> toValueType) {
throw new DecodeException("Mapping " + toValueType.getName() + " is not available without Jackson Databind on the classpath");
}

public <T> T fromValue(Object json, TypeReference<T> type) {
throw new DecodeException("Mapping " + type.getType().getTypeName() + " is not available without Jackson Databind on the classpath");
}

@Override
public String toString(Object object, boolean pretty) throws EncodeException {
BufferRecycler br = factory._getBufferRecycler();
Expand Down Expand Up @@ -378,17 +363,6 @@ private static void encodeNumber(JsonGenerator generator, Object json) throws IO
}
}

private static <T> Class<T> classTypeOf(TypeReference<T> typeRef) {
Type type = typeRef.getType();
if (type instanceof Class) {
return (Class<T>) type;
} else if (type instanceof ParameterizedType) {
return (Class<T>) ((ParameterizedType)type).getRawType();
} else {
throw new DecodeException();
}
}

private static <T> T cast(Object o, Class<T> clazz) {
if (o instanceof Map) {
if (!clazz.isAssignableFrom(Map.class)) {
Expand Down Expand Up @@ -449,28 +423,4 @@ private static <T> T cast(Object o, Class<T> clazz) {
return clazz.cast(o);
}
}

/**
* Decode a given JSON string to a POJO of the given type.
* @param str the JSON string.
* @param type the type to map to.
* @param <T> the generic type.
* @return an instance of T
* @throws DecodeException when there is a parsing or invalid mapping.
*/
public static <T> T decodeValue(String str, TypeReference<T> type) throws DecodeException {
return JacksonFactory.CODEC.fromString(str, type);
}

/**
* Decode a given JSON buffer to a POJO of the given class type.
* @param buf the JSON buffer.
* @param type the type to map to.
* @param <T> the generic type.
* @return an instance of T
* @throws DecodeException when there is a parsing or invalid mapping.
*/
public static <T> T decodeValue(Buffer buf, TypeReference<T> type) throws DecodeException {
return JacksonFactory.CODEC.fromBuffer(buf, type);
}
}
13 changes: 1 addition & 12 deletions vertx-core/src/main/java/io/vertx/core/parsetools/JsonEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@

package io.vertx.core.parsetools;

import com.fasterxml.jackson.core.type.TypeReference;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.time.Instant;

/**
* A JSON event emited by the {@link JsonParser}.
* A JSON event emitted by the {@link JsonParser}.
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
Expand Down Expand Up @@ -156,13 +154,4 @@ public interface JsonEvent {
*/
<T> T mapTo(Class<T> type);

/**
* Decodes and returns the current value as the specified {@code type}.
*
* @param type the type to decode the value to
* @return the decoded value
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
<T> T mapTo(TypeReference<T> type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ public <T> T mapTo(Class<T> type) {
}
}

@Override
public <T> T mapTo(TypeReference<T> type) {
try {
return JacksonFactory.CODEC.fromValue(value, type);
} catch (Exception e) {
throw new DecodeException(e.getMessage(), e);
}
}

@Override
public Integer integerValue() {
if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.jackson.DatabindCodec;
import io.vertx.core.json.jackson.JacksonCodec;
import io.vertx.test.core.TestUtils;
import io.vertx.test.core.VertxTestBase;
import org.junit.Test;
Expand Down Expand Up @@ -57,13 +56,15 @@ public void testGenericDecoding() {
String json = Json.encode(Collections.singletonList(original));
List<Pojo> correct;

correct = JacksonCodec.decodeValue(json, new TypeReference<List<Pojo>>() {
DatabindCodec databindCodec = new DatabindCodec();

correct = databindCodec.fromString(json, new TypeReference<List<Pojo>>() {
});
assertTrue(((List) correct).get(0) instanceof Pojo);
assertEquals(original.value, correct.get(0).value);

// same must apply if instead of string we use a buffer
correct = JacksonCodec.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {
correct = databindCodec.fromBuffer(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {
});
assertTrue(((List) correct).get(0) instanceof Pojo);
assertEquals(original.value, correct.get(0).value);
Expand Down
Loading

0 comments on commit 340492e

Please sign in to comment.