Example usage for io.vertx.core.json JsonObject getValue

List of usage examples for io.vertx.core.json JsonObject getValue

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getValue.

Prototype

public Object getValue(String key) 

Source Link

Document

Get the value with the specified key, as an Object with types respecting the limitations of JSON.

Usage

From source file:com.reachauto.device.DeviceConverter.java

License:Apache License

public static void fromJson(JsonObject json, Device obj) {
    if (json.getValue("authInfo") instanceof String) {
        obj.setAuthInfo((String) json.getValue("authInfo"));
    }//  w w w.  j ava 2  s  . co m
    if (json.getValue("delFlag") instanceof String) {
        obj.setDelFlag((String) json.getValue("delFlag"));
    }
    if (json.getValue("desc") instanceof String) {
        obj.setDesc((String) json.getValue("desc"));
    }
    if (json.getValue("id") instanceof Number) {
        obj.setId(((Number) json.getValue("id")).intValue());
    }
    if (json.getValue("location") instanceof JsonObject) {
        obj.setLocation(new com.reachauto.device.Location((JsonObject) json.getValue("location")));
    }
    if (json.getValue("other") instanceof String) {
        obj.setOther((String) json.getValue("other"));
    }
    if (json.getValue("privat") instanceof Boolean) {
        obj.setPrivat((Boolean) json.getValue("privat"));
    }
    if (json.getValue("protocol") instanceof String) {
        obj.setProtocol(com.reachauto.device.ProtocolType.valueOf((String) json.getValue("protocol")));
    }
    if (json.getValue("tags") instanceof JsonArray) {
        java.util.ArrayList<java.lang.String> list = new java.util.ArrayList<>();
        json.getJsonArray("tags").forEach(item -> {
            if (item instanceof String)
                list.add((String) item);
        });
        obj.setTags(list);
    }
    if (json.getValue("title") instanceof String) {
        obj.setTitle((String) json.getValue("title"));
    }
}

From source file:com.reachauto.device.DeviceServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from www .  j  ava2 s  .  c om
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {
        case "addDevice": {
            service.addDevice(
                    json.getJsonObject("device") == null ? null
                            : new com.reachauto.device.Device(json.getJsonObject("device")),
                    createHandler(msg));
            break;
        }
        case "deleteDevice": {
            service.deleteDevice((java.lang.String) json.getValue("id"), createHandler(msg));
            break;
        }
        case "updateDevice": {
            service.updateDevice(json.getJsonObject("device") == null ? null
                    : new com.reachauto.device.Device(json.getJsonObject("device")), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            msg.reply(res.result() == null ? null : res.result().toJson());
                        }
                    });
            break;
        }
        case "retrieveDevice": {
            service.retrieveDevice((java.lang.String) json.getValue("id"), res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    msg.reply(res.result() == null ? null : res.result().toJson());
                }
            });
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.reachauto.device.LocationConverter.java

License:Apache License

public static void fromJson(JsonObject json, Location obj) {
    if (json.getValue("ele") instanceof Number) {
        obj.setEle(((Number) json.getValue("ele")).doubleValue());
    }//ww w. ja  va 2  s.co  m
    if (json.getValue("lat") instanceof Number) {
        obj.setLat(((Number) json.getValue("lat")).doubleValue());
    }
    if (json.getValue("lon") instanceof Number) {
        obj.setLon(((Number) json.getValue("lon")).doubleValue());
    }
}

From source file:com.reachauto.product.ProductConverter.java

License:Apache License

public static void fromJson(JsonObject json, Product obj) {
    if (json.getValue("account") instanceof JsonObject) {
        obj.setAccount(new com.reachauto.account.Account((JsonObject) json.getValue("account")));
    }/*  www. ja va2s.  c o m*/
    if (json.getValue("apiKey") instanceof String) {
        obj.setApiKey((String) json.getValue("apiKey"));
    }
    if (json.getValue("delFlag") instanceof String) {
        obj.setDelFlag((String) json.getValue("delFlag"));
    }
    if (json.getValue("id") instanceof Number) {
        obj.setId(((Number) json.getValue("id")).intValue());
    }
    if (json.getValue("name") instanceof String) {
        obj.setName((String) json.getValue("name"));
    }
}

