Skip to content

Commit

Permalink
Remove InputStream references, added decode to buffer with type refer…
Browse files Browse the repository at this point in the history
…ence

Signed-off-by: Paulo Lopes <pmlopes@gmail.com>
  • Loading branch information
pmlopes committed May 11, 2017
1 parent 8888d0c commit 4c8dcba
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
18 changes: 12 additions & 6 deletions src/main/java/io/vertx/core/json/Json.java
Expand Up @@ -22,10 +22,10 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.vertx.core.buffer.Buffer;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.Base64;
Expand Down Expand Up @@ -74,7 +74,9 @@ public static String encode(Object obj) throws EncodeException {

public static Buffer encodeToBuffer(Object obj) throws EncodeException {
try {
return Buffer.buffer(mapper.writeValueAsBytes(obj));
Buffer buf = Buffer.buffer();
mapper.writeValue(new ByteBufOutputStream(buf.getByteBuf()), obj);
return buf;
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
}
Expand Down Expand Up @@ -104,13 +106,17 @@ public static <T> T decodeValue(String str, TypeReference<T> type) throws Decode
}
}

public static <T> T decodeValue(Buffer buf, Class<T> clazz) throws DecodeException {
return decodeValue(new ByteBufInputStream(buf.getByteBuf()), clazz);
public static <T> T decodeValue(Buffer buf, TypeReference<T> type) throws DecodeException {
try {
return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), type);
} catch (Exception e) {
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
}
}

public static <T> T decodeValue(InputStream stream, Class<T> clazz) throws DecodeException {
public static <T> T decodeValue(Buffer buf, Class<T> clazz) throws DecodeException {
try {
return mapper.readValue(stream, clazz);
return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), clazz);
} catch (Exception e) {
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/vertx/core/json/JsonArray.java
Expand Up @@ -19,7 +19,6 @@
import io.vertx.core.buffer.Buffer;
import io.vertx.core.shareddata.impl.ClusterSerializable;

import java.io.InputStream;
import java.time.Instant;
import java.util.*;
import java.util.stream.Stream;
Expand Down Expand Up @@ -664,10 +663,6 @@ private void fromBuffer(Buffer buf) {
list = Json.decodeValue(buf, List.class);
}

private void fromStream(InputStream stream) {
list = Json.decodeValue(stream, List.class);
}

private class Iter implements Iterator<Object> {

final Iterator<Object> listIter;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/vertx/core/json/JsonObject.java
Expand Up @@ -20,7 +20,6 @@
import io.vertx.core.buffer.Buffer;
import io.vertx.core.shareddata.impl.ClusterSerializable;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.*;
Expand Down Expand Up @@ -955,10 +954,6 @@ private void fromBuffer(Buffer buf) {
map = Json.decodeValue(buf, Map.class);
}

private void fromStream(InputStream stream) {
map = Json.decodeValue(stream, Map.class);
}

private class Iter implements Iterator<Map.Entry<String, Object>> {

final Iterator<Map.Entry<String, Object>> mapIter;
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/io/vertx/test/core/JsonMapperTest.java
Expand Up @@ -106,8 +106,14 @@ public void testGenericDecoding() {
original.value = "test";

String json = Json.encode(Collections.singletonList(original));
List<Pojo> correct;

List<Pojo> correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {});
correct = Json.decodeValue(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 = Json.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {});
assertTrue(((List)correct).get(0) instanceof Pojo);
assertEquals(original.value, correct.get(0).value);

Expand Down

0 comments on commit 4c8dcba

Please sign in to comment.