Example usage for io.vertx.core.json JsonArray JsonArray

List of usage examples for io.vertx.core.json JsonArray JsonArray

Introduction

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

Prototype

public JsonArray(Buffer buf) 

Source Link

Document

Create an instance from a Buffer of JSON.

Usage

From source file:com.englishtown.vertx.elasticsearch.ElasticSearchServiceVertxEBProxy.java

License:Apache License

public void search(List<String> indices, SearchOptions options,
        Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*  w  w  w.j  av a 2  s .  c  om*/
    }
    JsonObject _json = new JsonObject();
    _json.put("indices", new JsonArray(indices));
    _json.put("options", options == null ? null : options.toJson());
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "search");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

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

License:Apache License

public static void toJson(Device obj, JsonObject json) {
    if (obj.getAuthInfo() != null) {
        json.put("authInfo", obj.getAuthInfo());
    }/*from   ww  w  . j  a v  a  2  s. co m*/
    if (obj.getDelFlag() != null) {
        json.put("delFlag", obj.getDelFlag());
    }
    if (obj.getDesc() != null) {
        json.put("desc", obj.getDesc());
    }
    if (obj.getId() != null) {
        json.put("id", obj.getId());
    }
    if (obj.getOther() != null) {
        json.put("other", obj.getOther());
    }
    if (obj.getPrivat() != null) {
        json.put("privat", obj.getPrivat());
    }
    if (obj.getProtocol() != null) {
        json.put("protocol", obj.getProtocol().name());
    }
    if (obj.getTags() != null) {
        json.put("tags", new JsonArray(
                obj.getTags().stream().map(item -> item).collect(java.util.stream.Collectors.toList())));
    }
    if (obj.getTitle() != null) {
        json.put("title", obj.getTitle());
    }
}

From source file:de.braintags.netrelay.typehandler.HttpGeoPointTypeHandler.java

License:Open Source License

private String encode(GeoPoint source) {
    return new JsonArray(source.getCoordinates().getValues()).encode();
}

From source file:de.braintags.netrelay.typehandler.HttpGeoPointTypeHandler.java

License:Open Source License

private GeoPoint parse(String source) {
    JsonArray js = new JsonArray(source);
    Position pos = new Position(js.iterator());
    return new GeoPoint(pos);
}

From source file:docoverride.json.Examples.java

License:Open Source License

public void example0_2() {
    String jsonString = "[\"foo\",\"bar\"]";
    JsonArray array = new JsonArray(jsonString);
}

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   w w w  . j  a va 2s .  co m
    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 ww w. j  ava 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 w  w  .  ja v a 2s.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());/*www .  j a v a 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 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:fr.pjthin.vertx.client.UserDaoVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {// w  w  w.j a v a 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 "save": {
            service.save(
                    json.getJsonObject("newUser") == null ? null
                            : new fr.pjthin.vertx.client.data.User(json.getJsonObject("newUser")),
                    createHandler(msg));
            break;
        }
        case "findAll": {
            service.findAll(res -> {
                if (res.failed()) {
                    msg.fail(-1, res.cause().getMessage());
                } else {
                    msg.reply(new JsonArray(
                            res.result().stream().map(User::toJson).collect(Collectors.toList())));
                }
            });
            break;
        }
        case "findUserByLogin": {
            service.findUserByLogin((java.lang.String) json.getValue("login"), res -> {
                if (res.failed()) {
                    msg.fail(-1, res.cause().getMessage());
                } else {
                    msg.reply(res.result() == null ? null : res.result().toJson());
                }
            });
            break;
        }
        case "deleteByLogin": {
            service.deleteByLogin((java.lang.String) json.getValue("login"), createHandler(msg));
            break;
        }
        case "close": {
            service.close();
            close();
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.fail(-1, t.getMessage());
        throw t;
    }
}