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

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

Introduction

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

Prototype

public JsonObject() 

Source Link

Document

Create a new, empty instance

Usage

From source file:com.nasa.ElasticSearchAdapter.java

public JsonObject postFeedback(JsonObject feedback) throws IOException {
    JsonObject result = new JsonObject();
    String data = feedback.toString();
    // normalize or remove non ascii characters
    data = Normalizer.normalize(data, Normalizer.Form.NFD);
    data = data.replaceAll("[^\\x00-\\x7F]", "");
    JsonObject response = doPost(ES_URL + "/healthy-travel/feedback", data);

    result.put("success", response.getJsonObject("_shards").getInteger("successful") > 0);

    return result;
}

From source file:com.nasa.ESWorkerVerticle.java

@Override
public void start() throws Exception {

    EventBus eb = vertx.eventBus();/* w  ww. j  av  a2  s .c  om*/

    eb.consumer("bus.symptoms").handler(message -> {

        String text = ((JsonObject) message.body()).getString("text");

        JsonObject result = null;

        try {
            if (text != null) {
                result = es.getSymptoms(text);
            }
        } catch (Exception ex) {
            result = new JsonObject();
            result.put("symptoms", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.conditions").handler(message -> {

        String text = ((JsonObject) message.body()).getString("text");

        JsonObject result = null;

        try {
            if (text != null) {
                result = es.getConditions(text);
            }
        } catch (Exception ex) {
            result = new JsonObject();
            result.put("conditions", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.map.weather").handler(message -> {

        String lat = ((JsonObject) message.body()).getString("lat");
        String lon = ((JsonObject) message.body()).getString("lon");
        String zoom = ((JsonObject) message.body()).getString("zoom");
        String count = ((JsonObject) message.body()).getString("count");

        JsonObject result = null;

        try {
            if (lat != null && lon != null && zoom != null) {
                result = Transformer.weatherToMap(es.getMap(lat, lon, zoom, count));
            } else {
                throw new IllegalArgumentException("incorrect weather params: " + message.body().toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new JsonObject();
            result.put("type", "FeatureCollection");
            result.put("features", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.map.pollution").handler(message -> {

        String lat = ((JsonObject) message.body()).getString("lat");
        String lon = ((JsonObject) message.body()).getString("lon");
        String zoom = ((JsonObject) message.body()).getString("zoom");
        String count = ((JsonObject) message.body()).getString("count");

        System.out.println("Worker: " + message.body());

        JsonObject result = null;

        try {
            if (lat != null && lon != null && zoom != null) {
                result = Transformer.pollutionToMap(es.getMap(lat, lon, zoom, count));
            } else {
                throw new IllegalArgumentException("incorrect pollution params: " + message.body().toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new JsonObject();
            result.put("type", "FeatureCollection");
            result.put("features", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.map.condition").handler(message -> {

        String lat = ((JsonObject) message.body()).getString("lat");
        String lon = ((JsonObject) message.body()).getString("lon");
        String zoom = ((JsonObject) message.body()).getString("zoom");
        String count = ((JsonObject) message.body()).getString("count");

        JsonObject result = null;

        try {
            if (lat != null && lon != null && zoom != null) {
                result = Transformer.conditionToMap(es.getMap(lat, lon, zoom, count));
            } else {
                throw new IllegalArgumentException("incorrect condition params: " + message.body().toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new JsonObject();
            result.put("type", "FeatureCollection");
            result.put("features", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.check").handler(message -> {

        Float lat = Float.parseFloat(((JsonObject) message.body()).getString("lat"));
        Float lon = Float.parseFloat(((JsonObject) message.body()).getString("lon"));

        JsonObject result = new JsonObject();

        Gson gson = new Gson();

        try {
            JsonArray matchesArr = new JsonArray();

            if (lat != null && lon != null) {
                matchesArr = es.getCheck(lat, lon);
                result.put("conditions", matchesArr);
            }

            OtherOmwClient owm = new OtherOmwClient();
            BreezoMeterClient bm = new BreezoMeterClient();

            JsonObject weatherJ = owm.currentWeather(lat, lon);
            JsonObject pollutionJ = bm.currentAirQualityAtPoint(lat, lon);

            Weather weather = owm.toWeather(weatherJ);
            if (weather != null) {
                result.put("weather", new JsonObject(gson.toJson(weather)));
            }

            Pollution pollution = bm.toPollution(pollutionJ);
            if (pollution != null) {
                result.put("pollution", new JsonObject(gson.toJson(pollution)));
            }

        } catch (Exception ex) {
            result = new JsonObject();
            result.put("conditions", new JsonArray());
            result.put("weather", new JsonObject());
            result.put("pollution", new JsonObject());
        }

        message.reply(result);
    });

    eb.consumer("bus.feedback").handler(message -> {

        JsonObject userFeedback = (JsonObject) message.body();

        JsonObject result = new JsonObject();

        try {
            Gson gson = new Gson();
            Feedback feedback = gson.fromJson(userFeedback.toString(), Feedback.class);

            float lat = feedback.getLocation().getLat();
            float lon = feedback.getLocation().getLon();

            OtherOmwClient owm = new OtherOmwClient();
            JsonObject owmData = owm.currentWeather(lat, lon);
            feedback.setWeather(owm.toWeather(owmData));

            BreezoMeterClient bm = new BreezoMeterClient();
            JsonObject bmData = bm.currentAirQualityAtPoint(lat, lon);
            feedback.setPollution(bm.toPollution(bmData));

            result = es.postFeedback(new JsonObject(gson.toJson(feedback)));
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("success", false);
        }

        message.reply(result);
    });
}

From source file:com.nasa.Transformer.java

public static JsonObject weatherToMap(JsonObject jsonObject) {
    JsonObject result = new JsonObject();
    JsonArray featuresArr = new JsonArray();

    JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits");

    int idx = 0;//from  ww w  .j av a  2  s  .  c o  m
    for (Object hit : items) {
        JsonObject source = ((JsonObject) hit).getJsonObject("_source");

        JsonObject feature = new JsonObject();
        feature.put("type", "Feature");
        feature.put("id", idx);

        JsonObject properties = new JsonObject();
        float mag = source.getJsonObject("weather").getFloat("temp");
        properties.put("mag", mag);

        feature.put("properties", properties);

        JsonObject geometry = new JsonObject();
        geometry.put("type", "Point");

        JsonArray coordinates = new JsonArray();
        Float lon = source.getJsonObject("location").getFloat("lon");
        Float lat = source.getJsonObject("location").getFloat("lat");
        coordinates.add(lon);
        coordinates.add(lat);

        geometry.put("coordinates", coordinates);
        feature.put("geometry", geometry);

        featuresArr.add(feature);

        idx++;
    }

    result.put("type", "FeatureCollection");
    result.put("features", featuresArr);

    return result;
}

From source file:com.nasa.Transformer.java

public static JsonObject pollutionToMap(JsonObject jsonObject) {
    JsonObject result = new JsonObject();
    JsonArray featuresArr = new JsonArray();

    JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits");

    int idx = 0;/*from   w w w .  j  a  va  2  s.  c  o m*/
    for (Object hit : items) {
        JsonObject source = ((JsonObject) hit).getJsonObject("_source");

        JsonObject feature = new JsonObject();
        feature.put("type", "Feature");
        feature.put("id", idx);

        JsonObject properties = new JsonObject();
        float mag = source.getJsonObject("pollution").getFloat("aqi");
        properties.put("mag", mag);

        feature.put("properties", properties);

        JsonObject geometry = new JsonObject();
        geometry.put("type", "Point");

        JsonArray coordinates = new JsonArray();
        Float lon = source.getJsonObject("location").getFloat("lon");
        Float lat = source.getJsonObject("location").getFloat("lat");
        coordinates.add(lon);
        coordinates.add(lat);

        geometry.put("coordinates", coordinates);
        feature.put("geometry", geometry);

        featuresArr.add(feature);

        idx++;
    }

    result.put("type", "FeatureCollection");
    result.put("features", featuresArr);

    return result;
}

From source file:com.nasa.Transformer.java

public static JsonObject conditionToMap(JsonObject jsonObject) {

    //        private String conditionsMap = "{\n"
    //            + "  \"Diseases\": [\n"
    //            + "    {\n"
    //            + "      \"lat\": 46.77029284,\n"
    //            + "      \"lng\": 23.57641889,\n"
    //            + "      \"type\": \"something good\",\n"
    //            + "      \"description\": \"good\",\n"
    //            + "      \"rating\": 1.23\n"
    //            + "    },\n"
    //            + "    {\n"
    //            + "      \"lat\": 46.78185201,\n"
    //            + "      \"lng\": 23.68522613,\n"
    //            + "      \"type\": \"something not bad\",\n"
    //            + "      \"description\": \"not bad\",\n"
    //            + "      \"rating\": 3.99\n"
    //            + "    },\n"
    //            + "    {\n"
    //            + "      \"lat\": 46.7558097,\n"
    //            + "      \"lng\": 23.5940353,\n"
    //            + "      \"type\": \"something bad\",\n"
    //            + "      \"description\": \"bad\",\n"
    //            + "      \"rating\": 4.75\n"
    //            + "    }\n"
    //            + "  ]\n"
    //            + "}";

    JsonObject result = new JsonObject();
    JsonArray featuresArr = new JsonArray();

    JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits");

    for (Object hit : items) {
        JsonObject source = ((JsonObject) hit).getJsonObject("_source");

        JsonObject feature = new JsonObject();

        Float lon = source.getJsonObject("location").getFloat("lon");
        Float lat = source.getJsonObject("location").getFloat("lat");
        feature.put("lat", lat);
        feature.put("lng", lon);

        feature.put("type", source.getString("condition"));

        String symptoms = "";
        int ratingSum = 0;
        int ratingCount = 0;

        JsonArray symptomsJ = source.getJsonArray("symptoms");

        for (Object sym : symptomsJ) {
            JsonObject symJ = (JsonObject) sym;
            symptoms += symJ.getString("name") + ", ";
            ratingSum += symJ.getInteger("rating");
            ratingCount++;/*from ww  w. j a v  a  2s. c o m*/
        }

        feature.put("description", symptoms);

        if (ratingCount > 0) {
            feature.put("rating", ratingSum / ratingCount);
        } else {
            feature.put("rating", 0);
        }

        featuresArr.add(feature);
    }

    result.put("Diseases", featuresArr);

    return result;
}

From source file:com.organization.project.service.BaseSQLClient.java

License:Open Source License

public JsonObject executeQuerySuper(String queryString, SQLConnection sqlConnection, JsonArray jsonArray,
        OperationQuery type) {/*from w  w  w . j  av  a 2  s . c om*/
    jsonObject = new JsonObject();
    jsonArrays = new ArrayList<>();

    switch (type) {
    case SELECT:
        if (jsonArray == null) {
            getSelectedQuery(queryString, sqlConnection);
        } else {
            getSelectedQueryWithParams(queryString, jsonArray, sqlConnection);
        }
    case UPDATE:
        if (jsonArray == null) {
            getUpdatedQuery(queryString, sqlConnection);
        } else {
            getUpdatedQueryWithParams(queryString, jsonArray, sqlConnection);
        }
    case DELETE:
        if (jsonArray == null) {
            getUpdatedQuery(queryString, sqlConnection);
        } else {
            getUpdatedQueryWithParams(queryString, jsonArray, sqlConnection);
        }
    default:
        break;
    }

    return jsonObject;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService save(JsonObject payload, Handler<AsyncResult<JsonArray>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }//from   w  w  w  .ja  v a2s.  c om
    JsonObject _json = new JsonObject();
    _json.put("payload", payload);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "save");
    _vertx.eventBus().<JsonArray>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService query(String sql, Handler<AsyncResult<List<JsonObject>>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/* w  ww .ja  v  a 2 s  .c  o  m*/
    JsonObject _json = new JsonObject();
    _json.put("sql", sql);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "query");
    _vertx.eventBus().<JsonArray>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(convertList(res.result().body().getList())));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService queryOne(String sql, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*w w w .ja  va 2 s .  com*/
    JsonObject _json = new JsonObject();
    _json.put("sql", sql);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "queryOne");
    _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()));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService one(Long id, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }// w  w w. j  a va  2s. c o  m
    JsonObject _json = new JsonObject();
    _json.put("id", id);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "one");
    _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()));
        }
    });
    return this;
}