mirror of
https://github.com/jlengrand/vert.x.git
synced 2026-03-10 08:51:19 +00:00
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 <julien@lengrand.fr>
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user