Example usage for javax.json JsonArrayBuilder add

List of usage examples for javax.json JsonArrayBuilder add

Introduction

In this page you can find the example usage for javax.json JsonArrayBuilder add.

Prototype

JsonArrayBuilder add(JsonArrayBuilder builder);

Source Link

Document

Adds a JsonArray from an array builder to the array.

Usage

From source file:be.fedict.dcat.datagovbe.Drupal.java

/**
 * Create a JsonArrayBuilder with a list of Drupal URL fields
 * // w  w w.j  ava2  s .  c o  m
 * @param l list of URLs (as string)
 * @return JsonArrayBuilder containing URL field JSON objects
 */
private static JsonArrayBuilder urlArrayJson(Collection<String> l) {
    JsonArrayBuilder builder = Json.createArrayBuilder();
    for (String s : l) {
        builder.add(Json.createObjectBuilder().add(Drupal.URL, s));
    }
    return builder;
}

From source file:be.fedict.dcat.datagovbe.Drupal.java

/**
 * Create a JsonArrayBuilder with a list of Drupal text fields
 * /* w  w  w  .j av  a2s.  c  o m*/
 * @param l list of URLs (as string)
 * @return JsonArrayBuilder containing text fields JSON objects
 */
private static JsonArrayBuilder fieldArrayJson(List<String> l) {
    JsonArrayBuilder builder = Json.createArrayBuilder();
    for (String s : l) {
        builder.add(Json.createObjectBuilder().add(Drupal.VALUE, s));
    }
    return builder;
}

From source file:be.fedict.dcat.datagovbe.Drupal.java

/**
 * Get Drupal taxonomy terms as JSON Array
 *
 * @param terms/*w w w .j a  v a 2  s . c o m*/
 * @return JsonArrayBuilder
 */
private static JsonArrayBuilder arrayTermsJson(Collection<String> terms) {
    JsonArrayBuilder arr = Json.createArrayBuilder();

    for (String term : terms) {
        if (term.startsWith(Drupal.TAXO_PREFIX)) {
            String id = term.substring(term.lastIndexOf('/') + 1);
            arr.add(Json.createObjectBuilder().add(Drupal.ID, id).build());
        }
    }
    return arr;
}

From source file:org.piwik.java.tracking.PiwikTracker.java

/**
 * Send multiple requests in a single HTTP call.  More efficient than sending
 * several individual requests.  Specify the AuthToken if parameters that require
 * an auth token is used.//  ww w .j  a v  a  2 s  .  co  m
 * @param requests the requests to send
 * @param authToken specify if any of the parameters use require AuthToken
 * @return the response from these requests
 * @throws IOException thrown if there was a problem with this connection
 */
public HttpResponse sendBulkRequest(Iterable<PiwikRequest> requests, String authToken) throws IOException {
    if (authToken != null && authToken.length() != PiwikRequest.AUTH_TOKEN_LENGTH) {
        throw new IllegalArgumentException(
                authToken + " is not " + PiwikRequest.AUTH_TOKEN_LENGTH + " characters long.");
    }

    JsonObjectBuilder ob = Json.createObjectBuilder();
    JsonArrayBuilder ab = Json.createArrayBuilder();

    for (PiwikRequest request : requests) {
        ab.add("?" + request.getQueryString());
    }

    ob.add(REQUESTS, ab);

    if (authToken != null) {
        ob.add(AUTH_TOKEN, authToken);
    }

    HttpClient client = getHttpClient();
    HttpPost post = new HttpPost(uriBuilder.build());
    post.setEntity(new StringEntity(ob.build().toString(), ContentType.APPLICATION_JSON));

    return client.execute(post);
}

From source file:org.grogshop.services.endpoints.impl.ShopUserProfileServiceImpl.java

@Override
public Response getInterests(@NotNull @PathParam("user_id") Long user_id) throws ServiceException {
    List<Interest> interests = profileService.getInterests(user_id);
    log.info("Interests from the database: (" + user_id + ") " + interests);

    JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
    for (Interest i : interests) {
        jsonArrayBuilder
                .add(Json.createObjectBuilder().add("name", i.getName()).add("imagePath", i.getImageURL()));
    }/*from  www. j  av  a2  s .com*/
    JsonArray build = jsonArrayBuilder.build();
    return Response.ok(build.toString()).build();
}

From source file:nl.sidn.dnslib.message.records.dnssec.NSEC3ResourceRecord.java