From source file:com.reachauto.product.ProductServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {/*from  ww w .jav a  2s  . c  o m*/
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {
        case "addProduct": {
            service.addProduct(
                    json.getJsonObject("product") == null ? null
                            : new com.reachauto.product.Product(json.getJsonObject("product")),
                    createHandler(msg));
            break;
        }
        case "deleteProduct": {
            service.deleteProduct(json.getValue("id") == null ? null : (json.getLong("id").intValue()),
                    createHandler(msg));
            break;
        }
        case "updateProduct": {
            service.updateProduct(json.getJsonObject("product") == null ? null
                    : new com.reachauto.product.Product(json.getJsonObject("product")), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            msg.reply(res.result() == null ? null : res.result().toJson());
                        }
                    });
            break;
        }
        case "retrieveProduct": {
            service.retrieveProduct(json.getValue("id") == null ? null : (json.getLong("id").intValue()),
                    res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            msg.reply(res.result() == null ? null : res.result().toJson());
                        }
                    });
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.test.db.DbServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from   w  w w. j  a va 2 s.co  m
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "storedProc": {
            service.storedProc((java.lang.String) json.getValue("procName"),
                    (io.vertx.core.json.JsonObject) json.getValue("databaseConfig"), createHandler(msg));
            break;
        }
        case "createStm": {
            service.createStm((java.lang.String) json.getValue("query"),
                    (io.vertx.core.json.JsonObject) json.getValue("databaseConfig"), createHandler(msg));
            break;
        }
        case "update": {
            service.update((java.lang.String) json.getValue("query"),
                    (io.vertx.core.json.JsonObject) json.getValue("databaseConfig"), createHandler(msg));
            break;
        }
        case "delete": {
            service.delete((java.lang.String) json.getValue("query"),
                    (io.vertx.core.json.JsonObject) json.getValue("databaseConfig"), createHandler(msg));
            break;
        }
        case "read": {
            service.read((java.lang.String) json.getValue("query"),
                    (io.vertx.core.json.JsonObject) json.getValue("databaseConfig"), createHandler(msg));
            break;
        }
        case "nonSharedRead": {
            service.nonSharedRead((java.lang.String) json.getValue("query"),
                    (io.vertx.core.json.JsonObject) json.getValue("databaseConfig"), createHandler(msg));
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.fail(-1, t.getMessage());
        throw t;
    }
}

