@Override public Object next() { Object val = listIter.next(); if (val instanceof Map) { val = new JsonObject((Map)val); } else if (val instanceof List) { val = new JsonArray((List)val); } return val; }
public HAManager(VertxInternal vertx, DeploymentManager deploymentManager, ClusterManager clusterManager, Map<String, String> clusterMap, int quorumSize, String group, boolean enabled) { this.vertx = vertx; this.deploymentManager = deploymentManager; this.clusterManager = clusterManager; this.clusterMap = clusterMap; this.quorumSize = enabled ? quorumSize : 0; this.group = enabled ? group : "__DISABLED__"; this.enabled = enabled; this.haInfo = new JsonObject().put("verticles", new JsonArray()).put("group", this.group); this.nodeID = clusterManager.getNodeID(); }
static void toJson(PemTrustOptions obj, java.util.Map<String, Object> json) { if (obj.getCertPaths() != null) { JsonArray array = new JsonArray(); obj.getCertPaths().forEach(item -> array.add(item)); json.put("certPaths", array); } if (obj.getCertValues() != null) { JsonArray array = new JsonArray(); obj.getCertValues().forEach(item -> array.add(java.util.Base64.getEncoder().encodeToString(item.getBytes()))); json.put("certValues", array); } } }
@Test public void testCommentsInJson() { String jsonWithComments = "// single line comment\n" + "/*\n" + " This is a multi \n" + " line comment\n" + "*/\n" + "[\n" + "// another single line comment this time inside the JSON array itself\n" + " \"foo\", \"bar\" // and a single line comment at end of line \n" + "/*\n" + " This is a another multi \n" + " line comment this time inside the JSON array itself\n" + "*/\n" + "]"; JsonArray json = new JsonArray(jsonWithComments); assertEquals("[\"foo\",\"bar\"]", json.encode()); }
private void listAlbums(Message<JsonObject> msg) { // issue a find command to mongo to fetch all documents from the "albums" collection. mongo.find("albums", new JsonObject(), lookup -> { // error handling if (lookup.failed()) { msg.fail(500, lookup.cause().getMessage()); return; } // now convert the list to a JsonArray because it will be easier to encode the final object as the response. final JsonArray json = new JsonArray(); for (JsonObject o : lookup.result()) { json.add(o); } msg.reply(json); }); }
@Override public void start() throws Exception { // A simple backend vertx.eventBus().<JsonObject>consumer("backend", msg -> { JsonObject json = msg.body(); switch (json.getString("op", "")) { case "get": { String productID = json.getString("id"); msg.reply(products.get(productID)); break; } case "add": { String productID = json.getString("id"); JsonObject product = json.getJsonObject("product"); product.put("id", productID); msg.reply(addProduct(product)); break; } case "list": { JsonArray arr = new JsonArray(); products.forEach((k, v) -> arr.add(v)); msg.reply(arr); break; } default: { msg.fail(0, "operation not permitted"); } } }); }
@Test public void testJsonArrayEquality() { JsonObject obj = new JsonObject(Collections.singletonMap("abc", Collections.singletonList(3))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", Collections.singletonList(3)))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", Collections.singletonList(3L)))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", new JsonArray().add(3)))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", new JsonArray().add(3L)))); assertNotEquals(obj, new JsonObject(Collections.singletonMap("abc", Collections.singletonList(4)))); assertNotEquals(obj, new JsonObject(Collections.singletonMap("abc", new JsonArray().add(4)))); JsonArray array = new JsonArray(Collections.singletonList(Collections.singletonList(3))); assertEquals(array, new JsonArray(Collections.singletonList(Collections.singletonList(3)))); assertEquals(array, new JsonArray(Collections.singletonList(Collections.singletonList(3L)))); assertEquals(array, new JsonArray(Collections.singletonList(new JsonArray().add(3)))); assertEquals(array, new JsonArray(Collections.singletonList(new JsonArray().add(3L)))); assertNotEquals(array, new JsonArray(Collections.singletonList(Collections.singletonList(4)))); assertNotEquals(array, new JsonArray(Collections.singletonList(new JsonArray().add(4)))); }
@Test public void testCreateFromList() { List<Object> list = new ArrayList<>(); list.add("foo"); list.add(123); JsonArray arr = new JsonArray(list); assertEquals("foo", arr.getString(0)); assertEquals(Integer.valueOf(123), arr.getInteger(1)); assertSame(list, arr.getList()); }
/** * Get the Object value at position {@code pos} in the array. * * @param pos the position in the array * @return the Integer, or null if a null value present */ public Object getValue(int pos) { Object val = list.get(pos); if (val instanceof Map) { val = new JsonObject((Map)val); } else if (val instanceof List) { val = new JsonArray((List)val); } return val; }
@Test public void testJsonObjectEquality2() { JsonObject obj1 = new JsonObject().put("arr", new JsonArray().add("x")); List < Object > list = new ArrayList<>(); list.add("x"); Map<String, Object> map = new HashMap<>(); map.put("arr", list); JsonObject obj2 = new JsonObject(map); Iterator<Map.Entry<String, Object>> iter = obj2.iterator(); // There was a bug where iteration of entries caused the underlying object to change resulting in a // subsequent equals changing while (iter.hasNext()) { Map.Entry<String, Object> entry = iter.next(); } assertEquals(obj2, obj1); }
@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()); }
@Test public void testInvalidValsOnCopy3() { List<Object> invalid = new ArrayList<>(); Map<String, Object> invalid2 = new HashMap<>(); invalid2.put("foo", new SomeClass()); invalid.add(invalid2); JsonArray arr = new JsonArray(invalid); try { arr.copy(); fail(); } catch (IllegalStateException e) { // OK } }
/** * Remove the value at the specified position in the JSON array. * * @param pos the position to remove the value at * @return the removed value if removed, null otherwise. If the value is a Map, a {@link JsonObject} is built from * this Map and returned. It the value is a List, a {@link JsonArray} is built form this List and returned. */ public Object remove(int pos) { Object removed = list.remove(pos); if (removed instanceof Map) { return new JsonObject((Map) removed); } else if (removed instanceof ArrayList) { return new JsonArray((List) removed); } return removed; }
@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)); }