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 usage of jackson type reference where it is not needed #5465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading