/** * Like {@link #getJsonArray(String)} but specifying a default value to return if there is no entry. * * @param key the key to lookup * @param def the default value to use if the entry is not present * @return the value or {@code def} if no entry present */ public JsonArray getJsonArray(String key, JsonArray def) { JsonArray val = getJsonArray(key); return val != null || map.containsKey(key) ? val : def; }
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); } }
public void removeFromHA(String depID) { Deployment dep = deploymentManager.getDeployment(depID); if (dep == null || !dep.deploymentOptions().isHa()) { return; } synchronized (haInfo) { JsonArray haMods = haInfo.getJsonArray("verticles"); Iterator<Object> iter = haMods.iterator(); while (iter.hasNext()) { Object obj = iter.next(); JsonObject mod = (JsonObject) obj; if (mod.getString("dep_id").equals(depID)) { iter.remove(); } } clusterMap.put(nodeID, haInfo.encode()); } }
/** * Initialise the fields of this instance from the specified JSON * * @param json the JSON */ public void fromJson(JsonObject json) { this.config = json.getJsonObject("config"); this.worker = json.getBoolean("worker", DEFAULT_WORKER); this.isolationGroup = json.getString("isolationGroup", DEFAULT_ISOLATION_GROUP); this.ha = json.getBoolean("ha", DEFAULT_HA); JsonArray arr = json.getJsonArray("extraClasspath", null); if (arr != null) { this.extraClasspath = arr.getList(); } this.instances = json.getInteger("instances", DEFAULT_INSTANCES); JsonArray arrIsolated = json.getJsonArray("isolatedClasses", null); if (arrIsolated != null) { this.isolatedClasses = arrIsolated.getList(); } }
private void testStreamCorrectTypes(JsonObject object) { object.getJsonArray("object1").stream().forEach(innerMap -> { assertTrue("Expecting JsonObject, found: " + innerMap.getClass().getCanonicalName(), innerMap instanceof JsonObject); }); }
@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)); }
@Test public void testCreateFromMapNestedJsonArray() { Map<String, Object> map = new HashMap<>(); JsonArray nestedArr = new JsonArray().add("foo"); map.put("nested", nestedArr); JsonObject obj = new JsonObject(map); JsonArray nestedRetrieved = obj.getJsonArray("nested"); assertEquals("foo", nestedRetrieved.getString(0)); }
@Test public void testGetJsonArray() { JsonArray arr = new JsonArray().add("blah").add("wibble"); jsonObject.put("foo", arr); assertEquals(arr, jsonObject.getJsonArray("foo")); jsonObject.put("foo", "hello"); try { jsonObject.getJsonArray("foo"); fail(); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getJsonArray("foo")); assertNull(jsonObject.getJsonArray("absent")); try { jsonObject.getJsonArray(null); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testGetJsonArrayDefault() { JsonArray arr = new JsonArray().add("blah").add("wibble"); JsonArray def = new JsonArray().add("quux").add("eek"); jsonObject.put("foo", arr); assertEquals(arr, jsonObject.getJsonArray("foo", def)); assertEquals(arr, jsonObject.getJsonArray("foo", null)); jsonObject.put("foo", "hello"); try { jsonObject.getJsonArray("foo", def); fail(); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getJsonArray("foo", def)); assertEquals(def, jsonObject.getJsonArray("absent", def)); assertNull(jsonObject.getJsonArray("foo", null)); assertNull(jsonObject.getJsonArray("absent", null)); try { jsonObject.getJsonArray(null, null); fail(); } catch (NullPointerException e) { // OK } }
private void checkFailover(String failedNodeID, JsonObject theHAInfo) { try { JsonArray deployments = theHAInfo.getJsonArray("verticles"); String group = theHAInfo.getString("group"); String chosen = chooseHashedNode(group, failedNodeID.hashCode()); if (chosen != null && chosen.equals(this.nodeID)) { if (deployments != null && deployments.size() != 0) { log.info("node" + nodeID + " says: Node " + failedNodeID + " has failed. This node will deploy " + deployments.size() + " deploymentIDs from that node."); for (Object obj: deployments) { JsonObject app = (JsonObject)obj; processFailover(app); } } // Failover is complete! We can now remove the failed node from the cluster map clusterMap.remove(failedNodeID); runOnContextAndWait(() -> { if (failoverCompleteHandler != null) { failoverCompleteHandler.handle(failedNodeID, theHAInfo, true); } }); } } catch (Throwable t) { log.error("Failed to handle failover", t); runOnContextAndWait(() -> { if (failoverCompleteHandler != null) { failoverCompleteHandler.handle(failedNodeID, theHAInfo, false); } }); } }
@Test public void testPutJsonArray() { JsonArray obj1 = new JsonArray().add("parp"); JsonArray obj2 = new JsonArray().add("fleep"); JsonArray obj3 = new JsonArray().add("woob"); assertSame(jsonObject, jsonObject.put("foo", obj1)); assertEquals(obj1, jsonObject.getJsonArray("foo")); jsonObject.put("quux", obj2); assertEquals(obj2, jsonObject.getJsonArray("quux")); assertEquals(obj1, jsonObject.getJsonArray("foo")); jsonObject.put("foo", obj3); assertEquals(obj3, jsonObject.getJsonArray("foo")); jsonObject.put("foo", (JsonArray) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, new JsonArray()); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testToJsonObject() { Map<String, Object> map = new HashMap<>(); map.put("string", "the_string"); map.put("integer", 4); map.put("boolean", true); map.put("charsequence", new AsciiString("the_charsequence")); map.put("biginteger", new BigInteger("1234567")); map.put("binary", Buffer.buffer("hello")); map.put("object", Collections.singletonMap("nested", 4)); map.put("array", Arrays.asList(1, 2, 3)); JsonObject json = (JsonObject) ConversionHelper.toObject(map); assertEquals(8, json.size()); assertEquals("the_string", json.getString("string")); assertEquals(4, (int)json.getInteger("integer")); assertEquals(true, json.getBoolean("boolean")); assertEquals("the_charsequence", json.getString("charsequence")); assertEquals(1234567, (int)json.getInteger("biginteger")); assertEquals("hello", new String(json.getBinary("binary"))); assertEquals(new JsonObject().put("nested", 4), json.getJsonObject("object")); assertEquals(new JsonArray().add(1).add(2).add(3), json.getJsonArray("array")); }
assertEquals(now.toString(), jsonObject.getValue("instant")); assertEquals(obj, jsonObject.getJsonObject("obj")); assertEquals(arr, jsonObject.getJsonArray("arr")); try { jsonObject.put("inv", new SomeClass());
@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")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("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))); }
/** * Like {@link #getJsonArray(String)} but specifying a default value to return if there is no entry. * * @param key the key to lookup * @param def the default value to use if the entry is not present * @return the value or {@code def} if no entry present */ public JsonArray getJsonArray(String key, JsonArray def) { JsonArray val = getJsonArray(key); return val != null || map.containsKey(key) ? val : def; }
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); } }
public SockJSHandlerOptions(JsonObject json) { this.sessionTimeout = json.getLong("sessionTimeout", DEFAULT_SESSION_TIMEOUT); this.insertJSESSIONID = json.getBoolean("insertJSESSIONID", DEFAULT_INSERT_JSESSIONID); this.heartbeatInterval = json.getLong("heartbeatInterval", DEFAULT_HEARTBEAT_INTERVAL); this.maxBytesStreaming = json.getInteger("maxBytesStreaming", DEFAULT_MAX_BYTES_STREAMING); this.libraryURL = json.getString("libraryURL", DEFAULT_LIBRARY_URL); JsonArray arr = json.getJsonArray("disabledTransports"); if (arr != null) { for (Object str : arr) { if (str instanceof String) { String sstr = (String) str; disabledTransports.add(sstr); } else { throw new IllegalArgumentException("Invalid type " + str.getClass() + " in disabledTransports array"); } } } }
private void testStreamCorrectTypes(JsonObject object) { object.getJsonArray("object1").stream().forEach(innerMap -> { assertTrue("Expecting JsonObject, found: " + innerMap.getClass().getCanonicalName(), innerMap instanceof JsonObject); }); }
@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)); }
@Test public void testCreateFromMapNestedJsonArray() { Map<String, Object> map = new HashMap<>(); JsonArray nestedArr = new JsonArray().add("foo"); map.put("nested", nestedArr); JsonObject obj = new JsonObject(map); JsonArray nestedRetrieved = obj.getJsonArray("nested"); assertEquals("foo", nestedRetrieved.getString(0)); }