@Override
public JsonObject toJSon() {
    JsonObjectBuilder builder = super.createJsonBuilder();
    builder.add("rdata",
            Json.createObjectBuilder().add("hash-algorithm", hashAlgorithm.name()).add("flags", flags)
                    .add("iterations", (int) iterations).add("salt-length", saltLength)
                    .add("salt", Hex.encodeHexString(salt)).add("hash-length", (int) hashLength)
                    .add("nxt-own-name", nexthashedownername));

    JsonArrayBuilder typeBuilder = Json.createArrayBuilder();
    for (TypeMap type : types) {
        typeBuilder.add(type.getType().name());
    }/*w w  w . j  a va2  s .c  o m*/
    return builder.add("types", typeBuilder.build()).build();
}

From source file:org.openstreetmap.josm.plugins.mapillary.actions.MapillarySubmitCurrentChangesetAction.java

@Override
public void actionPerformed(ActionEvent event) {
    String token = Main.pref.get("mapillary.access-token");
    if (token == null || token.trim().isEmpty()) {
        PluginState.notLoggedInToMapillaryDialog();
        return;//from  w w  w. jav a  2s  . c  om
    }
    PluginState.setSubmittingChangeset(true);
    MapillaryUtils.updateHelpText();
    HttpClientBuilder builder = HttpClientBuilder.create();
    HttpPost httpPost = new HttpPost(MapillaryURL.submitChangesetURL().toString());
    httpPost.addHeader("content-type", "application/json");
    httpPost.addHeader("Authorization", "Bearer " + token);
    JsonArrayBuilder changes = Json.createArrayBuilder();
    MapillaryLocationChangeset locationChangeset = MapillaryLayer.getInstance().getLocationChangeset();
    for (MapillaryImage image : locationChangeset) {
        changes.add(Json.createObjectBuilder().add("image_key", image.getKey()).add("values",
                Json.createObjectBuilder()
                        .add("from", Json.createObjectBuilder().add("ca", image.getCa())
                                .add("lat", image.getLatLon().getY()).add("lon", image.getLatLon().getX()))
                        .add("to",
                                Json.createObjectBuilder().add("ca", image.getTempCa())
                                        .add("lat", image.getTempLatLon().getY())
                                        .add("lon", image.getTempLatLon().getX()))));
    }
    String json = Json.createObjectBuilder().add("change_type", "location").add("changes", changes)
            .add("request_comment", "JOSM-created").build().toString();
    try (CloseableHttpClient httpClient = builder.build()) {
        httpPost.setEntity(new StringEntity(json));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            String key = Json.createReader(response.getEntity().getContent()).readObject().getString("key");
            synchronized (MapillaryUtils.class) {
                Main.map.statusLine.setHelpText(
                        String.format("%s images submitted, Changeset key: %s", locationChangeset.size(), key));
            }
            locationChangeset.cleanChangeset();

        }

    } catch (IOException e) {
        logger.error("got exception", e);
        synchronized (MapillaryUtils.class) {
            Main.map.statusLine.setHelpText("Error submitting Mapillary changeset: " + e.getMessage());
        }
    } finally {
        PluginState.setSubmittingChangeset(false);
    }
}

From source file:edu.harvard.iq.dataverse.api.HarvestingServer.java

@GET
@Path("/")
public Response oaiSets(@QueryParam("key") String apiKey) throws IOException {

    List<OAISet> oaiSets = null;
    try {//ww  w . ja  v  a2  s  . co m
        oaiSets = oaiSetService.findAll();
    } catch (Exception ex) {
        return error(Response.Status.INTERNAL_SERVER_ERROR,
                "Caught an exception looking up available OAI sets; " + ex.getMessage());
    }

    if (oaiSets == null) {
        // returning an empty list:
        return ok(jsonObjectBuilder().add("oaisets", ""));
    }

    JsonArrayBuilder hcArr = Json.createArrayBuilder();

    for (OAISet set : oaiSets) {
        hcArr.add(oaiSetAsJson(set));
    }

    return ok(jsonObjectBuilder().add("oaisets", hcArr));
}

From source file:com.open.shift.support.controller.SupportResource.java

