Skip to content

Commit

Permalink
Fix merge conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Lopes <pmlopes@gmail.com>
  • Loading branch information
pmlopes committed May 10, 2017
2 parents f3e0802 + d71e802 commit d9a38f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/io/vertx/core/json/EncodeException.java
Expand Up @@ -25,6 +25,10 @@ public EncodeException(String message) {
super(message);
}

public EncodeException(String message, Throwable cause) {
super(message, cause);
}

public EncodeException() {
}
}
10 changes: 9 additions & 1 deletion src/main/java/io/vertx/core/json/Json.java
Expand Up @@ -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;
Expand Down Expand Up @@ -101,8 +102,15 @@ public static String encodePrettily(Object obj) throws EncodeException {
public static <T> T decodeValue(String str, Class<T> 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> T decodeValue(String str, TypeReference<T> type) throws DecodeException {
try {
return mapper.readValue(str, type);
} catch (Exception e) {
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/io/vertx/test/core/JsonMapperTest.java
Expand Up @@ -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;
Expand All @@ -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 <a href="http://tfox.org">Tim Fox</a>
*/
Expand Down Expand Up @@ -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<Pojo> correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {});
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;
}
}

0 comments on commit d9a38f3

Please sign in to comment.