@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(); }
private void addToHA(String deploymentID, String verticleName, DeploymentOptions deploymentOptions) { String encoded; synchronized (haInfo) { JsonObject verticleConf = new JsonObject().put("dep_id", deploymentID); verticleConf.put("verticle_name", verticleName); verticleConf.put("options", deploymentOptions.toJson()); JsonArray haMods = haInfo.getJsonArray("verticles"); haMods.add(verticleConf); encoded = haInfo.encode(); clusterMap.put(nodeID, encoded); } }
@Test public void testCreateFromMapNestedJsonObject() { Map<String, Object> map = new HashMap<>(); JsonObject nestedObj = new JsonObject().put("foo", "bar"); map.put("nested", nestedObj); JsonObject obj = new JsonObject(map); JsonObject nestedRetrieved = obj.getJsonObject("nested"); assertEquals("bar", nestedRetrieved.getString("foo")); }
@Test public void testMapReplaceIfPresentJsonObject() { testMapReplaceIfPresent(new JsonObject().put("foo", "bar"), new JsonObject().put("uihwqduh", "qiwiojw"), new JsonObject().put("regerg", "wfwef")); }
@Test public void testCreateFromMapNestedList() { Map<String, Object> map = new HashMap<>(); List<String> nestedArr = Arrays.asList("foo"); map.put("nested", nestedArr); JsonObject obj = new JsonObject(map); JsonArray nestedRetrieved = obj.getJsonArray("nested"); assertEquals("foo", nestedRetrieved.getString(0)); }
private void testInvalidValueToPOJO(String key) { try { new JsonObject().put(key, "1").mapTo(MyType2.class); fail(); } catch (IllegalArgumentException e) { assertThat(e.getCause(), is(instanceOf(InvalidFormatException.class))); InvalidFormatException ife = (InvalidFormatException) e.getCause(); assertEquals("1", ife.getValue()); } }
@Override public void process(JsonObject document, Handler<AsyncResult<JsonObject>> resultHandler) { System.out.println("Processing..."); JsonObject result = document.copy(); if (!document.containsKey("name")) { resultHandler.handle(ServiceException.fail(NO_NAME_ERROR, "No name in the document")); } else if (document.getString("name").isEmpty() || document.getString("name").equalsIgnoreCase("bad")) { resultHandler.handle(ServiceException.fail(BAD_NAME_ERROR, "Bad name in the document: " + document.getString("name"), new JsonObject().put("name", document.getString("name")))); } else { result.put("approved", true); resultHandler.handle(Future.succeededFuture(result)); } }
private String chooseHashedNode(String group, int hashCode) { List<String> nodes = clusterManager.getNodes(); ArrayList<String> matchingMembers = new ArrayList<>(); for (String node: nodes) { String sclusterInfo = clusterMap.get(node); if (sclusterInfo != null) { JsonObject clusterInfo = new JsonObject(sclusterInfo); String memberGroup = clusterInfo.getString("group"); if (group == null || group.equals(memberGroup)) { matchingMembers.add(node); } } } if (!matchingMembers.isEmpty()) { // Hashcodes can be -ve so make it positive long absHash = (long)hashCode + Integer.MAX_VALUE; long lpos = absHash % matchingMembers.size(); return matchingMembers.get((int)lpos); } else { return null; } } }
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); }); }
@Test public void testCreateFromBuffer() { JsonObject excepted = new JsonObject(); excepted.put("foo", "bar"); excepted.put("quux", 123); Buffer buf = Buffer.buffer(excepted.encode()); assertEquals(excepted, new JsonObject(buf)); }
@Test public void testMapRemoveIfPresentJsonObject() { testMapRemoveIfPresent(new JsonObject().put("foo", "bar"), new JsonObject().put("uihwqduh", "qiwiojw"), new JsonObject().put("regerg", "wfwef")); }
@Test public final void testEquals() { NetClientOptions options1 = new NetClientOptions(); NetClientOptions options2 = new NetClientOptions(new JsonObject("{}")); assertEquals(options1, options2); options1.setHostnameVerificationAlgorithm("HTTPS"); options2.setHostnameVerificationAlgorithm("HTTPS"); assertEquals(options1, options2); options2.setHostnameVerificationAlgorithm(new String("HTTPS")); assertEquals(options1, options2); }
String uri = config.getString("mongo_uri"); if (uri == null) { uri = "mongodb://localhost:27017"; String db = config.getString("mongo_db"); if (db == null) { db = "test"; JsonObject mongoconfig = new JsonObject() .put("connection_string", uri) .put("db_name", db); JsonObject product1 = new JsonObject().put("itemId", "12345").put("name", "Cooler").put("price", "100.0"); mongoClient.find("products", new JsonObject().put("itemId", "12345"), res -> { System.out.println("Name is " + res.result().get(0).getString("name")); mongoClient.remove("products", new JsonObject().put("itemId", "12345"), rs -> { if (rs.succeeded()) { System.out.println("Product removed ");
/** * 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; }
private void checkQuorum() { if (quorumSize == 0) { this.attainedQuorum = true; } else { List<String> nodes = clusterManager.getNodes(); int count = 0; for (String node : nodes) { String json = clusterMap.get(node); if (json != null) { JsonObject clusterInfo = new JsonObject(json); String group = clusterInfo.getString("group"); if (group.equals(this.group)) { count++; } } } boolean attained = count >= quorumSize; if (!attainedQuorum && attained) { // A quorum has been attained so we can deploy any currently undeployed HA deploymentIDs log.info("A quorum has been obtained. Any deploymentIDs waiting on a quorum will now be deployed"); this.attainedQuorum = true; } else if (attainedQuorum && !attained) { // We had a quorum but we lost it - we must undeploy any HA deploymentIDs log.info("There is no longer a quorum. Any HA deploymentIDs will be undeployed until a quorum is re-attained"); this.attainedQuorum = false; } } }
@Test public void testJsonObjectEquality() { JsonObject obj = new JsonObject(Collections.singletonMap("abc", Collections.singletonMap("def", 3))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", Collections.singletonMap("def", 3)))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", Collections.singletonMap("def", 3L)))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", new JsonObject().put("def", 3)))); assertEquals(obj, new JsonObject(Collections.singletonMap("abc", new JsonObject().put("def", 3L)))); assertNotEquals(obj, new JsonObject(Collections.singletonMap("abc", Collections.singletonMap("def", 4)))); assertNotEquals(obj, new JsonObject(Collections.singletonMap("abc", new JsonObject().put("def", 4)))); JsonArray array = new JsonArray(Collections.singletonList(Collections.singletonMap("def", 3))); assertEquals(array, new JsonArray(Collections.singletonList(Collections.singletonMap("def", 3)))); assertEquals(array, new JsonArray(Collections.singletonList(Collections.singletonMap("def", 3L)))); assertEquals(array, new JsonArray(Collections.singletonList(new JsonObject().put("def", 3)))); assertEquals(array, new JsonArray(Collections.singletonList(new JsonObject().put("def", 3L)))); assertNotEquals(array, new JsonArray(Collections.singletonList(Collections.singletonMap("def", 4)))); assertNotEquals(array, new JsonArray(Collections.singletonList(new JsonObject().put("def", 4)))); }
@Test public void testMapReplaceJsonObject() { testMapReplace(new JsonObject().put("foo", "bar"), new JsonObject().put("uihwqduh", "qiwiojw"), new JsonObject().put("regerg", "wfwef")); }
private void handleRoot(RoutingContext routingContext) { JsonObject result = new JsonObject(); result.put("welcome", "欢迎使用古诗词·一言"); result.put("api-document", "下面为本API可用的所有类型,使用时,在链接最后面加上 .svg / .txt / .json / .png 可以获得不同格式的输出"); result.put("help", "具体安装方法请访问项目首页 " + config().getString("index.url", "http://localhost/")); vertx.eventBus().<JsonArray>send(Key.GET_HELP_FROM_REDIS, null, res -> { if (res.succeeded()) { result.put("list", res.result().body()); returnJsonWithCache(routingContext, result); } else { routingContext.fail(res.cause()); } }); }
/** * 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; }