Refine search
/** * Create options from JSON * * @param json the JSON */ public RequestOptions(JsonObject json) { setHost(json.getString("host", DEFAULT_HOST)); setPort(json.getInteger("port", DEFAULT_PORT)); setSsl(json.getBoolean("ssl", DEFAULT_SSL)); setURI(json.getString("uri", DEFAULT_URI)); }
@Override public CustomMessage decodeFromWire(int position, Buffer buffer) { // My custom message starting from this *position* of buffer int _pos = position; // Length of JSON int length = buffer.getInt(_pos); // Get JSON string by it`s length // Jump 4 because getInt() == 4 bytes String jsonStr = buffer.getString(_pos+=4, _pos+=length); JsonObject contentJson = new JsonObject(jsonStr); // Get fields int statusCode = contentJson.getInteger("statusCode"); String resultCode = contentJson.getString("resultCode"); String summary = contentJson.getString("summary"); // We can finally create custom message object return new CustomMessage(statusCode, resultCode, summary); }
@Test public void testCreateFromMapCharSequence() { Map<String, Object> map = new HashMap<>(); map.put("foo", "bar"); map.put("quux", 123); map.put("eeek", new StringBuilder("blah")); JsonObject obj = new JsonObject(map); assertEquals("bar", obj.getString("foo")); assertEquals(Integer.valueOf(123), obj.getInteger("quux")); assertEquals("blah", obj.getString("eeek")); assertSame(map, obj.getMap()); }
@Test public void testCreateFromMap() { Map<String, Object> map = new HashMap<>(); map.put("foo", "bar"); map.put("quux", 123); JsonObject obj = new JsonObject(map); assertEquals("bar", obj.getString("foo")); assertEquals(Integer.valueOf(123), obj.getInteger("quux")); assertSame(map, obj.getMap()); }
private void handleAddProduct(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); JsonObject product = routingContext.getBodyAsJson(); conn.updateWithParams("INSERT INTO products (name, price, weight) VALUES (?, ?, ?)", new JsonArray().add(product.getString("name")).add(product.getFloat("price")).add(product.getInteger("weight")), query -> { if (query.failed()) { sendError(500, response); } else { response.end(); } }); }
/** * 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(); } }
@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")); }
Map<String, Integer> countMap = new ConcurrentHashMap<>(); vertx.eventBus().<JsonObject>consumer("testcounts").handler((Message<JsonObject> msg) -> { countMap.put(msg.body().getString("deploymentID"), msg.body().getInteger("count")); }); CountDownLatch latch = new CountDownLatch(1);
jsonObject.put("obj", (Object)obj); jsonObject.put("arr", (Object)arr); assertEquals("bar", jsonObject.getString("str")); assertEquals(Integer.valueOf(123), jsonObject.getInteger("int")); assertEquals(Long.valueOf(123l), jsonObject.getLong("long")); assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float"));
@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))); }
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"); } } } }
/** * Create options from JSON * * @param json the JSON */ public RequestOptions(JsonObject json) { setHost(json.getString("host", DEFAULT_HOST)); setPort(json.getInteger("port", DEFAULT_PORT)); setSsl(json.getBoolean("ssl", DEFAULT_SSL)); setURI(json.getString("uri", DEFAULT_URI)); }
@Test public void testCreateFromMapCharSequence() { Map<String, Object> map = new HashMap<>(); map.put("foo", "bar"); map.put("quux", 123); map.put("eeek", new StringBuilder("blah")); JsonObject obj = new JsonObject(map); assertEquals("bar", obj.getString("foo")); assertEquals(Integer.valueOf(123), obj.getInteger("quux")); assertEquals("blah", obj.getString("eeek")); assertSame(map, obj.getMap()); }
@Test public void testCreateFromMap() { Map<String, Object> map = new HashMap<>(); map.put("foo", "bar"); map.put("quux", 123); JsonObject obj = new JsonObject(map); assertEquals("bar", obj.getString("foo")); assertEquals(Integer.valueOf(123), obj.getInteger("quux")); assertSame(map, obj.getMap()); }
@Override public void start() { // 读取配置文件 RedisOptions redisOptions = new RedisOptions() .setHost(config().getJsonObject("redis").getString("host","127.0.0.1")) .setPort(config().getJsonObject("redis").getInteger("port",6379)) .setSelect(config().getJsonObject("redis").getInteger("select",0)); // 配置 RuntimeError 错误记录 vertx.exceptionHandler(error -> log.error(error)); // 顺序部署 Verticle Future.<Void>succeededFuture() .compose(v -> Future.<String>future(s -> vertx.deployVerticle(new ApiVerticle(),new DeploymentOptions().setConfig(config()), s))) .compose(v -> Future.<String>future(s -> vertx.deployVerticle(new DataService(redisOptions), new DeploymentOptions().setConfig(config()),s))) .compose(v -> Future.<String>future(s -> vertx.deployVerticle(new LogService(redisOptions), s))) .compose(v -> log.info("Vert.x started successfully"), Future.future().setHandler(ar -> { if (ar.failed()) { log.error("Vert.x failed to start", ar.cause()); } })); } }
/** * 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.multiThreaded = json.getBoolean("multiThreaded", DEFAULT_MULTI_THREADED); 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(); } }
@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")); }
Map<String, Integer> countMap = new ConcurrentHashMap<>(); vertx.eventBus().<JsonObject>consumer("testcounts").handler((Message<JsonObject> msg) -> { countMap.put(msg.body().getString("deploymentID"), msg.body().getInteger("count")); }); CountDownLatch latch = new CountDownLatch(1);
jsonObject.put("obj", (Object)obj); jsonObject.put("arr", (Object)arr); assertEquals("bar", jsonObject.getString("str")); assertEquals(Integer.valueOf(123), jsonObject.getInteger("int")); assertEquals(Long.valueOf(123l), jsonObject.getLong("long")); assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float"));
@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))); }