Search in sources :

Example 26 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class JsonArrayTest method testCreateFromListCharSequence.

@Test
public void testCreateFromListCharSequence() {
    List<Object> list = new ArrayList<>();
    list.add("foo");
    list.add(123);
    list.add(new StringBuilder("eek"));
    JsonArray arr = new JsonArray(list);
    assertEquals("foo", arr.getString(0));
    assertEquals(Integer.valueOf(123), arr.getInteger(1));
    assertEquals("eek", arr.getString(2));
    assertSame(list, arr.getList());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 27 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class JsonArrayTest method testAddJsonArray.

@Test
public void testAddJsonArray() {
    JsonArray arr = new JsonArray().add("foo");
    assertSame(jsonArray, jsonArray.add(arr));
    assertEquals(arr, jsonArray.getJsonArray(0));
    try {
        jsonArray.add((JsonArray) null);
        fail();
    } catch (NullPointerException e) {
    // OK
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Test(org.junit.Test)

Example 28 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class JsonObjectTest method testGetValue.

@Test
public void testGetValue() {
    jsonObject.put("foo", 123);
    assertEquals(123, jsonObject.getValue("foo"));
    jsonObject.put("foo", 123l);
    assertEquals(123l, jsonObject.getValue("foo"));
    jsonObject.put("foo", 123f);
    assertEquals(123f, jsonObject.getValue("foo"));
    jsonObject.put("foo", 123d);
    assertEquals(123d, jsonObject.getValue("foo"));
    jsonObject.put("foo", false);
    assertEquals(false, jsonObject.getValue("foo"));
    jsonObject.put("foo", true);
    assertEquals(true, jsonObject.getValue("foo"));
    jsonObject.put("foo", "bar");
    assertEquals("bar", jsonObject.getValue("foo"));
    JsonObject obj = new JsonObject().put("blah", "wibble");
    jsonObject.put("foo", obj);
    assertEquals(obj, jsonObject.getValue("foo"));
    JsonArray arr = new JsonArray().add("blah").add("wibble");
    jsonObject.put("foo", arr);
    assertEquals(arr, jsonObject.getValue("foo"));
    byte[] bytes = TestUtils.randomByteArray(100);
    jsonObject.put("foo", bytes);
    assertTrue(TestUtils.byteArraysEqual(bytes, Base64.getDecoder().decode((String) jsonObject.getValue("foo"))));
    jsonObject.putNull("foo");
    assertNull(jsonObject.getValue("foo"));
    assertNull(jsonObject.getValue("absent"));
    // JsonObject with inner Map
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> innerMap = new HashMap<>();
    innerMap.put("blah", "wibble");
    map.put("foo", innerMap);
    jsonObject = new JsonObject(map);
    obj = (JsonObject) jsonObject.getValue("foo");
    assertEquals("wibble", obj.getString("blah"));
    // JsonObject with inner List
    map = new HashMap<>();
    List<Object> innerList = new ArrayList<>();
    innerList.add("blah");
    map.put("foo", innerList);
    jsonObject = new JsonObject(map);
    arr = (JsonArray) jsonObject.getValue("foo");
    assertEquals("blah", arr.getString(0));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 29 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class JsonObjectTest method assertNumberEquals.

private void assertNumberEquals(Number value1, Number value2) {
    JsonObject o1 = new JsonObject().put("key", value1);
    JsonObject o2 = new JsonObject().put("key", value2);
    if (!o1.equals(o2)) {
        fail("Was expecting " + value1.getClass().getSimpleName() + ":" + value1 + " == " + value2.getClass().getSimpleName() + ":" + value2);
    }
    JsonArray a1 = new JsonArray().add(value1);
    JsonArray a2 = new JsonArray().add(value2);
    if (!a1.equals(a2)) {
        fail("Was expecting " + value1.getClass().getSimpleName() + ":" + value1 + " == " + value2.getClass().getSimpleName() + ":" + value2);
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Example 30 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class JsonObjectTest method testDecode.

@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"));
    assertTrue(TestUtils.byteArraysEqual(bytes, obj.getBinary("mybinary")));
    assertEquals(now, obj.getInstant("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)));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Instant(java.time.Instant) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)116 JsonObject (io.vertx.core.json.JsonObject)82 Test (org.junit.Test)72 ArrayList (java.util.ArrayList)19 Future (io.vertx.core.Future)15 Buffer (io.vertx.core.buffer.Buffer)15 Handler (io.vertx.core.Handler)14 HttpURLConnection (java.net.HttpURLConnection)13 HashMap (java.util.HashMap)13 TestContext (io.vertx.ext.unit.TestContext)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)10 RunWith (org.junit.runner.RunWith)10 StandardCharsets (java.nio.charset.StandardCharsets)9 Instant (java.time.Instant)9 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)9 Vertx (io.vertx.core.Vertx)8 Async (io.vertx.ext.unit.Async)8 Constants (org.eclipse.hono.util.Constants)8 CredentialsObject (org.eclipse.hono.util.CredentialsObject)8 Before (org.junit.Before)8