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();
}
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));
}
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();
}
Aggregations