@GET
@Produces(MediaType.APPLICATION_JSON)/*from  w  w  w.  j av  a  2  s .  c  o m*/
@Path("areachart")
public JsonObject generateAreaChart() throws Exception {
    System.out.println("The injected hashcode is " + this.supportDAO);
    int usa[] = { 0, 0, 0, 0, 0, 6, 11, 32, 110, 235, 369, 640, 1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468,
            20434, 24126, 27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662, 26956, 27912, 28999,
            28965, 27826, 25579, 25722, 24826, 24605, 24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344,
            23586, 22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950, 10871, 10824, 10577, 10527,
            10475, 10421, 10358, 10295, 10104 };

    int ussr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 25, 50, 120, 150, 200, 426, 660, 869, 1060, 1605, 2471,
            3322, 4238, 5221, 6129, 7089, 8339, 9399, 10538, 11643, 13092, 14478, 15915, 17385, 19055, 21205,
            23044, 25393, 27935, 30062, 32049, 33952, 35804, 37431, 39197, 45000, 43000, 41000, 39000, 37000,
            35000, 33000, 31000, 29000, 27000, 25000, 24000, 23000, 22000, 21000, 20000, 19000, 18000, 18000,
            17000, 16000 };

    URL u = ctx.getResource("/WEB-INF/classes/templates/area.json");
    String areaJson = null;
    if (OS.indexOf("win") >= 0) {
        areaJson = u.toString().substring(6);
    } else if (OS.indexOf("nux") >= 0) {
        areaJson = u.toString().substring(5);
    }
    System.out.println("The areaJson is " + areaJson);

    JsonReader reader = Json.createReader(new FileReader(areaJson));
    JsonObject main = reader.readObject();

    JsonObject yaxis = main.getJsonObject("yAxis");

    JsonObjectBuilder title = Json.createObjectBuilder().add("text", "US and USSR Deposits");
    JsonObjectBuilder ytitle = Json.createObjectBuilder().add("text", "Amount Deposisted");
    JsonArrayBuilder usaData = Json.createArrayBuilder();
    for (Integer usa1 : usa) {
        usaData.add(usa1);
    }

    JsonArrayBuilder ussrData = Json.createArrayBuilder();
    for (Integer uss1 : ussr) {
        ussrData.add(uss1);
    }

    JsonObjectBuilder usaSeries = Json.createObjectBuilder().add("name", "USA").add("data", usaData);
    JsonObjectBuilder ussrSeries = Json.createObjectBuilder().add("name", "USSR").add("data", ussrData);
    JsonArrayBuilder series = Json.createArrayBuilder().add(usaSeries).add(ussrSeries);

    //main json object builder
    JsonObjectBuilder mainJsonObj = Json.createObjectBuilder();
    for (Map.Entry<String, JsonValue> mainObj : main.entrySet()) {
        String key = mainObj.getKey();
        JsonValue value = mainObj.getValue();

        if (key.equalsIgnoreCase("yAxis")) {
            String yaxisLabel = "labels";
            mainJsonObj.add(key, Json.createObjectBuilder().add(yaxisLabel, yaxis.getJsonObject(yaxisLabel))
                    .add("title", ytitle.build()));
        } else {
            mainJsonObj.add(key, value);
        }

    }
    mainJsonObj.add("title", title.build()).add("series", series.build());

    return mainJsonObj.build();

}

From source file:org.grogshop.services.endpoints.impl.ShopItemsServiceImpl.java

@Override
public Response get(@PathParam("id") Long item_id) throws ServiceException {
    Item i = itemsService.getById(item_id);
    JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
    jsonObjectBuilder.add("id", (i.getId() == null) ? "" : i.getId().toString())
            .add("user_id", (i.getUser().getId() == null) ? "" : i.getUser().getId().toString())
            .add("user_email", (i.getUser().getEmail() == null) ? "" : i.getUser().getEmail())
            .add("club_id", (i.getClub().getId() == null) ? "" : i.getClub().getId().toString())
            .add("type", (i.getType() == null) ? "" : i.getType().toString())
            .add("name", (i.getName() == null) ? "" : i.getName())
            .add("description", (i.getDescription() == null) ? "" : i.getDescription())
            .add("hasImage", i.hasImage())
            .add("minPrice", (i.getMinPrice() == null) ? "" : i.getMinPrice().toString())
            .add("maxPrice", (i.getMaxPrice() == null) ? "" : i.getMaxPrice().toString());
    if (i.getTags() != null) {
        JsonArrayBuilder jsonArrayBuilderInterest = Json.createArrayBuilder();
        for (String s : i.getTags()) {
            jsonArrayBuilderInterest.add(Json.createObjectBuilder().add("text", s));
        }/*w  ww.  j ava  2 s .c om*/
        jsonObjectBuilder.add("tags", jsonArrayBuilderInterest);
    }
    JsonObject build = jsonObjectBuilder.build();
    return Response.ok(build.toString()).build();

}