Search in sources :

Example 6 with BsonReader

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

the class ProtocolHelper method getNestedFieldValue.

@SuppressWarnings("SameParameterValue")
private static BsonValue getNestedFieldValue(final ResponseBuffers responseBuffers, final String topLevelFieldName, final String nestedFieldName) {
    try {
        BsonReader bsonReader = createBsonReader(responseBuffers);
        bsonReader.readStartDocument();
        while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            if (bsonReader.readName().equals(topLevelFieldName)) {
                return getField(bsonReader, nestedFieldName);
            }
            bsonReader.skipValue();
        }
        bsonReader.readEndDocument();
        return null;
    } finally {
        responseBuffers.reset();
    }
}
Also used : BsonReader(org.bson.BsonReader)

Example 7 with BsonReader

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

the class StringCodecTest method testDecodeOnStringWithObjectIdRep.

@Test(expected = BsonInvalidOperationException.class)
public void testDecodeOnStringWithObjectIdRep() {
    BsonReader reader = new JsonReader("{'name': 'Brian'");
    reader.readStartDocument();
    reader.readName();
    child.decode(reader, decoderContext);
}
Also used : BsonReader(org.bson.BsonReader) JsonReader(org.bson.json.JsonReader) Test(org.junit.Test)

Example 8 with BsonReader

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

the class StringCodecTest method testDecodeOnObjectIdWithStringRep.

@Test(expected = BsonInvalidOperationException.class)
public void testDecodeOnObjectIdWithStringRep() {
    BsonReader reader = new JsonReader("{'_id':  ObjectId('5f5a6cc03237b5e06d6b887b'), 'name': 'Brian'}");
    reader.readStartDocument();
    reader.readName();
    parent.decode(reader, decoderContext);
}
Also used : BsonReader(org.bson.BsonReader) JsonReader(org.bson.json.JsonReader) Test(org.junit.Test)

Example 9 with BsonReader

use of org.bson.BsonReader in project morphia by mongodb.

the class DocumentReaderTest method read.

@Test
public void read() {
    setup(new Document("key", "value").append("numbers", List.of(1, 2, 3, 4, 5)).append("another", "entry"));
    step(BsonReader::readStartDocument);
    step(r -> assertEquals(r.getCurrentBsonType(), BsonType.STRING));
    step(r -> assertEquals(r.readName(), "key"));
    step(r -> assertEquals(r.readString(), "value"));
    step(r -> assertEquals(r.readName(), "numbers"));
    step(BsonReader::readStartArray);
    for (int i = 1; i < 6; i++) {
        final int finalI = i;
        step(r -> assertEquals(finalI, r.readInt32()));
    }
    step(BsonReader::readEndArray);
    step(r -> assertEquals(r.readName(), "another"));
    step(r -> assertEquals(r.readString(), "entry"));
    step(BsonReader::readEndDocument);
}
Also used : BsonReader(org.bson.BsonReader) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 10 with BsonReader

use of org.bson.BsonReader in project morphia by mongodb.

the class DocumentReaderTest method mark.

@Test
public void mark() {
    setup(new Document("key", "value").append("nested", "detsen"));
    step(BsonReader::readStartDocument);
    BsonReaderMark docMark = reader.getMark();
    step(r -> assertEquals(r.readName(), "key"));
    step(r -> assertEquals(r.readString(), "value"));
    docMark.reset();
    step(r -> assertEquals(r.readName(), "key"));
    step(r -> assertEquals(r.readString(), "value"));
    step(r -> assertEquals(r.readName(), "nested"));
    step(r -> assertEquals(r.readString(), "detsen"));
    step(BsonReader::readEndDocument);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) BsonReader(org.bson.BsonReader) Document(org.bson.Document) 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