Search in sources :

Example 6 with JsonGenerator

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);
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(javax.json.stream.JsonGenerator) JsonGeneratorFactory(javax.json.stream.JsonGeneratorFactory) Test(org.junit.Test)

Example 7 with JsonGenerator

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);
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(javax.json.stream.JsonGenerator) JsonGeneratorFactory(javax.json.stream.JsonGeneratorFactory) Test(org.junit.Test)

Example 8 with JsonGenerator

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();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(javax.json.stream.JsonGenerator) Properties(java.util.Properties)

Example 9 with JsonGenerator

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();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(javax.json.stream.JsonGenerator) Metric(javax.batch.runtime.Metric)

Example 10 with JsonGenerator

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();
}
Also used : JsonGenerator(javax.json.stream.JsonGenerator)

Aggregations

JsonGenerator (javax.json.stream.JsonGenerator)18 StringWriter (java.io.StringWriter)9 HashMap (java.util.HashMap)4 JsonGeneratorFactory (javax.json.stream.JsonGeneratorFactory)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 Map (java.util.Map)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 AuthOpException (digilib.auth.AuthOpException)1 DigilibServletRequest (digilib.conf.DigilibServletRequest)1 ImageOpException (digilib.image.ImageOpException)1 DocuDirectory (digilib.io.DocuDirectory)1 FileOpException (digilib.io.FileOpException)1 ImageInput (digilib.io.ImageInput)1 ImageSet (digilib.io.ImageSet)1 ImageSize (digilib.util.ImageSize)1 FileOutputStream (java.io.FileOutputStream)1 Writer (java.io.Writer)1