@Test public void testGetBinaryDefault() { byte[] bytes = TestUtils.randomByteArray(100); byte[] defBytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo", defBytes)); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo", Base64.getEncoder().encode(defBytes))); assertArrayEquals(bytes, jsonObject.getBinary("foo", null)); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo", null)); jsonObject.put("foo", 123); try { jsonObject.getBinary("foo", defBytes); fail(); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getBinary("foo", defBytes)); assertTrue(TestUtils.byteArraysEqual(defBytes, jsonObject.getBinary("absent", defBytes))); assertNull(jsonObject.getBinary("foo", null)); assertNull(jsonObject.getBinary("absent", null)); try { jsonObject.getBinary(null, null); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testPutBinary() { byte[] bin1 = TestUtils.randomByteArray(100); byte[] bin2 = TestUtils.randomByteArray(100); byte[] bin3 = TestUtils.randomByteArray(100); assertSame(jsonObject, jsonObject.put("foo", bin1)); assertArrayEquals(bin1, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin1), jsonObject.getValue("foo")); jsonObject.put("quux", bin2); assertArrayEquals(bin2, jsonObject.getBinary("quux")); assertEquals(Base64.getEncoder().encodeToString(bin2), jsonObject.getValue("quux")); assertArrayEquals(bin1, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin1), jsonObject.getValue("foo")); jsonObject.put("foo", bin3); assertArrayEquals(bin3, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin3), jsonObject.getValue("foo")); jsonObject.put("foo", (byte[]) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, bin1); fail(); } catch (NullPointerException e) { // OK } }
byte[] bytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo")); jsonObject.getBinary("foo"); fail(); } catch (ClassCastException e) { assertNull(jsonObject.getBinary("foo")); assertNull(jsonObject.getValue("foo")); assertNull(jsonObject.getBinary("absent")); assertNull(jsonObject.getValue("absent")); try { jsonObject.getBinary(null); fail(); } catch (NullPointerException e) { jsonObject.getBinary(null, null); fail(); } catch (NullPointerException e) {
@Test public void testBase64FromPOJO() { JsonObject json = JsonObject.mapFrom(new MyType2()); // attempt to deserialize back to a byte[], asserting for not null // already means that there was an attempt to parse a string to byte[] // and that the parsing succeeded (the object is of type byte[] and not null) assertNotNull(json.getBinary("base64")); }
@Test public void testToJsonObject() { Map<String, Object> map = new HashMap<>(); map.put("string", "the_string"); map.put("integer", 4); map.put("boolean", true); map.put("charsequence", new AsciiString("the_charsequence")); map.put("biginteger", new BigInteger("1234567")); map.put("binary", Buffer.buffer("hello")); map.put("object", Collections.singletonMap("nested", 4)); map.put("array", Arrays.asList(1, 2, 3)); JsonObject json = (JsonObject) ConversionHelper.toObject(map); assertEquals(8, json.size()); assertEquals("the_string", json.getString("string")); assertEquals(4, (int)json.getInteger("integer")); assertEquals(true, json.getBoolean("boolean")); assertEquals("the_charsequence", json.getString("charsequence")); assertEquals(1234567, (int)json.getInteger("biginteger")); assertEquals("hello", new String(json.getBinary("binary"))); assertEquals(new JsonObject().put("nested", 4), json.getJsonObject("object")); assertEquals(new JsonArray().add(1).add(2).add(3), json.getJsonArray("array")); }
assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float")); assertEquals(Double.valueOf(1.23d), jsonObject.getDouble("double")); assertArrayEquals(bytes, jsonObject.getBinary("binary")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("binary")); assertEquals(now, jsonObject.getInstant("instant"));
@Test public void testDecode() throws Exception { byte[] bytes = TestUtils.randomByteArray(10); String strBytes = Base64.getEncoder().encodeToString(bytes); Instant now = Instant.now(); String strInstant = ISO_INSTANT.format(now); String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; JsonObject obj = new JsonObject(json); assertEquals(json, obj.encode()); assertEquals("foo", obj.getString("mystr")); assertEquals(Integer.valueOf(123), obj.getInteger("myint")); assertEquals(Long.valueOf(1234), obj.getLong("mylong")); assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat")); assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble")); assertTrue(obj.getBoolean("myboolean")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("myinstant")); assertTrue(obj.containsKey("mynull")); JsonObject nestedObj = obj.getJsonObject("myobj"); assertEquals("bar", nestedObj.getString("foo")); JsonArray nestedArr = obj.getJsonArray("myarr"); assertEquals("foo", nestedArr.getString(0)); assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1))); }
@Test public void testGetBinaryDefault() { byte[] bytes = TestUtils.randomByteArray(100); byte[] defBytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo", defBytes)); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo", Base64.getEncoder().encode(defBytes))); assertArrayEquals(bytes, jsonObject.getBinary("foo", null)); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo", null)); jsonObject.put("foo", 123); try { jsonObject.getBinary("foo", defBytes); fail(); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getBinary("foo", defBytes)); assertTrue(TestUtils.byteArraysEqual(defBytes, jsonObject.getBinary("absent", defBytes))); assertNull(jsonObject.getBinary("foo", null)); assertNull(jsonObject.getBinary("absent", null)); try { jsonObject.getBinary(null, null); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testPutBinary() { byte[] bin1 = TestUtils.randomByteArray(100); byte[] bin2 = TestUtils.randomByteArray(100); byte[] bin3 = TestUtils.randomByteArray(100); assertSame(jsonObject, jsonObject.put("foo", bin1)); assertArrayEquals(bin1, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin1), jsonObject.getValue("foo")); jsonObject.put("quux", bin2); assertArrayEquals(bin2, jsonObject.getBinary("quux")); assertEquals(Base64.getEncoder().encodeToString(bin2), jsonObject.getValue("quux")); assertArrayEquals(bin1, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin1), jsonObject.getValue("foo")); jsonObject.put("foo", bin3); assertArrayEquals(bin3, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin3), jsonObject.getValue("foo")); jsonObject.put("foo", (byte[]) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, bin1); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testBase64FromPOJO() { JsonObject json = JsonObject.mapFrom(new MyType2()); // attempt to deserialize back to a byte[], asserting for not null // already means that there was an attempt to parse a string to byte[] // and that the parsing succeeded (the object is of type byte[] and not null) assertNotNull(json.getBinary("base64")); }
byte[] bytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo")); jsonObject.getBinary("foo"); fail(); } catch (ClassCastException e) { assertNull(jsonObject.getBinary("foo")); assertNull(jsonObject.getValue("foo")); assertNull(jsonObject.getBinary("absent")); assertNull(jsonObject.getValue("absent")); try { jsonObject.getBinary(null); fail(); } catch (NullPointerException e) { jsonObject.getBinary(null, null); fail(); } catch (NullPointerException e) {
@Test public void testToJsonObject() { Map<String, Object> map = new HashMap<>(); map.put("string", "the_string"); map.put("integer", 4); map.put("boolean", true); map.put("charsequence", new AsciiString("the_charsequence")); map.put("biginteger", new BigInteger("1234567")); map.put("binary", Buffer.buffer("hello")); map.put("object", Collections.singletonMap("nested", 4)); map.put("array", Arrays.asList(1, 2, 3)); JsonObject json = (JsonObject) ConversionHelper.toObject(map); assertEquals(8, json.size()); assertEquals("the_string", json.getString("string")); assertEquals(4, (int)json.getInteger("integer")); assertEquals(true, json.getBoolean("boolean")); assertEquals("the_charsequence", json.getString("charsequence")); assertEquals(1234567, (int)json.getInteger("biginteger")); assertEquals("hello", new String(json.getBinary("binary"))); assertEquals(new JsonObject().put("nested", 4), json.getJsonObject("object")); assertEquals(new JsonArray().add(1).add(2).add(3), json.getJsonArray("array")); }
assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float")); assertEquals(Double.valueOf(1.23d), jsonObject.getDouble("double")); assertArrayEquals(bytes, jsonObject.getBinary("binary")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("binary")); assertEquals(now, jsonObject.getInstant("instant"));
@Test public void testDecode() throws Exception { byte[] bytes = TestUtils.randomByteArray(10); String strBytes = Base64.getEncoder().encodeToString(bytes); Instant now = Instant.now(); String strInstant = ISO_INSTANT.format(now); String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; JsonObject obj = new JsonObject(json); assertEquals(json, obj.encode()); assertEquals("foo", obj.getString("mystr")); assertEquals(Integer.valueOf(123), obj.getInteger("myint")); assertEquals(Long.valueOf(1234), obj.getLong("mylong")); assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat")); assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble")); assertTrue(obj.getBoolean("myboolean")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("myinstant")); assertTrue(obj.containsKey("mynull")); JsonObject nestedObj = obj.getJsonObject("myobj"); assertEquals("bar", nestedObj.getString("foo")); JsonArray nestedArr = obj.getJsonArray("myarr"); assertEquals("foo", nestedArr.getString(0)); assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1))); }
private JsonObject saveRetainMessage(JsonObject request) { String topic = request.getString("topic"); String tenant = request.getString("tenant"); byte[] message = request.getBinary("message"); Map<String, byte[]> db = db(tenant); db.put(topic, message); JsonObject response = new JsonObject(); response.put("topic", topic).put("message", message); return response; }
public DataObjectWithBuffer(JsonObject json) { byte[] buffer = json.getBinary("buffer"); this.buffer = buffer != null ? Buffer.buffer(buffer) : null; }
@SuppressWarnings("all") public Buffer responseBuffer() { Buffer buffer = Buffer.buffer(); if (null == this.error) { final JsonObject response = this.data.getJsonObject(Key.DATA); buffer = Buffer.buffer(response.getBinary("bytes")); } else { final JsonObject error = this.error.toJson(); buffer = Buffer.buffer(error.encode()); } return buffer; }
@SuppressWarnings("all") public Buffer responseBuffer() { Buffer buffer = Buffer.buffer(); if (null == this.error) { final JsonObject response = this.data.getJsonObject(Key.DATA); buffer = Buffer.buffer(response.getBinary("bytes")); } else { final JsonObject error = this.error.toJson(); buffer = Buffer.buffer(error.encode()); } return buffer; }
public PublishMessage deserializePublishMessage(JsonObject json) { PublishMessage ret = new PublishMessage(); ret.setTopicName(json.getString("topicName")); AbstractMessage.QOSType qos = AbstractMessage.QOSType.valueOf(json.getString("qos")); ret.setQos(qos); byte[] payload = json.getBinary("payload"); ret.setPayload(ByteBuffer.wrap(payload)); if(qos == AbstractMessage.QOSType.LEAST_ONE || qos == AbstractMessage.QOSType.EXACTLY_ONCE) { ret.setMessageID(json.getInteger("messageID")); } return ret; }
public DataObjectWithNestedBuffer(JsonObject json) { byte[] buffer = json.getBinary("buffer"); this.buffer = buffer != null ? Buffer.buffer(buffer) : null; JsonObject nested = json.getJsonObject("nested"); this.nested = nested != null ? new DataObjectWithBuffer(nested) : null; JsonArray buffers_ = json.getJsonArray("buffers"); if (buffers_ != null) { this.buffers = new ArrayList<>(); for (int i = 0;i < buffers_.size();i++) { buffers.add(Buffer.buffer(buffers_.getBinary(i))); } } }