Search in sources :

Example 1 with BsonReader

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");
}
Also used : BsonReader(org.bson.BsonReader) JsonReader(org.bson.json.JsonReader) Test(org.junit.Test)

Example 2 with BsonReader

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");
}
Also used : BsonReader(org.bson.BsonReader) JsonReader(org.bson.json.JsonReader) Test(org.junit.Test)

Example 3 with BsonReader

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

Example 4 with BsonReader

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);
}
Also used : BsonReader(org.bson.BsonReader) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 5 with BsonReader

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);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) BsonReader(org.bson.BsonReader) Test(org.testng.annotations.Test)

Aggregations

BsonReader (org.bson.BsonReader)10 JsonReader (org.bson.json.JsonReader)4 Test (org.junit.Test)4 Test (org.testng.annotations.Test)4 Document (org.bson.Document)3 BsonReaderMark (org.bson.BsonReaderMark)2 StringWriter (java.io.StringWriter)1 JsonWriter (org.bson.json.JsonWriter)1