use of javax.json.stream.JsonGenerator in project javaee7-samples by javaee-samples.
the class StreamingGeneratorTest method testSimpleObject.
@Test
public void testSimpleObject() throws JSONException {
JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
StringWriter w = new StringWriter();
JsonGenerator gen = factory.createGenerator(w);
gen.writeStartObject().write("apple", "red").write("banana", "yellow").writeEnd();
gen.flush();
JSONAssert.assertEquals("{\"apple\" : \"red\", \"banana\" : \"yellow\" }", w.toString(), JSONCompareMode.STRICT);
}
use of javax.json.stream.JsonGenerator in project javaee7-samples by javaee-samples.
the class StreamingGeneratorTest method testArray.
@Test
public void testArray() throws JSONException {
JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
StringWriter w = new StringWriter();
JsonGenerator gen = factory.createGenerator(w);
gen.writeStartArray().writeStartObject().write("apple", "red").writeEnd().writeStartObject().write("banana", "yellow").writeEnd().writeEnd();
gen.flush();
JSONAssert.assertEquals("[{\"apple\":\"red\"},{\"banana\":\"yellow\"}]", w.toString(), JSONCompareMode.STRICT);
}
use of javax.json.stream.JsonGenerator in project wildfly by wildfly.
the class JobExecutionMarshaller method marshall.
public static String marshall(final JobExecution jobExecution) {
final StringWriter writer = new StringWriter();
final JsonGenerator generator = Json.createGenerator(writer);
generator.writeStartObject().write(ID, jobExecution.getExecutionId()).write(NAME, jobExecution.getJobName()).write(STATUS, jobExecution.getBatchStatus().toString()).write(EXIT_STATUS, jobExecution.getExitStatus()).write(CREATE_TIME, jobExecution.getCreateTime().getTime()).write(END_TIME, jobExecution.getEndTime().getTime()).write(LAST_UPDATE_TIME, jobExecution.getLastUpdatedTime().getTime()).write(START_TIME, jobExecution.getStartTime().getTime());
// Write out properties
generator.writeStartObject(PROPERTIES);
final Properties params = jobExecution.getJobParameters();
for (String key : params.stringPropertyNames()) {
generator.write(key, params.getProperty(key));
}
generator.writeEnd();
// End main object
generator.writeEnd();
generator.close();
return writer.toString();
}
use of javax.json.stream.JsonGenerator in project wildfly by wildfly.
the class StepExecutionMarshaller method marshall.
public static String marshall(final StepExecution stepExecution) throws IOException {
final StringWriter writer = new StringWriter();
final JsonGenerator generator = Json.createGenerator(writer);
generator.writeStartObject().write(ID, stepExecution.getStepExecutionId()).write(NAME, stepExecution.getStepName()).write(STATUS, stepExecution.getBatchStatus().toString()).write(START_TIME, stepExecution.getStartTime().getTime()).write(END_TIME, stepExecution.getEndTime().getTime()).write(EXIT_STATUS, stepExecution.getExitStatus()).write(PERSISTENT_USER_DATA, serialize(stepExecution.getPersistentUserData()));
generator.writeStartObject(METRICS);
for (Metric metric : stepExecution.getMetrics()) {
generator.writeStartObject(METRIC);
generator.write(METRIC_TYPE, metric.getType().toString());
generator.write(METRIC_VALUE, metric.getValue());
generator.writeEnd();
}
generator.writeEnd();
// End main object
generator.writeEnd();
generator.close();
return writer.toString();
}
use of javax.json.stream.JsonGenerator in project wildfly by wildfly.
the class JsonServlet method doGet.
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
final JsonGenerator generator = Json.createGenerator(response.getWriter());
generator.writeStartObject();
generator.write("name", "value");
generator.writeEnd();
generator.close();
}
Aggregations