use of org.bson.BsonReader in project mongo-java-driver by mongodb.
the class StringCodecTest method testDecodeOnObjectIdWithObjectIdRep.
@Test
public void testDecodeOnObjectIdWithObjectIdRep() {
BsonReader reader = new JsonReader("{'_id': ObjectId('5f5a6cc03237b5e06d6b887b'), 'name': 'Brian'}");
reader.readStartDocument();
reader.readName();
String stringId = child.decode(reader, decoderContext);
assertEquals(stringId, "5f5a6cc03237b5e06d6b887b");
}
use of org.bson.BsonReader in project mongo-java-driver by mongodb.
the class StringCodecTest method testDecodeOnStringWithStringRep.
@Test
public void testDecodeOnStringWithStringRep() {
BsonReader reader = new JsonReader("{'name': 'Brian'");
reader.readStartDocument();
reader.readName();
assertEquals(parent.decode(reader, decoderContext), "Brian");
}
use of org.bson.BsonReader 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.BsonReader in project morphia by mongodb.
the class DocumentReaderTest method nested.
@Test
public void nested() {
setup(new Document("key", new Document("nested", "detsen")).append("list", List.of(new Document("list1", "value1"), new Document("list2", "value2"), new Document("list3", "value3"))));
step(BsonReader::readStartDocument);
step(r -> assertEquals(r.getCurrentBsonType(), BsonType.DOCUMENT));
step(r -> assertEquals(r.readName(), "key"));
step(BsonReader::readStartDocument);
step(r -> assertEquals(r.readName(), "nested"));
step(r -> assertEquals(r.readString(), "detsen"));
step(BsonReader::readEndDocument);
step(r -> assertEquals(r.readName(), "list"));
step(BsonReader::readStartArray);
readDocument(1);
readDocument(2);
readDocument(3);
step(BsonReader::readEndArray);
step(BsonReader::readEndDocument);
step(BsonReader::close);
}
use of org.bson.BsonReader in project morphia by mongodb.
the class DocumentReaderTest method testBookmarkingAndSkips.
@Test
public void testBookmarkingAndSkips() {
setup(Document.parse("{ key: { subKey: 3 }, second: 2 }"));
step(r -> assertEquals(r.getCurrentBsonType(), BsonType.DOCUMENT));
step(BsonReader::readStartDocument);
step(r -> assertEquals(r.readName(), "key"));
BsonReaderMark mark = reader.getMark();
step(BsonReader::readStartDocument);
step(r -> assertEquals(r.readName(), "subKey"));
step(r -> assertEquals(r.readInt32(), 3));
step(BsonReader::readEndDocument);
mark.reset();
reader.skipValue();
step(r -> assertEquals(r.readName(), "second"));
step(r -> assertEquals(r.readInt32(), 2));
step(BsonReader::readEndDocument);
setup(Document.parse("{ key: [ 1, 2, 3 ], second: 8 }"));
step(r -> assertEquals(r.getCurrentBsonType(), BsonType.DOCUMENT));
step(BsonReader::readStartDocument);
step(r -> assertEquals(r.readName(), "key"));
mark = reader.getMark();
step(BsonReader::readStartArray);
step(r -> assertEquals(r.readInt32(), 1));
step(r -> assertEquals(r.readInt32(), 2));
mark.reset();
reader.skipValue();
step(r -> assertEquals(r.readName(), "second"));
step(r -> assertEquals(r.readInt32(), 8));
step(BsonReader::readEndDocument);
}
Aggregations