Search in sources :

Example 1 with JsonGeneratorFactory

use of javax.json.stream.JsonGeneratorFactory in project javaee7-samples by javaee-samples.

the class StreamingGeneratorTest method testNestedStructure.

@Test
public void testNestedStructure() throws JSONException {
    JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
    StringWriter w = new StringWriter();
    JsonGenerator gen = factory.createGenerator(w);
    gen.writeStartObject().write("title", "The Matrix").write("year", 1999).writeStartArray("cast").write("Keanu Reaves").write("Laurence Fishburne").write("Carrie-Anne Moss").writeEnd().writeEnd();
    gen.flush();
    JSONAssert.assertEquals("{\"title\":\"The Matrix\",\"year\":1999,\"cast\":[\"Keanu Reaves\",\"Laurence Fishburne\",\"Carrie-Anne Moss\"]}", 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 2 with JsonGeneratorFactory

use of javax.json.stream.JsonGeneratorFactory 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 3 with JsonGeneratorFactory

use of javax.json.stream.JsonGeneratorFactory 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 4 with JsonGeneratorFactory

use of javax.json.stream.JsonGeneratorFactory in project javaee7-samples by javaee-samples.

the class StreamingGeneratorTest method testEmptyObject.

@Test
public void testEmptyObject() throws JSONException {
    JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
    StringWriter w = new StringWriter();
    JsonGenerator gen = factory.createGenerator(w);
    gen.writeStartObject().writeEnd();
    gen.flush();
    JSONAssert.assertEquals("{}", 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 5 with JsonGeneratorFactory

use of javax.json.stream.JsonGeneratorFactory in project beast-mcmc by beast-dev.

the class AuspiceGenerator method writeTreeJSON.

private void writeTreeJSON(String filename, RootedTree tree) throws IOException {
    FileWriter writer = new FileWriter(filename);
    HashMap<String, Object> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonGeneratorFactory factory = Json.createGeneratorFactory(config);
    JsonGenerator generator = factory.createGenerator(writer);
    currentYValue = 0;
    writeNode(generator, tree, tree.getRootNode());
    generator.close();
}
Also used : HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) JsonGenerator(javax.json.stream.JsonGenerator) JsonGeneratorFactory(javax.json.stream.JsonGeneratorFactory)

Aggregations

JsonGenerator (javax.json.stream.JsonGenerator)6 JsonGeneratorFactory (javax.json.stream.JsonGeneratorFactory)6 StringWriter (java.io.StringWriter)4 Test (org.junit.Test)4 FileWriter (java.io.FileWriter)2 HashMap (java.util.HashMap)2