Search in sources :

Example 11 with JsonWriter

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);
}
Also used : StringWriter(java.io.StringWriter) ObjectId(org.bson.types.ObjectId) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonWriter(org.bson.json.JsonWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 12 with JsonWriter

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\"}");
}
Also used : StringWriter(java.io.StringWriter) BsonWriter(org.bson.BsonWriter) JsonWriter(org.bson.json.JsonWriter) Test(org.junit.Test)

Example 13 with JsonWriter

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());
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test)

Example 14 with JsonWriter

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();
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) JsonWriter(org.bson.json.JsonWriter) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 15 with JsonWriter

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);
            }
        }
    };
}
Also used : FileOutputStream(java.io.FileOutputStream) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) OutputStreamWriter(java.io.OutputStreamWriter) RawBsonDocument(org.bson.RawBsonDocument) IOException(java.io.IOException) File(java.io.File) JsonWriter(org.bson.json.JsonWriter) JsonWriter(org.bson.json.JsonWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) TextBasedBenchmarkResultWriter(com.mongodb.benchmark.framework.TextBasedBenchmarkResultWriter)

Aggregations

JsonWriter (org.bson.json.JsonWriter)20 StringWriter (java.io.StringWriter)19 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)4 Test (org.junit.Test)4 BsonBinaryReader (org.bson.BsonBinaryReader)2 BsonWriter (org.bson.BsonWriter)2 ByteBuf (org.bson.ByteBuf)2 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)2 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TextBasedBenchmarkResultWriter (com.mongodb.benchmark.framework.TextBasedBenchmarkResultWriter)1 AppException (io.realm.mongodb.AppException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 UtilityClass (lombok.experimental.UtilityClass)1 BsonReader (org.bson.BsonReader)1