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