From a4ec87bdb80e9fd2905189372d5def93dbf3f4b5 Mon Sep 17 00:00:00 2001 From: amannocci Date: Mon, 8 May 2017 16:38:19 +0100 Subject: [PATCH 1/8] Faster json parsing #1975 Signed-off-by: amannocci --- .../java/io/vertx/core/buffer/impl/BufferImpl.java | 7 +++++-- src/main/java/io/vertx/core/json/Json.java | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java index 5cb1fbcc32a..8cdf2e458e3 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java @@ -21,12 +21,15 @@ import io.netty.util.CharsetUtil; import io.vertx.core.buffer.Buffer; import io.vertx.core.impl.Arguments; +import io.vertx.core.json.Json; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -79,12 +82,12 @@ public String toString(Charset enc) { @Override public JsonObject toJsonObject() { - return new JsonObject(toString()); + return new JsonObject(Json.decodeValue(this, Map.class)); } @Override public JsonArray toJsonArray() { - return new JsonArray(toString()); + return new JsonArray(Json.decodeValue(this, List.class)); } public byte getByte(int pos) { diff --git a/src/main/java/io/vertx/core/json/Json.java b/src/main/java/io/vertx/core/json/Json.java index 6335f1eefdf..7f5e4a11223 100644 --- a/src/main/java/io/vertx/core/json/Json.java +++ b/src/main/java/io/vertx/core/json/Json.java @@ -21,6 +21,8 @@ import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ByteArraySerializer; +import io.netty.buffer.ByteBufInputStream; +import io.vertx.core.buffer.Buffer; import java.io.IOException; import java.math.BigDecimal; @@ -86,6 +88,14 @@ public static T decodeValue(String str, Class clazz) throws DecodeExcepti } } + public static T decodeValue(Buffer buf, Class clazz) throws DecodeException { + try { + return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), clazz); + } catch (Exception e) { + throw new DecodeException("Failed to decode:" + e.getMessage(), e); + } + } + @SuppressWarnings("unchecked") static Object checkAndCopy(Object val, boolean copy) { if (val == null) { From 75487feb5784e46dfd8f22dc6573562ef34c2cbf Mon Sep 17 00:00:00 2001 From: amannocci Date: Mon, 8 May 2017 18:00:15 +0100 Subject: [PATCH 2/8] Add convenient ctor #1975 Signed-off-by: amannocci --- .../io/vertx/core/buffer/impl/BufferImpl.java | 7 ++--- src/main/java/io/vertx/core/json/Json.java | 7 ++++- .../java/io/vertx/core/json/JsonArray.java | 27 +++++++++++++++++++ .../java/io/vertx/core/json/JsonObject.java | 27 +++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java index 8cdf2e458e3..f91c5bf3367 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java @@ -21,15 +21,12 @@ import io.netty.util.CharsetUtil; import io.vertx.core.buffer.Buffer; import io.vertx.core.impl.Arguments; -import io.vertx.core.json.Json; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; import java.util.Objects; /** @@ -82,12 +79,12 @@ public String toString(Charset enc) { @Override public JsonObject toJsonObject() { - return new JsonObject(Json.decodeValue(this, Map.class)); + return new JsonObject(this); } @Override public JsonArray toJsonArray() { - return new JsonArray(Json.decodeValue(this, List.class)); + return new JsonArray(this); } public byte getByte(int pos) { diff --git a/src/main/java/io/vertx/core/json/Json.java b/src/main/java/io/vertx/core/json/Json.java index 7f5e4a11223..bc4b9bc9f93 100644 --- a/src/main/java/io/vertx/core/json/Json.java +++ b/src/main/java/io/vertx/core/json/Json.java @@ -25,6 +25,7 @@ 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; @@ -89,8 +90,12 @@ public static T decodeValue(String str, Class clazz) throws DecodeExcepti } public static T decodeValue(Buffer buf, Class clazz) throws DecodeException { + return decodeValue(new ByteBufInputStream(buf.getByteBuf()), clazz); + } + + public static T decodeValue(InputStream stream, Class clazz) throws DecodeException { try { - return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), clazz); + return mapper.readValue(stream, clazz); } catch (Exception e) { throw new DecodeException("Failed to decode:" + e.getMessage(), e); } diff --git a/src/main/java/io/vertx/core/json/JsonArray.java b/src/main/java/io/vertx/core/json/JsonArray.java index 35118b0ea4a..ab4f0ed67dc 100644 --- a/src/main/java/io/vertx/core/json/JsonArray.java +++ b/src/main/java/io/vertx/core/json/JsonArray.java @@ -19,6 +19,7 @@ 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; @@ -67,6 +68,24 @@ public JsonArray(List list) { this.list = list; } + /** + * Create an instance from a Buffer of JSON. + * + * @param buf the buffer of JSON. + */ + public JsonArray(Buffer buf) { + fromBuffer(buf); + } + + /** + * Create an instance from a stream of JSON. + * + * @param stream the stream of JSON. + */ + public JsonArray(InputStream stream) { + fromStream(stream); + } + /** * Get the String at position {@code pos} in the array, * @@ -641,6 +660,14 @@ private void fromJson(String json) { list = Json.decodeValue(json, List.class); } + 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 { final Iterator listIter; diff --git a/src/main/java/io/vertx/core/json/JsonObject.java b/src/main/java/io/vertx/core/json/JsonObject.java index 6c19c151f8a..370bc0dc12f 100644 --- a/src/main/java/io/vertx/core/json/JsonObject.java +++ b/src/main/java/io/vertx/core/json/JsonObject.java @@ -20,6 +20,7 @@ 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.*; @@ -69,6 +70,24 @@ public JsonObject(Map map) { this.map = map; } + /** + * Create an instance from a buffer. + * + * @param buf the buffer to create the instance from. + */ + public JsonObject(Buffer buf) { + fromBuffer(buf); + } + + /** + * Create an instance from a stream. + * + * @param stream the stream to create the instance from. + */ + public JsonObject(InputStream stream) { + fromStream(stream); + } + /** * Create a JsonObject from the fields of a Java object. * Faster than calling `new JsonObject(Json.encode(obj))`. @@ -932,6 +951,14 @@ private void fromJson(String json) { map = Json.decodeValue(json, Map.class); } + 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> { final Iterator> mapIter; From 0ae9aa6459ecb722025e5d73c20eb5ca08908bd2 Mon Sep 17 00:00:00 2001 From: amannocci Date: Mon, 8 May 2017 21:06:04 +0100 Subject: [PATCH 3/8] Remove InputStream constructor relate #1975 Signed-off-by: amannocci --- src/main/java/io/vertx/core/json/JsonArray.java | 9 --------- src/main/java/io/vertx/core/json/JsonObject.java | 9 --------- src/test/java/io/vertx/test/core/JsonArrayTest.java | 9 +++++++++ .../java/io/vertx/test/core/JsonObjectTest.java | 13 +++++++++++++ 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/vertx/core/json/JsonArray.java b/src/main/java/io/vertx/core/json/JsonArray.java index ab4f0ed67dc..cb34f9289ef 100644 --- a/src/main/java/io/vertx/core/json/JsonArray.java +++ b/src/main/java/io/vertx/core/json/JsonArray.java @@ -77,15 +77,6 @@ public JsonArray(Buffer buf) { fromBuffer(buf); } - /** - * Create an instance from a stream of JSON. - * - * @param stream the stream of JSON. - */ - public JsonArray(InputStream stream) { - fromStream(stream); - } - /** * Get the String at position {@code pos} in the array, * diff --git a/src/main/java/io/vertx/core/json/JsonObject.java b/src/main/java/io/vertx/core/json/JsonObject.java index 370bc0dc12f..bb90f314910 100644 --- a/src/main/java/io/vertx/core/json/JsonObject.java +++ b/src/main/java/io/vertx/core/json/JsonObject.java @@ -79,15 +79,6 @@ public JsonObject(Buffer buf) { fromBuffer(buf); } - /** - * Create an instance from a stream. - * - * @param stream the stream to create the instance from. - */ - public JsonObject(InputStream stream) { - fromStream(stream); - } - /** * Create a JsonObject from the fields of a Java object. * Faster than calling `new JsonObject(Json.encode(obj))`. diff --git a/src/test/java/io/vertx/test/core/JsonArrayTest.java b/src/test/java/io/vertx/test/core/JsonArrayTest.java index 496f540da5f..279bcd5b4b1 100644 --- a/src/test/java/io/vertx/test/core/JsonArrayTest.java +++ b/src/test/java/io/vertx/test/core/JsonArrayTest.java @@ -1027,6 +1027,15 @@ public void testCreateFromListNestedList() { assertSame(list2, arr2.getList()); } + @Test + public void testCreateFromBuffer() { + JsonArray excepted = new JsonArray(); + excepted.add("foobar"); + excepted.add(123); + Buffer buf = Buffer.buffer(excepted.encode()); + assertEquals(excepted, new JsonArray(buf)); + } + @Test public void testClusterSerializable() { jsonArray.add("foo").add(123); diff --git a/src/test/java/io/vertx/test/core/JsonObjectTest.java b/src/test/java/io/vertx/test/core/JsonObjectTest.java index b72ac8de1a3..d018fdcca82 100644 --- a/src/test/java/io/vertx/test/core/JsonObjectTest.java +++ b/src/test/java/io/vertx/test/core/JsonObjectTest.java @@ -24,6 +24,10 @@ import org.junit.Before; import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.StringBufferInputStream; +import java.io.StringReader; import java.math.BigDecimal; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -1509,6 +1513,15 @@ public void testCreateFromMap() { assertSame(map, obj.getMap()); } + @Test + public void testCreateFromBuffer() { + JsonObject excepted = new JsonObject(); + excepted.put("foo", "bar"); + excepted.put("quux", 123); + Buffer buf = Buffer.buffer(excepted.encode()); + assertEquals(excepted, new JsonObject(buf)); + } + @Test public void testCreateFromMapCharSequence() { Map map = new HashMap<>(); From 147b3cd98f240b4d0732cdc2f17b157363cb150c Mon Sep 17 00:00:00 2001 From: amannocci Date: Mon, 8 May 2017 21:07:54 +0100 Subject: [PATCH 4/8] Remove unused imports in test #1975 Signed-off-by: amannocci --- src/test/java/io/vertx/test/core/JsonObjectTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/test/java/io/vertx/test/core/JsonObjectTest.java b/src/test/java/io/vertx/test/core/JsonObjectTest.java index d018fdcca82..a9662d36e25 100644 --- a/src/test/java/io/vertx/test/core/JsonObjectTest.java +++ b/src/test/java/io/vertx/test/core/JsonObjectTest.java @@ -24,10 +24,6 @@ import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.StringBufferInputStream; -import java.io.StringReader; import java.math.BigDecimal; import java.time.Instant; import java.time.temporal.ChronoUnit; From f3e08021e5dfe6ec55e27237d815a7f944ef52ff Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Wed, 10 May 2017 10:18:10 +0200 Subject: [PATCH 5/8] Add encode to buffer Signed-off-by: Paulo Lopes --- src/main/asciidoc/java/http.adoc | 2 +- src/main/java/io/vertx/core/json/Json.java | 19 ++++++++++++++ .../java/io/vertx/core/json/JsonArray.java | 9 +++++++ .../java/io/vertx/core/json/JsonObject.java | 13 ++++++++-- .../io/vertx/test/core/JsonArrayTest.java | 21 +++++++++++++++- .../io/vertx/test/core/JsonMapperTest.java | 9 +++++++ .../io/vertx/test/core/JsonObjectTest.java | 25 +++++++++++++++++++ 7 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/main/asciidoc/java/http.adoc b/src/main/asciidoc/java/http.adoc index ff970183ee5..2bc8ba950d4 100644 --- a/src/main/asciidoc/java/http.adoc +++ b/src/main/asciidoc/java/http.adoc @@ -1993,7 +1993,7 @@ also overrides the default client setting. ==== Server Name Indication (SNI) -Vert.x http servers can be configured to use SNI in exactly the same way as net servers. +Vert.x http servers can be configured to use SNI in exactly the same way as net.adoc. Vert.x http client will present the actual hostname as _server name_ during the TLS handshake. diff --git a/src/main/java/io/vertx/core/json/Json.java b/src/main/java/io/vertx/core/json/Json.java index 4fde58c860c..4def03169e5 100644 --- a/src/main/java/io/vertx/core/json/Json.java +++ b/src/main/java/io/vertx/core/json/Json.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.math.BigDecimal; import java.time.Instant; import java.util.Base64; @@ -71,6 +72,24 @@ public static String encode(Object obj) throws EncodeException { } } + public static Buffer encodeToBuffer(Object obj) throws EncodeException { + try { + final Buffer buffer = Buffer.buffer(); + + mapper.writeValue(new OutputStream() { + @Override + public void write(int b) throws IOException { + buffer.appendByte((byte) b); + } + }, obj); + + return buffer; + + } catch (Exception e) { + throw new EncodeException("Failed to encode as JSON: " + e.getMessage()); + } + } + public static String encodePrettily(Object obj) throws EncodeException { try { return prettyMapper.writeValueAsString(obj); diff --git a/src/main/java/io/vertx/core/json/JsonArray.java b/src/main/java/io/vertx/core/json/JsonArray.java index cb34f9289ef..8aed5b41ca3 100644 --- a/src/main/java/io/vertx/core/json/JsonArray.java +++ b/src/main/java/io/vertx/core/json/JsonArray.java @@ -554,6 +554,15 @@ public String encode() { return Json.encode(list); } + /** + * Encode this JSON object as a string. + * + * @return the string encoding. + */ + public Buffer toBuffer() { + return Json.encodeToBuffer(list); + } + /** * Encode the JSON array prettily as a string * diff --git a/src/main/java/io/vertx/core/json/JsonObject.java b/src/main/java/io/vertx/core/json/JsonObject.java index bb90f314910..b1efb8236d7 100644 --- a/src/main/java/io/vertx/core/json/JsonObject.java +++ b/src/main/java/io/vertx/core/json/JsonObject.java @@ -82,7 +82,7 @@ public JsonObject(Buffer buf) { /** * Create a JsonObject from the fields of a Java object. * Faster than calling `new JsonObject(Json.encode(obj))`. - * + * * @param obj * The object to convert to a JsonObject. * @throws IllegalArgumentException @@ -96,7 +96,7 @@ public static JsonObject mapFrom(Object obj) { /** * Instantiate a Java object from a JsonObject. * Faster than calling `Json.decodeValue(Json.encode(jsonObject), type)`. - * + * * @param type * The type to instantiate from the JsonObject. * @throws IllegalArgumentException @@ -773,6 +773,15 @@ public String encodePrettily() { return Json.encodePrettily(map); } + /** + * Encode this JSON object as a string. + * + * @return the string encoding. + */ + public Buffer toBuffer() { + return Json.encodeToBuffer(map); + } + /** * Copy the JSON object * diff --git a/src/test/java/io/vertx/test/core/JsonArrayTest.java b/src/test/java/io/vertx/test/core/JsonArrayTest.java index 279bcd5b4b1..4a9aa7e3681 100644 --- a/src/test/java/io/vertx/test/core/JsonArrayTest.java +++ b/src/test/java/io/vertx/test/core/JsonArrayTest.java @@ -303,7 +303,7 @@ public void testGetInstant() { @Test public void testGetJsonObject() { - JsonObject obj = new JsonObject().put("foo", "bar"); + JsonObject obj = new JsonObject().put("foo", "bar"); jsonArray.add(obj); assertEquals(obj, jsonArray.getJsonObject(0)); try { @@ -846,6 +846,25 @@ public void testEncode() throws Exception { assertEquals(expected, json); } + @Test + public void testEncodeToBuffer() throws Exception { + jsonArray.add("foo"); + jsonArray.add(123); + jsonArray.add(1234l); + jsonArray.add(1.23f); + jsonArray.add(2.34d); + jsonArray.add(true); + byte[] bytes = TestUtils.randomByteArray(10); + jsonArray.add(bytes); + jsonArray.addNull(); + jsonArray.add(new JsonObject().put("foo", "bar")); + jsonArray.add(new JsonArray().add("foo").add(123)); + String strBytes = Base64.getEncoder().encodeToString(bytes); + Buffer expected = Buffer.buffer("[\"foo\",123,1234,1.23,2.34,true,\"" + strBytes + "\",null,{\"foo\":\"bar\"},[\"foo\",123]]", "UTF-8"); + Buffer json = jsonArray.toBuffer(); + assertArrayEquals(expected.getBytes(), json.getBytes()); + } + @Test public void testDecode() { byte[] bytes = TestUtils.randomByteArray(10); diff --git a/src/test/java/io/vertx/test/core/JsonMapperTest.java b/src/test/java/io/vertx/test/core/JsonMapperTest.java index a630a4b5711..8d0fc588c48 100644 --- a/src/test/java/io/vertx/test/core/JsonMapperTest.java +++ b/src/test/java/io/vertx/test/core/JsonMapperTest.java @@ -17,6 +17,7 @@ package io.vertx.test.core; import com.fasterxml.jackson.databind.ObjectMapper; +import io.vertx.core.buffer.Buffer; import io.vertx.core.json.Json; import org.junit.Test; @@ -84,4 +85,12 @@ public void encodeCustomTypeBinaryNull() { assertNotNull(json); assertEquals("null", json); } + + @Test + public void encodeToBuffer() { + Buffer json = Json.encodeToBuffer("Hello World!"); + assertNotNull(json); + // json strings are always UTF8 + assertEquals("\"Hello World!\"", json.toString("UTF-8")); + } } diff --git a/src/test/java/io/vertx/test/core/JsonObjectTest.java b/src/test/java/io/vertx/test/core/JsonObjectTest.java index a9662d36e25..bd6f626aa4e 100644 --- a/src/test/java/io/vertx/test/core/JsonObjectTest.java +++ b/src/test/java/io/vertx/test/core/JsonObjectTest.java @@ -1210,6 +1210,31 @@ public void testEncode() throws Exception { assertEquals(expected, json); } + @Test + public void testEncodeToBuffer() throws Exception { + jsonObject.put("mystr", "foo"); + jsonObject.put("mycharsequence", new StringBuilder("oob")); + jsonObject.put("myint", 123); + jsonObject.put("mylong", 1234l); + jsonObject.put("myfloat", 1.23f); + jsonObject.put("mydouble", 2.34d); + jsonObject.put("myboolean", true); + byte[] bytes = TestUtils.randomByteArray(10); + jsonObject.put("mybinary", bytes); + Instant now = Instant.now(); + jsonObject.put("myinstant", now); + jsonObject.putNull("mynull"); + jsonObject.put("myobj", new JsonObject().put("foo", "bar")); + jsonObject.put("myarr", new JsonArray().add("foo").add(123)); + String strBytes = Base64.getEncoder().encodeToString(bytes); + + Buffer expected = Buffer.buffer("{\"mystr\":\"foo\",\"mycharsequence\":\"oob\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + ISO_INSTANT.format(now) + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}", "UTF-8"); + + Buffer json = jsonObject.toBuffer(); + assertArrayEquals(expected.getBytes(), json.getBytes()); + } + @Test public void testDecode() throws Exception { byte[] bytes = TestUtils.randomByteArray(10); From 8888d0c7208fbd41e1279dd46cf42aa590c84416 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Wed, 10 May 2017 11:43:15 +0200 Subject: [PATCH 6/8] Avoid outputstream Signed-off-by: Paulo Lopes --- src/main/java/io/vertx/core/json/Json.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/main/java/io/vertx/core/json/Json.java b/src/main/java/io/vertx/core/json/Json.java index c18bd109567..2ef04da33ae 100644 --- a/src/main/java/io/vertx/core/json/Json.java +++ b/src/main/java/io/vertx/core/json/Json.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.math.BigDecimal; import java.time.Instant; import java.util.Base64; @@ -75,17 +74,7 @@ public static String encode(Object obj) throws EncodeException { public static Buffer encodeToBuffer(Object obj) throws EncodeException { try { - final Buffer buffer = Buffer.buffer(); - - mapper.writeValue(new OutputStream() { - @Override - public void write(int b) throws IOException { - buffer.appendByte((byte) b); - } - }, obj); - - return buffer; - + return Buffer.buffer(mapper.writeValueAsBytes(obj)); } catch (Exception e) { throw new EncodeException("Failed to encode as JSON: " + e.getMessage()); } From 4c8dcba1f44a936ac21fa626cd4ebcd54222a640 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Thu, 11 May 2017 08:58:46 +0200 Subject: [PATCH 7/8] Remove InputStream references, added decode to buffer with type reference Signed-off-by: Paulo Lopes --- src/main/java/io/vertx/core/json/Json.java | 18 ++++++++++++------ .../java/io/vertx/core/json/JsonArray.java | 5 ----- .../java/io/vertx/core/json/JsonObject.java | 5 ----- .../io/vertx/test/core/JsonMapperTest.java | 8 +++++++- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/vertx/core/json/Json.java b/src/main/java/io/vertx/core/json/Json.java index 2ef04da33ae..15f1afda1ad 100644 --- a/src/main/java/io/vertx/core/json/Json.java +++ b/src/main/java/io/vertx/core/json/Json.java @@ -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; @@ -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()); } @@ -104,13 +106,17 @@ public static T decodeValue(String str, TypeReference type) throws Decode } } - public static T decodeValue(Buffer buf, Class clazz) throws DecodeException { - return decodeValue(new ByteBufInputStream(buf.getByteBuf()), clazz); + public static T decodeValue(Buffer buf, TypeReference 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 decodeValue(InputStream stream, Class clazz) throws DecodeException { + public static T decodeValue(Buffer buf, Class 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); } diff --git a/src/main/java/io/vertx/core/json/JsonArray.java b/src/main/java/io/vertx/core/json/JsonArray.java index 8aed5b41ca3..cf7edcf1f43 100644 --- a/src/main/java/io/vertx/core/json/JsonArray.java +++ b/src/main/java/io/vertx/core/json/JsonArray.java @@ -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; @@ -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 { final Iterator listIter; diff --git a/src/main/java/io/vertx/core/json/JsonObject.java b/src/main/java/io/vertx/core/json/JsonObject.java index b1efb8236d7..ba85ddd693c 100644 --- a/src/main/java/io/vertx/core/json/JsonObject.java +++ b/src/main/java/io/vertx/core/json/JsonObject.java @@ -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.*; @@ -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> { final Iterator> mapIter; diff --git a/src/test/java/io/vertx/test/core/JsonMapperTest.java b/src/test/java/io/vertx/test/core/JsonMapperTest.java index ff353383096..b39aa99b27b 100644 --- a/src/test/java/io/vertx/test/core/JsonMapperTest.java +++ b/src/test/java/io/vertx/test/core/JsonMapperTest.java @@ -106,8 +106,14 @@ public void testGenericDecoding() { original.value = "test"; String json = Json.encode(Collections.singletonList(original)); + List correct; - List correct = Json.decodeValue(json, new TypeReference>() {}); + correct = Json.decodeValue(json, new TypeReference>() {}); + 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>() {}); assertTrue(((List)correct).get(0) instanceof Pojo); assertEquals(original.value, correct.get(0).value); From 008c793f8b3da8e07b0f82f692f916e03f75bf97 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Mon, 15 May 2017 09:47:39 +0200 Subject: [PATCH 8/8] Added javadoc Signed-off-by: Paulo Lopes --- src/main/java/io/vertx/core/json/Json.java | 53 +++++++++++++++++++ .../java/io/vertx/core/json/JsonArray.java | 4 +- .../java/io/vertx/core/json/JsonObject.java | 4 +- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/vertx/core/json/Json.java b/src/main/java/io/vertx/core/json/Json.java index 15f1afda1ad..1152eef87c7 100644 --- a/src/main/java/io/vertx/core/json/Json.java +++ b/src/main/java/io/vertx/core/json/Json.java @@ -64,6 +64,13 @@ public class Json { prettyMapper.registerModule(module); } + /** + * Encode a POJO to JSON using the underlying Jackson mapper. + * + * @param obj a POJO + * @return a String containing the JSON representation of the given POJO. + * @throws EncodeException if a property cannot be encoded. + */ public static String encode(Object obj) throws EncodeException { try { return mapper.writeValueAsString(obj); @@ -72,6 +79,13 @@ public static String encode(Object obj) throws EncodeException { } } + /** + * Encode a POJO to JSON using the underlying Jackson mapper. + * + * @param obj a POJO + * @return a Buffer containing the JSON representation of the given POJO. + * @throws EncodeException if a property cannot be encoded. + */ public static Buffer encodeToBuffer(Object obj) throws EncodeException { try { Buffer buf = Buffer.buffer(); @@ -82,6 +96,13 @@ public static Buffer encodeToBuffer(Object obj) throws EncodeException { } } + /** + * Encode a POJO to JSON with pretty indentation, using the underlying Jackson mapper. + * + * @param obj a POJO + * @return a String containing the JSON representation of the given POJO. + * @throws EncodeException if a property cannot be encoded. + */ public static String encodePrettily(Object obj) throws EncodeException { try { return prettyMapper.writeValueAsString(obj); @@ -90,6 +111,14 @@ public static String encodePrettily(Object obj) throws EncodeException { } } + /** + * Decode a given JSON string to a POJO of the given class type. + * @param str the JSON string. + * @param clazz the class to map to. + * @param the generic type. + * @return an instance of T + * @throws DecodeException when there is a parsing or invalid mapping. + */ public static T decodeValue(String str, Class clazz) throws DecodeException { try { return mapper.readValue(str, clazz); @@ -98,6 +127,14 @@ public static T decodeValue(String str, Class clazz) throws DecodeExcepti } } + /** + * 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 the generic type. + * @return an instance of T + * @throws DecodeException when there is a parsing or invalid mapping. + */ public static T decodeValue(String str, TypeReference type) throws DecodeException { try { return mapper.readValue(str, type); @@ -106,6 +143,14 @@ public static T decodeValue(String str, TypeReference type) throws Decode } } + /** + * 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 the generic type. + * @return an instance of T + * @throws DecodeException when there is a parsing or invalid mapping. + */ public static T decodeValue(Buffer buf, TypeReference type) throws DecodeException { try { return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), type); @@ -114,6 +159,14 @@ public static T decodeValue(Buffer buf, TypeReference type) throws Decode } } + /** + * Decode a given JSON buffer to a POJO of the given class type. + * @param buf the JSON buffer. + * @param clazz the class to map to. + * @param the generic type. + * @return an instance of T + * @throws DecodeException when there is a parsing or invalid mapping. + */ public static T decodeValue(Buffer buf, Class clazz) throws DecodeException { try { return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), clazz); diff --git a/src/main/java/io/vertx/core/json/JsonArray.java b/src/main/java/io/vertx/core/json/JsonArray.java index cf7edcf1f43..701849bad1f 100644 --- a/src/main/java/io/vertx/core/json/JsonArray.java +++ b/src/main/java/io/vertx/core/json/JsonArray.java @@ -554,9 +554,9 @@ public String encode() { } /** - * Encode this JSON object as a string. + * Encode this JSON object as buffer. * - * @return the string encoding. + * @return the buffer encoding. */ public Buffer toBuffer() { return Json.encodeToBuffer(list); diff --git a/src/main/java/io/vertx/core/json/JsonObject.java b/src/main/java/io/vertx/core/json/JsonObject.java index ba85ddd693c..ab1e77952bf 100644 --- a/src/main/java/io/vertx/core/json/JsonObject.java +++ b/src/main/java/io/vertx/core/json/JsonObject.java @@ -773,9 +773,9 @@ public String encodePrettily() { } /** - * Encode this JSON object as a string. + * Encode this JSON object as buffer. * - * @return the string encoding. + * @return the buffer encoding. */ public Buffer toBuffer() { return Json.encodeToBuffer(map);