From source file:com.test.mailer.MailerServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//  w  ww.  jav  a 2  s. c o m
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "sendAttachment": {
            service.sendAttachment((java.lang.String) json.getValue("To"),
                    json.getJsonObject("attachment") == null ? null
                            : new io.vertx.ext.mail.MailAttachment(json.getJsonObject("attachment")),
                    (java.lang.String) json.getValue("Title"), createHandler(msg));
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.weeaar.vertxwebconfig.codec.response.HttpResponseConverter.java

License:Apache License

public static void fromJson(JsonObject json, HttpResponse obj) {
    if (json.getValue("body") instanceof String) {
        obj.setBody((String) json.getValue("body"));
    }/*from  w  ww . j a va  2  s  . com*/
    if (json.getValue("statusCode") instanceof Number) {
        obj.setStatusCode(((Number) json.getValue("statusCode")).intValue());
    }
}

From source file:enmasse.kafka.bridge.converter.JsonMessageConverter.java

License:Apache License

@Override
public Message toAmqpMessage(String amqpAddress, ConsumerRecord<String, byte[]> record) {

    Message message = Proton.message();/*from  w  ww.  j  a va2s  .  co  m*/
    message.setAddress(amqpAddress);

    // get the root JSON
    JsonObject json = new JsonObject(new String(record.value()));

    // get AMQP properties from the JSON
    JsonObject jsonProperties = json.getJsonObject(JsonMessageConverter.PROPERTIES);
    if (jsonProperties != null) {

        for (Entry<String, Object> entry : jsonProperties) {

            if (entry.getValue() != null) {

                if (entry.getKey().equals(JsonMessageConverter.MESSAGE_ID)) {
                    message.setMessageId(entry.getValue());
                } else if (entry.getKey().equals(JsonMessageConverter.TO)) {
                    message.setAddress(entry.getValue().toString());
                } else if (entry.getKey().equals(JsonMessageConverter.SUBJECT)) {
                    message.setSubject(entry.getValue().toString());
                } else if (entry.getKey().equals(JsonMessageConverter.REPLY_TO)) {
                    message.setReplyTo(entry.getValue().toString());
                } else if (entry.getKey().equals(JsonMessageConverter.CORRELATION_ID)) {
                    message.setCorrelationId(entry.getValue());
                }
            }
        }
    }

    // get AMQP application properties from the JSON
    JsonObject jsonApplicationProperties = json.getJsonObject(JsonMessageConverter.APPLICATION_PROPERTIES);
    if (jsonApplicationProperties != null) {

        Map<Symbol, Object> applicationPropertiesMap = new HashMap<>();

        for (Entry<String, Object> entry : jsonApplicationProperties) {
            applicationPropertiesMap.put(Symbol.valueOf(entry.getKey()), entry.getValue());
        }

        ApplicationProperties applicationProperties = new ApplicationProperties(applicationPropertiesMap);
        message.setApplicationProperties(applicationProperties);
    }

    // put message annotations about partition, offset and key (if not null)
    Map<Symbol, Object> messageAnnotationsMap = new HashMap<>();
    messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_PARTITION_ANNOTATION), record.partition());
    messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_OFFSET_ANNOTATION), record.offset());
    if (record.key() != null)
        messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_KEY_ANNOTATION), record.key());
    messageAnnotationsMap.put(Symbol.valueOf(Bridge.AMQP_TOPIC_ANNOTATION), record.topic());

    // get AMQP message annotations from the JSON
    JsonObject jsonMessageAnnotations = json.getJsonObject(JsonMessageConverter.MESSAGE_ANNOTATIONS);
    if (jsonMessageAnnotations != null) {

        for (Entry<String, Object> entry : jsonMessageAnnotations) {
            messageAnnotationsMap.put(Symbol.valueOf(entry.getKey()), entry.getValue());
        }
    }

    MessageAnnotations messageAnnotations = new MessageAnnotations(messageAnnotationsMap);
    message.setMessageAnnotations(messageAnnotations);

    // get the AMQP message body from the JSON
    JsonObject jsonBody = json.getJsonObject(JsonMessageConverter.BODY);

    if (jsonBody != null) {

        // type attribtute for following sectin : AMQP value or raw data/binary
        String type = jsonBody.getString(JsonMessageConverter.SECTION_TYPE);

        if (type.equals(JsonMessageConverter.SECTION_AMQP_VALUE_TYPE)) {

            // section is an AMQP value
            Object jsonSection = jsonBody.getValue(JsonMessageConverter.SECTION);

            // encoded as String
            if (jsonSection instanceof String) {
                message.setBody(new AmqpValue(jsonSection));
                // encoded as an array/List
            } else if (jsonSection instanceof JsonArray) {
                JsonArray jsonArray = (JsonArray) jsonSection;
                message.setBody(new AmqpValue(jsonArray.getList()));
                // encoded as a Map
            } else if (jsonSection instanceof JsonObject) {
                JsonObject jsonObject = (JsonObject) jsonSection;
                message.setBody(new AmqpValue(jsonObject.getMap()));
            }

        } else if (type.equals(JsonMessageConverter.SECTION_DATA_TYPE)) {

            // section is a raw binary data

            // get the section from the JSON (it's base64 encoded)
            byte[] value = jsonBody.getBinary(JsonMessageConverter.SECTION);

            message.setBody(new Data(new Binary(Base64.getDecoder().decode(value))));
        }
    }

    return message;
}

From source file:examples.VertxAmqpBridgeExamples.java

License:Apache License

public void example2(Vertx vertx) {
    AmqpBridge bridge = AmqpBridge.create(vertx);
    // Start the bridge, then use the event loop thread to process things thereafter.
    bridge.start("localhost", 5672, res -> {
        // Set up a consumer using the bridge, register a handler for it.
        MessageConsumer<JsonObject> consumer = bridge.createConsumer("myAmqpAddress");
        consumer.handler(vertxMsg -> {
            JsonObject amqpMsgPayload = vertxMsg.body();
            Object amqpBody = amqpMsgPayload.getValue("body");

            System.out.println("Received a message with body: " + amqpBody);
        });/*from   w ww  .  j  a v  a  2 s  . c o  m*/
    });
}