From e27c137132165cac8130233a2e0f19901ccbb320 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Mon, 24 Feb 2020 16:47:03 +0100 Subject: [PATCH] Fixes #3289 : Renames toJsonXXX() methods from Buffer to mapToJsonXXX() * Renames toJsonObject to mapToJsonObject and deprecates the method * Renames toJsonArray to mapToJsonArray and deprecates the method * Renames toJson to mapToJson and deprecates the method Signed-off-by: Julien Lengrand-Lambert --- .../java/io/vertx/core/buffer/Buffer.java | 27 +++++++++++++++ .../io/vertx/core/buffer/impl/BufferImpl.java | 10 ++++++ .../java/io/vertx/core/buffer/BufferTest.java | 34 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/main/java/io/vertx/core/buffer/Buffer.java b/src/main/java/io/vertx/core/buffer/Buffer.java index 4a4830ee2..ed9e14cfd 100644 --- a/src/main/java/io/vertx/core/buffer/Buffer.java +++ b/src/main/java/io/vertx/core/buffer/Buffer.java @@ -133,23 +133,50 @@ public interface Buffer extends ClusterSerializable, Shareable { /** * Returns a Json object representation of the Buffer. + * + * @deprecated use {@link #mapToJsonObject()} instead. */ + @Deprecated JsonObject toJsonObject(); /** * Returns a Json array representation of the Buffer. + * + * @deprecated use {@link #mapToJsonArray()} instead. */ + @Deprecated JsonArray toJsonArray(); /** * Returns a Json representation of the Buffer. * * @return a JSON element which can be a {@link JsonArray}, {@link JsonObject}, {@link String}, ...etc if the buffer contains an array, object, string, ...etc + * + * @deprecated use {@link #mapToJson()} instead */ default Object toJson() { return Json.CODEC.fromBuffer(this, Object.class); } + /** + * Returns a Json object representation of the Buffer. + */ + JsonObject mapToJsonObject(); + + /** + * Returns a Json array representation of the Buffer. + */ + JsonArray mapToJsonArray(); + + /** + * Returns a Json representation of the Buffer. + * + * @return a JSON element which can be a {@link JsonArray}, {@link JsonObject}, {@link String}, ...etc if the buffer contains an array, object, string, ...etc + */ + default Object mapToJson() { + return Json.CODEC.fromBuffer(this, Object.class); + } + /** * Returns the {@code byte} at position {@code pos} in the Buffer. * 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 34524db5d..49a99be9f 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java @@ -118,6 +118,16 @@ public class BufferImpl implements Buffer { return new JsonArray(this); } + @Override + public JsonObject mapToJsonObject() { + return new JsonObject(this); + } + + @Override + public JsonArray mapToJsonArray() { + return new JsonArray(this); + } + public byte getByte(int pos) { return buffer.getByte(pos); } diff --git a/src/test/java/io/vertx/core/buffer/BufferTest.java b/src/test/java/io/vertx/core/buffer/BufferTest.java index 6036d4e4a..8460a6f9d 100644 --- a/src/test/java/io/vertx/core/buffer/BufferTest.java +++ b/src/test/java/io/vertx/core/buffer/BufferTest.java @@ -1057,6 +1057,23 @@ public class BufferTest { } } + @Test + public void testMapToJsonObject() throws Exception { + JsonObject obj = new JsonObject(); + obj.put("wibble", "wibble_value"); + obj.put("foo", 5); + obj.put("bar", true); + Buffer buff = Buffer.buffer(obj.encode()); + assertEquals(obj, buff.mapToJsonObject()); + + buff = Buffer.buffer(TestUtils.randomAlphaString(10)); + try { + buff.mapToJsonObject(); + fail(); + } catch (DecodeException ignore) { + } + } + @Test public void testToJsonArray() throws Exception { JsonArray arr = new JsonArray(); @@ -1074,6 +1091,23 @@ public class BufferTest { } } + @Test + public void testMapToJsonArray() throws Exception { + JsonArray arr = new JsonArray(); + arr.add("wibble"); + arr.add(5); + arr.add(true); + Buffer buff = Buffer.buffer(arr.encode()); + assertEquals(arr, buff.mapToJsonArray()); + + buff = Buffer.buffer(TestUtils.randomAlphaString(10)); + try { + buff.mapToJsonObject(); + fail(); + } catch (DecodeException ignore) { + } + } + @Test public void testLength() throws Exception { byte[] bytes = TestUtils.randomByteArray(100);