Example usage for javax.json JsonObjectBuilder add

List of usage examples for javax.json JsonObjectBuilder add

Introduction

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

Prototype

JsonObjectBuilder add(String name, JsonArrayBuilder builder);

Source Link

Document

Adds a name/ JsonArray pair to the JSON object associated with this object builder.

Usage

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));
        }/* www  .  j ava  2s.c om*/
        jsonObjectBuilder.add("tags", jsonArrayBuilderInterest);
    }
    JsonObject build = jsonObjectBuilder.build();
    return Response.ok(build.toString()).build();

}

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

@Override
public JsonObject toJSon() {
    Date exp = new Date();
    exp.setTime((long) (signatureExpiration * 1000));

    Date incep = new Date();
    incep.setTime((long) (signatureInception * 1000));

    JsonObjectBuilder builder = super.createJsonBuilder();
    return builder
            .add("rdata", Json.createObjectBuilder().add("type-covered", typeCovered.name())
                    .add("algorithm", algorithm.name()).add("labels", labels).add("original-ttl", originalTtl)
                    .add("sig-exp", DATE_FMT.format(exp)).add("sig-inc", DATE_FMT.format(incep))
                    .add("keytag", (int) keytag).add("signer-name", signerName)
                    .add("signature", new Base64(Integer.MAX_VALUE, "".getBytes()).encodeAsString(signature)))
            .build();/*ww w .j a va2 s  .c  o m*/
}

From source file:co.runrightfast.vertx.core.eventbus.EventBusAddressMessageMapping.java

public JsonObject toJson() {
    final JsonObjectBuilder json = Json.createObjectBuilder().add("address", address).add("requestMessageType",
            requestDefaultInstance.getDescriptorForType().getFullName());
    getResponseDefaultInstance().ifPresent(
            instance -> json.add("responseMessageType", instance.getDescriptorForType().getFullName()));
    return json.build();
}

From source file:org.openlmis.converter.FindObjectTypeConverter.java

@Override
public void convert(JsonObjectBuilder builder, Mapping mapping, String value) {
    BaseCommunicationService service = services.getService(mapping.getEntityName());
    String by = getBy(mapping.getType());

    JsonObject jsonRepresentation = service.findBy(by, value);

    if (jsonRepresentation != null) {
        builder.add(mapping.getTo(), jsonRepresentation);
    } else {/*from   ww  w  .ja v  a2s  . c om*/
        logger.warn("The CSV file contained reference to entity {} "
                + "with {} {} but such reference does not exist.", mapping.getEntityName(), by, value);
    }
}

From source file:co.runrightfast.core.application.event.AppEvent.java

private void addTags(final JsonObjectBuilder json) {
    final JsonArray jsonArray = JsonUtils.toJsonArray(tags);
    if (!jsonArray.isEmpty()) {
        json.add("tags", jsonArray);
    }//  w w  w  .  j  av  a 2s .c o  m
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkStringField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");

    Settings settings = new Settings();
    settings.setEntityClass(Person.class);
    Person person = (Person) aggregateService.create(jsonBuilder.build(), settings);
    assert (person.getId() != null);
    assert (person.getName().equals("DILIP_DALTON"));

    Object jsonObject = aggregateService.read(person, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));

}

From source file:tools.xor.logic.DefaultJson.java

protected void checkIntField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("employeeNo", 235);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getEmployeeNo() == 235);

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (((JsonNumber) json.get("employeeNo")).intValue() == 235);
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkLongField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("salary", 100000);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getSalary() == 100000);

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (((JsonNumber) json.get("salary")).intValue() == 100000);
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkEmptyLongField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getSalary() == null);
    assert (!employee.getIsCriticalSystemObject());

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (!json.containsKey("salary"));
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkBooleanField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("isCriticalSystemObject", true);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getIsCriticalSystemObject());

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (json.getBoolean("isCriticalSystemObject"));
}