@Test public void testAddNull() { assertSame(jsonArray, jsonArray.addNull()); assertEquals(null, jsonArray.getString(0)); assertTrue(jsonArray.hasNull(0)); }
@Test public void testEncode() 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); String expected = "[\"foo\",123,1234,1.23,2.34,true,\"" + strBytes + "\",null,{\"foo\":\"bar\"},[\"foo\",123]]"; String json = jsonArray.encode(); 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 testEncodePrettily() 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); String expected = "[ \"foo\", 123, 1234, 1.23, 2.34, true, \"" + strBytes + "\", null, {" + Utils.LINE_SEPARATOR + " \"foo\" : \"bar\"" + Utils.LINE_SEPARATOR + "}, [ \"foo\", 123 ] ]"; String json = jsonArray.encodePrettily(); assertEquals(expected, json); }
@Test public void testParseArrayValue() { JsonParser parser = JsonParser.newParser(); AtomicInteger status = new AtomicInteger(); parser.arrayValueMode(); JsonArray expected = new JsonArray() .add(3) .add(3.5d) .add(true) .add(false) .add("s") .addNull() .add(new JsonObject().put("foo", "bar")) .add(new JsonArray().add(0).add(1).add(2)) .add(new byte[]{1, 2, 3}); parser.handler(event -> { assertEquals(expected, event.value()); assertEquals(0, status.getAndIncrement()); }); parser.handle(expected.toBuffer()); assertEquals(1, status.get()); }
@Test public void testHasNull() { jsonArray.addNull(); jsonArray.add("foo"); assertEquals(null, jsonArray.getString(0)); assertTrue(jsonArray.hasNull(0)); assertFalse(jsonArray.hasNull(1)); }
@Test public void testGetBinary() { byte[] bytes = TestUtils.randomByteArray(10); jsonArray.add(bytes); assertArrayEquals(bytes, jsonArray.getBinary(0)); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonArray.getValue(0)); assertArrayEquals(bytes, Base64.getDecoder().decode(jsonArray.getString(0))); try { jsonArray.getBinary(-1); fail(); } catch (IndexOutOfBoundsException e) { // OK } try { jsonArray.getBinary(1); fail(); } catch (IndexOutOfBoundsException e) { // OK } jsonArray.add(123); try { jsonArray.getBinary(1); fail(); } catch (ClassCastException e) { // OK } jsonArray.addNull(); assertNull(jsonArray.getBinary(2)); }
@Test public void testGetJsonObject() { JsonObject obj = new JsonObject().put("foo", "bar"); jsonArray.add(obj); assertEquals(obj, jsonArray.getJsonObject(0)); try { jsonArray.getJsonObject(-1); fail(); } catch (IndexOutOfBoundsException e) { // OK } try { jsonArray.getJsonObject(1); fail(); } catch (IndexOutOfBoundsException e) { // OK } jsonArray.add(123); try { jsonArray.getJsonObject(1); fail(); } catch (ClassCastException e) { // OK } jsonArray.addNull(); assertNull(jsonArray.getJsonObject(2)); }
assertEquals(now, jsonArray.getInstant(10)); assertEquals(now.toString(), jsonArray.getValue(10)); jsonArray.addNull(); assertNull(jsonArray.getValue(11)); try {
jsonArray.addNull(); assertNull(jsonArray.getFloat(5));
jsonArray.addNull(); assertNull(jsonArray.getDouble(5));
jsonArray.addNull(); assertNull(jsonArray.getLong(5));
jsonArray.addNull(); assertNull(jsonArray.getInteger(5));
@Test public void testGetJsonArray() { JsonArray arr = new JsonArray().add("foo"); jsonArray.add(arr); assertEquals(arr, jsonArray.getJsonArray(0)); try { jsonArray.getJsonArray(-1); fail(); } catch (IndexOutOfBoundsException e) { // OK } try { jsonArray.getJsonArray(1); fail(); } catch (IndexOutOfBoundsException e) { // OK } jsonArray.add(123); try { jsonArray.getJsonArray(1); fail(); } catch (ClassCastException e) { // OK } jsonArray.addNull(); assertNull(jsonArray.getJsonArray(2)); }
jsonArray.addNull(); assertNull(jsonArray.getInstant(2)); assertNull(jsonArray.getValue(2));
@Test public void testGetString() { jsonArray.add("foo"); assertEquals("foo", jsonArray.getString(0)); try { jsonArray.getString(-1); fail(); } catch (IndexOutOfBoundsException e) { // OK } try { jsonArray.getString(1); fail(); } catch (IndexOutOfBoundsException e) { // OK } jsonArray.add(123); try { jsonArray.getString(1); fail(); } catch (ClassCastException e) { // OK } jsonArray.addNull(); assertNull(jsonArray.getString(2)); }
@Test public void testGetBoolean() { jsonArray.add(true); assertEquals(true, jsonArray.getBoolean(0)); jsonArray.add(false); assertEquals(false, jsonArray.getBoolean(1)); try { jsonArray.getBoolean(-1); fail(); } catch (IndexOutOfBoundsException e) { // OK } try { jsonArray.getBoolean(2); fail(); } catch (IndexOutOfBoundsException e) { // OK } jsonArray.add(123); try { jsonArray.getBoolean(2); fail(); } catch (ClassCastException e) { // OK } jsonArray.addNull(); assertNull(jsonArray.getBoolean(3)); }
@Test public void testAddNull() { assertSame(jsonArray, jsonArray.addNull()); assertEquals(null, jsonArray.getString(0)); assertTrue(jsonArray.hasNull(0)); }
@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 testHasNull() { jsonArray.addNull(); jsonArray.add("foo"); assertEquals(null, jsonArray.getString(0)); assertTrue(jsonArray.hasNull(0)); assertFalse(jsonArray.hasNull(1)); }