List of usage examples for io.vertx.core.json JsonObject put
public JsonObject put(String key, Object value)
From source file:de.openflorian.alarm.AlarmFaxEventMessageCodec.java
License:Open Source License
@Override public void encodeToWire(Buffer buffer, AlarmFaxEvent event) { JsonObject jsonToEncode = new JsonObject(); jsonToEncode.put(JSON_PROPERTY_FILENAME, event.getResultFile().getAbsolutePath()); // Encode object to string String jsonToStr = jsonToEncode.encode(); // Length of JSON: is NOT characters count int length = jsonToStr.getBytes().length; // Write data into given buffer buffer.appendInt(length);/*from w ww . j ava 2s .c om*/ buffer.appendString(jsonToStr); }
From source file:docoverride.json.Examples.java
License:Open Source License
public void example1() { JsonObject object = new JsonObject(); object.put("foo", "bar").put("num", 123).put("mybool", true); }
From source file:doug.iotdemo.server.web.WebServer.java
License:Open Source License
private void handleFetch(RoutingContext context) { ScanRequest scanRequest = new ScanRequest().withTableName(sensorTableName); db.scanAsync(scanRequest, new AsyncHandler<ScanRequest, ScanResult>() { @Override/*from w ww . j a v a2 s . c o m*/ public void onSuccess(ScanRequest request, ScanResult result) { JsonObject states = new JsonObject(); for (Map<String, AttributeValue> item : result.getItems()) { AttributeValue stateValue = item.get("state"); if (stateValue != null) { JsonObject sensorObj = new JsonObject(); sensorObj.put("state", Integer.valueOf(stateValue.getN())); if (item.get("time") != null) { long time = Long.parseLong(item.get("time").getN()); long count = Long.parseLong(item.get("count").getN()); long thresh = time / count; sensorObj.put("thresh", thresh); } states.put(item.get("sensor").getS(), sensorObj); } } context.response().end(states.encode()); } @Override public void onError(Exception exception) { StringWriter msg = new StringWriter(); exception.printStackTrace(new PrintWriter(msg)); context.response().setStatusCode(500).end(msg.toString()); } }); }
From source file:doug.iotdemo.server.web.WebServer.java
License:Open Source License
private void handlePut(RoutingContext context) { JsonObject msg = new JsonObject(); msg.put("sensor", context.request().getParam("sensor")); msg.put("state", Integer.valueOf(context.request().getParam("state"))); MqttMessage message = new MqttMessage(msg.toString().getBytes(StandardCharsets.UTF_8)); try {/* w w w . j a va 2s. c om*/ if (!mqtt.isConnected()) { mqtt.connect(this, new IMqttActionListener() { @Override public void onSuccess(IMqttToken arg0) { try { publishMessage(message, context); } catch (MqttException e) { throw new RuntimeException(e); } } @Override public void onFailure(IMqttToken arg0, Throwable t) { StringWriter msg = new StringWriter(); t.printStackTrace(new PrintWriter(msg)); context.response().setStatusCode(500).end(msg.toString()); } }); } else { publishMessage(message, context); } } catch (MqttException e) { throw new RuntimeException(e); } }
From source file:enmasse.kafka.bridge.converter.JsonMessageConverter.java
License:Apache License
@Override public ProducerRecord<String, byte[]> toKafkaRecord(String kafkaTopic, Message message) { Object partition = null, key = null; byte[] value = null; // root JSON//from www . jav a 2s .c om JsonObject json = new JsonObject(); // make JSON with AMQP properties JsonObject jsonProperties = new JsonObject(); if (message.getMessageId() != null) jsonProperties.put(JsonMessageConverter.MESSAGE_ID, message.getMessageId()); if (message.getAddress() != null) jsonProperties.put(JsonMessageConverter.TO, message.getAddress()); if (message.getSubject() != null) jsonProperties.put(JsonMessageConverter.SUBJECT, message.getSubject()); if (message.getReplyTo() != null) jsonProperties.put(JsonMessageConverter.REPLY_TO, message.getReplyTo()); if (message.getCorrelationId() != null) jsonProperties.put(JsonMessageConverter.CORRELATION_ID, message.getCorrelationId()); if (!jsonProperties.isEmpty()) json.put(JsonMessageConverter.PROPERTIES, jsonProperties); // make JSON with AMQP application properties ApplicationProperties applicationProperties = message.getApplicationProperties(); if (applicationProperties != null) { JsonObject jsonApplicationProperties = new JsonObject(); Map<String, Object> applicationPropertiesMap = (Map<String, Object>) applicationProperties.getValue(); for (Entry<String, Object> entry : applicationPropertiesMap.entrySet()) { jsonApplicationProperties.put(entry.getKey(), entry.getValue()); } json.put(JsonMessageConverter.APPLICATION_PROPERTIES, jsonApplicationProperties); } // get partition and key from AMQP message annotations // NOTE : they are not mandatory MessageAnnotations messageAnnotations = message.getMessageAnnotations(); if (messageAnnotations != null) { partition = messageAnnotations.getValue().get(Symbol.getSymbol(Bridge.AMQP_PARTITION_ANNOTATION)); key = messageAnnotations.getValue().get(Symbol.getSymbol(Bridge.AMQP_KEY_ANNOTATION)); if (partition != null && !(partition instanceof Integer)) throw new IllegalArgumentException("The partition annotation must be an Integer"); if (key != null && !(key instanceof String)) throw new IllegalArgumentException("The key annotation must be a String"); // make JSON with AMQP message annotations JsonObject jsonMessageAnnotations = new JsonObject(); Map<Symbol, Object> messageAnnotationsMap = (Map<Symbol, Object>) messageAnnotations.getValue(); for (Entry<Symbol, Object> entry : messageAnnotationsMap.entrySet()) { jsonMessageAnnotations.put(entry.getKey().toString(), entry.getValue()); } json.put(JsonMessageConverter.MESSAGE_ANNOTATIONS, jsonMessageAnnotations); } // get topic and body from AMQP message String topic = (message.getAddress() == null) ? kafkaTopic : message.getAddress().replace('/', '.'); Section body = message.getBody(); // check body null if (body != null) { JsonObject jsonBody = new JsonObject(); // section is AMQP value if (body instanceof AmqpValue) { jsonBody.put(JsonMessageConverter.SECTION_TYPE, JsonMessageConverter.SECTION_AMQP_VALUE_TYPE); Object amqpValue = ((AmqpValue) body).getValue(); // encoded as String if (amqpValue instanceof String) { String content = (String) ((AmqpValue) body).getValue(); jsonBody.put(JsonMessageConverter.SECTION, content); // encoded as a List } else if (amqpValue instanceof List) { List<?> list = (List<?>) ((AmqpValue) body).getValue(); JsonArray jsonArray = new JsonArray(list); jsonBody.put(JsonMessageConverter.SECTION, jsonArray); // encoded as an array } else if (amqpValue instanceof Object[]) { Object[] array = (Object[]) ((AmqpValue) body).getValue(); JsonArray jsonArray = new JsonArray(Arrays.asList(array)); jsonBody.put(JsonMessageConverter.SECTION, jsonArray); // encoded as a Map } else if (amqpValue instanceof Map) { Map<?, ?> map = (Map<?, ?>) ((AmqpValue) body).getValue(); value = map.toString().getBytes(); JsonObject jsonMap = new JsonObject((Map<String, Object>) map); jsonBody.put(JsonMessageConverter.SECTION, jsonMap); } // section is Data (binary) } else if (body instanceof Data) { Binary binary = (Binary) ((Data) body).getValue(); value = binary.getArray(); jsonBody.put(JsonMessageConverter.SECTION_TYPE, JsonMessageConverter.SECTION_DATA_TYPE); // put the section bytes as Base64 encoded string jsonBody.put(JsonMessageConverter.SECTION, Base64.getEncoder().encode(value)); } // put the body into the JSON root json.put(JsonMessageConverter.BODY, jsonBody); } // build the record for the KafkaProducer and then send it return new ProducerRecord<>(topic, (Integer) partition, (String) key, json.toString().getBytes()); }
From source file:eu.rethink.mn.component.AllocationManager.java
License:Apache License
@Override public void handle(PipeContext ctx) { final PipeMessage msg = ctx.getMessage(); final JsonObject body = msg.getBody(); if (msg.getType().equals("create")) { //process JSON msg requesting a number of available addresses final JsonObject msgBodyValue = body.getJsonObject("value"); final String scheme = body.getString("scheme"); int number = msgBodyValue.getInteger("number", 5); final List<String> allocated = allocate(ctx, scheme, number); final PipeMessage reply = new PipeMessage(); reply.setId(msg.getId());//from w w w . jav a 2s. c om reply.setFrom(name); reply.setTo(msg.getFrom()); reply.setReplyCode(ReplyCode.OK); final JsonObject value = new JsonObject(); value.put("allocated", new JsonArray(allocated)); reply.getBody().put("value", value); ctx.reply(reply); } else if (msg.getType().equals("delete")) { //process JSON msg releasing an address final String resource = body.getString("resource"); final JsonArray childrenResourcesList = body.getJsonArray("childrenResources"); if (resource != null) { deallocate(ctx, resource); } else { for (Object childrenResource : childrenResourcesList) { deallocate(ctx, childrenResource.toString()); } } ctx.replyOK(name); } }
From source file:eu.rethink.mn.component.HypertyAllocationManager.java
License:Apache License
@Override public void handle(PipeContext ctx) { final PipeMessage msg = ctx.getMessage(); if (msg.getType().equals("create")) { //process JSON msg requesting a number of available addresses final JsonObject msgBodyValue = msg.getBody().getJsonObject("value"); int number = msgBodyValue.getInteger("number", 5); final List<String> allocated = allocate(ctx, number); final PipeMessage reply = new PipeMessage(); reply.setId(msg.getId());/*from w ww .ja va 2 s. co m*/ reply.setFrom(name); reply.setTo(msg.getFrom()); reply.setReplyCode(ReplyCode.OK); final JsonObject value = new JsonObject(); value.put("allocated", new JsonArray(allocated)); reply.getBody().put("value", value); ctx.reply(reply); } else { //TODO: deallocate !? } }
From source file:eu.rethink.mn.component.ObjectAllocationManager.java
License:Apache License
@Override public void handle(PipeContext ctx) { final PipeMessage msg = ctx.getMessage(); final JsonObject body = msg.getBody(); if (msg.getType().equals("create")) { //process JSON msg requesting a number of available addresses final String scheme = body.getString("scheme"); //on value final JsonObject msgBodyValue = body.getJsonObject("value"); final int number = msgBodyValue.getInteger("number", 5); final List<String> allocated = allocate(ctx, scheme, number); final PipeMessage reply = new PipeMessage(); reply.setId(msg.getId());/*from w w w .j a v a 2s. c o m*/ reply.setFrom(name); reply.setTo(msg.getFrom()); reply.setReplyCode(ReplyCode.OK); final JsonObject value = new JsonObject(); value.put("allocated", new JsonArray(allocated)); reply.getBody().put("value", value); ctx.reply(reply); } else if (msg.getType().equals("delete")) { //process JSON msg releasing an address final String resource = body.getString("resource"); deallocate(ctx, resource); ctx.replyOK(name); } }
From source file:eventbusbridge.Main.java
License:Apache License
public static void main(String[] args) { final Vertx vertx = Vertx.vertx(); final EventBus eb = vertx.eventBus(); TcpEventBusBridge bridge = TcpEventBusBridge.create(vertx, new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddressRegex("test.*")) .addInboundPermitted(new PermittedOptions().setAddressRegex("test.*"))); final int port = Integer.parseInt(args[0]); final File semaphoreFile; if (args.length > 1) { semaphoreFile = new File(args[1]); } else {// ww w .java 2 s. c om semaphoreFile = null; } bridge.listen(port, res -> { System.out.println("Vert.x bridge started on " + port); if (semaphoreFile != null) { try { semaphoreFile.createNewFile(); } catch (IOException e) { System.err.println(e); } } vertx.setPeriodic(100, timer -> { //System.out.println("Sending the time..."); eb.publish("test.time", new JsonObject().put("now", System.currentTimeMillis())); eb.send("test.time-send", new JsonObject().put("now", System.currentTimeMillis())); }); vertx.eventBus().consumer("test.echo", m -> { //System.out.println("echo: " + m.body()); JsonObject reply = new JsonObject(); JsonObject headers = new JsonObject(); m.headers().forEach(e -> headers.put(e.getKey(), e.getValue())); reply.put("original-body", m.body()).put("original-headers", headers); //System.out.println("REPLY: " + m.headers() + " | " + reply); m.reply(reply); // send a copy to another address as well to test // non-replyable messages if (m.isSend()) { eb.send("test.echo.responses", reply); } else { eb.publish("test.echo.responses", reply); } }); vertx.eventBus().consumer("test.ping-pong", Main::pingPong); }); }
From source file:eventbusbridge.Main.java
License:Apache License
static void pingPong(final Message<JsonObject> m) { final int counter = m.body().getInteger("counter"); System.out.println("ping-pong: count is " + counter); final JsonObject reply = new JsonObject(); reply.put("counter", counter + 1); m.reply(reply, pingPongReply);// w w w .ja v a2 s. c o m }