Search in sources :

Example 21 with JsonObject

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

the class ConversionHelperTest method testWrapObject.

/**
   * Confirm that when we convert to map/list form we do so recursively.
   */
@Test
public void testWrapObject() {
    // Create a JsonObject with nested JsonObject and JsonArray values
    JsonObject obj = new JsonObject().put("nestedObj", new JsonObject().put("key", "value")).put("nestedList", new JsonArray().add(new JsonObject().put("key", "value")));
    // Get the wrapped form and confirm that it acted recursively
    Map<String, Object> wrapped = ConversionHelper.fromObject(obj);
    assertTrue(wrapped.get("nestedObj") instanceof Map);
    List<Object> theList = (List<Object>) wrapped.get("nestedList");
    assertTrue(theList.get(0) instanceof Map);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) AsciiString(io.netty.util.AsciiString) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 22 with JsonObject

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

the class ConversionHelperTest method testFromJsonArray.

@Test
public void testFromJsonArray() {
    JsonArray object = new JsonArray();
    object.add("the_string");
    object.add(4);
    object.add(true);
    object.add("hello".getBytes());
    object.add(new JsonObject().put("nested", 4));
    object.add(new JsonArray().add(1).add(2).add(3));
    List<Object> map = ConversionHelper.fromObject(object);
    assertEquals(6, map.size());
    assertEquals("the_string", map.get(0));
    assertEquals(4, map.get(1));
    assertEquals(true, map.get(2));
    assertEquals("hello", new String(Base64.getDecoder().decode((String) map.get(3))));
    assertEquals(Collections.singletonMap("nested", 4), map.get(4));
    assertEquals(Arrays.asList(1, 2, 3), map.get(5));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 23 with JsonObject

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

the class ConversionHelperTest method testFromJsonObject.

@Test
public void testFromJsonObject() {
    JsonObject object = new JsonObject();
    object.put("string", "the_string");
    object.put("integer", 4);
    object.put("boolean", true);
    object.put("binary", "hello".getBytes());
    object.put("object", new JsonObject().put("nested", 4));
    object.put("array", new JsonArray().add(1).add(2).add(3));
    Map<String, Object> map = ConversionHelper.fromObject(object);
    assertEquals(6, map.size());
    assertEquals("the_string", map.get("string"));
    assertEquals(4, map.get("integer"));
    assertEquals(true, map.get("boolean"));
    assertEquals("hello", new String(Base64.getDecoder().decode((String) map.get("binary"))));
    assertEquals(Collections.singletonMap("nested", 4), map.get("object"));
    assertEquals(Arrays.asList(1, 2, 3), map.get("array"));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 24 with JsonObject

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

the class CoreExamples method example13.

public void example13(Vertx vertx) {
    JsonObject config = new JsonObject().put("name", "tim").put("directory", "/blah");
    DeploymentOptions options = new DeploymentOptions().setConfig(config);
    vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", options);
}
Also used : JsonObject(io.vertx.core.json.JsonObject)

Example 25 with JsonObject

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

the class Examples method mapFromPojo.

public void mapFromPojo(HttpServerRequest request) {
    User user = new User("Dale", "Cooper");
    JsonObject jsonObject = JsonObject.mapFrom(user);
}
Also used : JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)185 Test (org.junit.Test)136 JsonArray (io.vertx.core.json.JsonArray)44 ArrayList (java.util.ArrayList)11 DeploymentOptions (io.vertx.core.DeploymentOptions)8 HashMap (java.util.HashMap)8 Launcher (io.vertx.core.Launcher)7 Buffer (io.vertx.core.buffer.Buffer)7 IOException (java.io.IOException)7 Instant (java.time.Instant)7 AsciiString (io.netty.util.AsciiString)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Message (io.vertx.core.eventbus.Message)4 File (java.io.File)4 Random (java.util.Random)4 AbstractVerticle (io.vertx.core.AbstractVerticle)3 Vertx (io.vertx.core.Vertx)3 VertxOptions (io.vertx.core.VertxOptions)3 VertxInternal (io.vertx.core.impl.VertxInternal)3 DecodeException (io.vertx.core.json.DecodeException)3