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