Search in sources :

Example 1 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 2 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class StringCodecTest method testEncodeWithObjectIdRep.

@Test
public void testEncodeWithObjectIdRep() {
    StringWriter writer = new StringWriter();
    BsonWriter jsonWriter = new JsonWriter(writer);
    jsonWriter.writeStartDocument();
    jsonWriter.writeName("_id");
    child.encode(jsonWriter, "5f5a6cc03237b5e06d6b887b", encoderContext);
    jsonWriter.writeEndDocument();
    assertEquals(writer.toString(), "{\"_id\": {\"$oid\": \"5f5a6cc03237b5e06d6b887b\"}}");
}
Also used : StringWriter(java.io.StringWriter) BsonWriter(org.bson.BsonWriter) JsonWriter(org.bson.json.JsonWriter) Test(org.junit.Test)

Example 3 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class BsonDocumentTest method toJsonShouldRespectJsonWriterSettings.

@Test
public void toJsonShouldRespectJsonWriterSettings() {
    StringWriter writer = new StringWriter();
    JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build();
    new BsonDocumentCodec().encode(new JsonWriter(writer, settings), document, EncoderContext.builder().build());
    assertEquals(writer.toString(), document.toJson(settings));
}
Also used : StringWriter(java.io.StringWriter) JsonWriterSettings(org.bson.json.JsonWriterSettings) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test)

Example 4 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class LoggingCommandEventSender method getTruncatedJsonCommand.

private String getTruncatedJsonCommand() {
    StringWriter writer = new StringWriter();
    BsonReader bsonReader = commandDocument.asBsonReader();
    try {
        JsonWriter jsonWriter = new JsonWriter(writer, JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).maxLength(MAX_COMMAND_DOCUMENT_LENGTH_TO_LOG).build());
        jsonWriter.pipe(bsonReader);
        if (jsonWriter.isTruncated()) {
            writer.append(" ...");
        }
        return writer.toString();
    } finally {
        bsonReader.close();
    }
}
Also used : StringWriter(java.io.StringWriter) BsonReader(org.bson.BsonReader) JsonWriter(org.bson.json.JsonWriter)

Example 5 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class JsonObjectCodec method decode.

@Override
public JsonObject decode(final BsonReader reader, final DecoderContext decoderContext) {
    StringWriter stringWriter = new StringWriter();
    new JsonWriter(stringWriter, writerSettings).pipe(reader);
    return new JsonObject(stringWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) JsonObject(org.bson.json.JsonObject) JsonWriter(org.bson.json.JsonWriter)

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