use of org.bson.json.JsonWriter in project core-ng-project by neowu.
the class EntityEncoderBuilderTest method encode.
@Test
void encode() throws IOException {
assertEquals(Sets.newHashSet(TestEntityChild.TestEnum.class), builder.enumCodecFields.keySet());
StringWriter writer = new StringWriter();
TestEntity entity = new TestEntity();
entity.id = new ObjectId("5627b47d54b92d03adb9e9cf");
entity.booleanField = true;
entity.longField = 325L;
entity.stringField = "string";
entity.zonedDateTimeField = ZonedDateTime.of(LocalDateTime.of(2016, 9, 1, 11, 0, 0), ZoneId.of("America/New_York"));
entity.child = new TestEntityChild();
entity.child.enumField = TestEntityChild.TestEnum.ITEM1;
entity.child.enumListField = Lists.newArrayList(TestEntityChild.TestEnum.ITEM2);
entity.listField = Lists.newArrayList("V1", "V2");
entity.mapField = Maps.newHashMap();
entity.mapField.put("K1", "V1");
entity.mapField.put("K2", "V2");
encoder.encode(new JsonWriter(writer, JsonWriterSettings.builder().indent(true).build()), entity);
ObjectMapper mapper = new ObjectMapper();
JsonNode expectedEntityNode = mapper.readTree(ClasspathResources.text("mongo-test/entity.json"));
JsonNode entityNode = mapper.readTree(writer.toString());
assertEquals(expectedEntityNode, entityNode);
}
use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.
the class StringCodecTest method testEncodeWithStringRep.
@Test
public void testEncodeWithStringRep() {
StringWriter writer = new StringWriter();
BsonWriter jsonWriter = new JsonWriter(writer);
jsonWriter.writeStartDocument();
jsonWriter.writeName("_id");
parent.encode(jsonWriter, "5f5a6cc03237b5e06d6b887b", EncoderContext.builder().build());
jsonWriter.writeEndDocument();
assertEquals(writer.toString(), "{\"_id\": \"5f5a6cc03237b5e06d6b887b\"}");
}
use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.
the class BsonDocumentTest method toJsonShouldRespectDefaultJsonWriterSettings.
@Test
public void toJsonShouldRespectDefaultJsonWriterSettings() {
StringWriter writer = new StringWriter();
new BsonDocumentCodec().encode(new JsonWriter(writer), document, EncoderContext.builder().build());
assertEquals(writer.toString(), document.toJson());
}
use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method toJson.
@Override
public String toJson(final JsonWriterSettings settings) {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
ByteBuf duplicate = byteBuf.duplicate();
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
try {
jsonWriter.pipe(reader);
return stringWriter.toString();
} finally {
duplicate.release();
reader.close();
}
}
use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.
the class MultiFileExportBenchmark method writeJsonFile.
private Runnable writeJsonFile(final int fileId, final List<RawBsonDocument> documents, final CountDownLatch latch) {
return new Runnable() {
@Override
public void run() {
try {
Writer writer = new OutputStreamWriter(new FileOutputStream(new File(tempDirectory, String.format("%03d", fileId) + ".txt")), StandardCharsets.UTF_8);
try {
RawBsonDocumentCodec codec = new RawBsonDocumentCodec();
for (RawBsonDocument cur : documents) {
codec.encode(new JsonWriter(writer), cur, EncoderContext.builder().build());
writer.write('\n');
}
} finally {
writer.close();
}
latch.countDown();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
Aggregations