Search in sources :

Example 11 with JsonWriter

use of javax.json.JsonWriter in project torodb by torodb.

the class ToroIndexConverter method to.

@Override
public String to(NamedToroIndex userObject) {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    objectBuilder.add(NAME_KEY, userObject.getName());
    if (userObject.isUnique()) {
        objectBuilder.add(UNIQUE_KEY, true);
    }
    JsonArrayBuilder attsBuilder = Json.createArrayBuilder();
    JsonArrayBuilder descBuilder = Json.createArrayBuilder();
    int attPosition = 0;
    boolean hasDescending = false;
    for (Map.Entry<AttributeReference, IndexType> entry : userObject.getAttributes().entrySet()) {
        attsBuilder.add(entry.getKey().toString());
        if (IndexType.desc.equals(entry.getValue())) {
            descBuilder.add(attPosition);
            hasDescending = true;
        }
        attPosition++;
    }
    objectBuilder.add(ATTS_KEY, attsBuilder);
    if (hasDescending) {
        objectBuilder.add(DESCENDING, descBuilder);
    }
    StringWriter stringWriter = new StringWriter(200);
    JsonWriter jsonWriter = Json.createWriter(stringWriter);
    jsonWriter.writeObject(objectBuilder.build());
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) AttributeReference(com.torodb.core.language.AttributeReference) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) IndexType(com.torodb.core.model.IndexedAttributes.IndexType) Map(java.util.Map) JsonWriter(javax.json.JsonWriter)

Example 12 with JsonWriter

use of javax.json.JsonWriter in project knime-core by knime.

the class BugAP7806_WorkflowSaveHooks method testOpenAPIGeneration.

/**
 * Checks if the OpenAPI definition for the input parameters is generated correctly.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testOpenAPIGeneration() throws Exception {
    getManager().setDirty();
    getManager().save(m_workflowDir, new ExecutionMonitor(), true);
    Path openApiFragmentFile = m_workflowDir.toPath().resolve(WorkflowSaveHook.ARTIFACTS_FOLDER_NAME).resolve(OpenAPIDefinitionGenerator.INPUT_PARAMETERS_FILE);
    String actualInputParameters;
    try (JsonReader reader = Json.createReader(Files.newInputStream(openApiFragmentFile))) {
        JsonObject o = reader.readObject();
        try (StringWriter sw = new StringWriter();
            JsonWriter writer = Json.createWriter(sw)) {
            writer.write(o);
            actualInputParameters = sw.toString();
        }
    }
    String expectedInputParameters;
    try (JsonReader reader = Json.createReader(Files.newInputStream(m_workflowDir.toPath().resolve("openapi-input-parameters-reference.json")))) {
        JsonObject o = reader.readObject();
        try (StringWriter sw = new StringWriter();
            JsonWriter writer = Json.createWriter(sw)) {
            writer.write(o);
            expectedInputParameters = sw.toString();
        }
    }
    assertThat("Unexpected input parameters definition written", actualInputParameters, is(expectedInputParameters));
}
Also used : Path(java.nio.file.Path) StringWriter(java.io.StringWriter) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Example 13 with JsonWriter

use of javax.json.JsonWriter in project quickstart by wildfly.

the class BiddingEncoder method encode.

@Override
public String encode(Bidding bidding) throws EncodeException {
    // It uses the JSON-P API to create a JSON representation
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder().add("item", Json.createObjectBuilder().add("buyNowPrice", bidding.getItem().getBuyNowPrice()).add("description", bidding.getItem().getDescription()).add("imagePath", bidding.getItem().getImagePath()).add("title", bidding.getItem().getTitle()).build()).add("bidStatus", bidding.getBidStatus().toString()).add("currentPrice", bidding.getCurrentPrice()).add("secondsLeft", 0);
    if (bidding.getDueDate() != null) {
        jsonBuilder.add("dueDate", bidding.getDueDate().getTime());
    }
    if (bidding.getSecondsLeft() != null) {
        jsonBuilder.add("secondsLeft", bidding.getSecondsLeft());
    }
    JsonArrayBuilder jsonBidArray = Json.createArrayBuilder();
    for (Bid bid : bidding.getBids()) {
        jsonBidArray.add(Json.createObjectBuilder().add("dateTime", bid.getDateTime().getTime()).add("value", bid.getValue()).add("id", bid.getId()).build());
    }
    jsonBuilder.add("bids", jsonBidArray);
    StringWriter stWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(stWriter);
    jsonWriter.writeObject(jsonBuilder.build());
    jsonWriter.close();
    return stWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Bid(org.jboss.quickstarts.websocket.model.Bid) JsonWriter(javax.json.JsonWriter)

Aggregations

JsonWriter (javax.json.JsonWriter)13 StringWriter (java.io.StringWriter)10 JsonObject (javax.json.JsonObject)7 Test (org.junit.Test)5 JsonArrayBuilder (javax.json.JsonArrayBuilder)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 JsonReader (javax.json.JsonReader)2 AttributeReference (com.torodb.core.language.AttributeReference)1 IndexType (com.torodb.core.model.IndexedAttributes.IndexType)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 JsonArray (javax.json.JsonArray)1