diff --git a/src/main/java/io/vertx/core/json/EncodeException.java b/src/main/java/io/vertx/core/json/EncodeException.java index d399eb857a0..127019ad662 100644 --- a/src/main/java/io/vertx/core/json/EncodeException.java +++ b/src/main/java/io/vertx/core/json/EncodeException.java @@ -25,6 +25,10 @@ public EncodeException(String message) { super(message); } + public EncodeException(String message, Throwable cause) { + super(message, cause); + } + public EncodeException() { } } diff --git a/src/main/java/io/vertx/core/json/Json.java b/src/main/java/io/vertx/core/json/Json.java index 4def03169e5..c18bd109567 100644 --- a/src/main/java/io/vertx/core/json/Json.java +++ b/src/main/java/io/vertx/core/json/Json.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.module.SimpleModule; import io.netty.buffer.ByteBufInputStream; @@ -101,8 +102,15 @@ public static String encodePrettily(Object obj) throws EncodeException { public static T decodeValue(String str, Class clazz) throws DecodeException { try { return mapper.readValue(str, clazz); + } catch (Exception e) { + throw new DecodeException("Failed to decode:" + e.getMessage()); } - catch (Exception e) { + } + + public static T decodeValue(String str, TypeReference type) throws DecodeException { + try { + return mapper.readValue(str, type); + } catch (Exception e) { throw new DecodeException("Failed to decode:" + e.getMessage(), e); } } diff --git a/src/test/java/io/vertx/test/core/JsonMapperTest.java b/src/test/java/io/vertx/test/core/JsonMapperTest.java index 8d0fc588c48..ff353383096 100644 --- a/src/test/java/io/vertx/test/core/JsonMapperTest.java +++ b/src/test/java/io/vertx/test/core/JsonMapperTest.java @@ -16,6 +16,8 @@ package io.vertx.test.core; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.vertx.core.buffer.Buffer; import io.vertx.core.json.Json; @@ -25,6 +27,10 @@ import static java.time.format.DateTimeFormatter.ISO_INSTANT; +import java.util.Collections; +import java.util.List; +import java.util.Map; + /** * @author Tim Fox */ @@ -93,4 +99,26 @@ public void encodeToBuffer() { // json strings are always UTF8 assertEquals("\"Hello World!\"", json.toString("UTF-8")); } + + @Test + public void testGenericDecoding() { + Pojo original = new Pojo(); + original.value = "test"; + + String json = Json.encode(Collections.singletonList(original)); + + List correct = Json.decodeValue(json, new TypeReference>() {}); + assertTrue(((List)correct).get(0) instanceof Pojo); + assertEquals(original.value, correct.get(0).value); + + List incorrect = Json.decodeValue(json, List.class); + assertFalse(incorrect.get(0) instanceof Pojo); + assertTrue(incorrect.get(0) instanceof Map); + assertEquals(original.value, ((Map)(incorrect.get(0))).get("value")); + } + + private static class Pojo { + @JsonProperty + String value; + } }