JacksonWriter implements DocumentWriter, ArrayWriter { @Override public void write(Document document, OutputStream jsonStream) throws IOException { try (JsonGenerator jsonGenerator = factory.createGenerator(jsonStream)) { configure(jsonGenerator); writeDocument(document, jsonGenerator); } } private  JacksonWriter(boolean pretty); @Override void write(Document document, OutputStream jsonStream); @Override void write(Document document, Writer jsonWriter); @Override String write(Document document); @Override byte[] writeAsBytes(Document document); @Override void write(Array array, OutputStream jsonStream); @Override void write(Array array, Writer jsonWriter); @Override String write(Array array); static final JacksonWriter INSTANCE; static final JacksonWriter PRETTY_WRITER; }
@Test public void shouldWriteDocumentWithSingleField() throws Exception { doc.set("field", "value"); after = reader.read(writer.write(doc)); assertThat(after.getString("field")).isEqualTo("value"); assertThat(after.size()).isEqualTo(1); }
@Test public void shouldWriteDocumentWithTwoFields() throws Exception { doc.set("field1", "value"); doc.set("field2", 22); after = reader.read(writer.write(doc)); assertThat(after.getString("field1")).isEqualTo("value"); assertThat(after.getInteger("field2")).isEqualTo(22); assertThat(after.size()).isEqualTo(2); }
@Test public void shouldWriteDocumentWithNestedDocument() throws Exception { doc.set("field1", "value"); doc.set("field2", 22); doc.set("field3", Document.create("a", "A", "b", "B")); after = reader.read(writer.write(doc)); Testing.print(after); assertThat(after.getString("field1")).isEqualTo("value"); assertThat(after.getInteger("field2")).isEqualTo(22); assertThat(after.size()).isEqualTo(3); Document nested = after.getDocument("field3"); assertThat(nested.getString("a")).isEqualTo("A"); assertThat(nested.getString("b")).isEqualTo("B"); assertThat(nested.size()).isEqualTo(2); }
@Test public void shouldWriteDocumentWithDeeplyNestedDocument() throws Exception { doc.set("field1", "value"); doc.set("field2", 22); doc.set("field3", Document.create("a", "A", "b", "B", "c", Document.create("x", "X"))); after = reader.read(writer.write(doc)); Testing.print(after); assertThat(after.getString("field1")).isEqualTo("value"); assertThat(after.getInteger("field2")).isEqualTo(22); assertThat(after.size()).isEqualTo(3); Document nested = after.getDocument("field3"); assertThat(nested.getString("a")).isEqualTo("A"); assertThat(nested.getString("b")).isEqualTo("B"); assertThat(nested.size()).isEqualTo(3); Document deepNested = nested.getDocument("c"); assertThat(deepNested.getString("x")).isEqualTo("X"); assertThat(deepNested.size()).isEqualTo(1); }