Search in sources :

Example 1 with BsonDbPointer

use of org.bson.BsonDbPointer in project pinpoint by naver.

the class WritecontextTest method parseBsonArrayWithValues.

@Test
public void parseBsonArrayWithValues() throws IOException {
    BsonValue a = new BsonString("stest");
    BsonValue b = new BsonDouble(111);
    BsonValue c = new BsonBoolean(true);
    BsonDocument document = new BsonDocument().append("int32", new BsonInt32(12)).append("int64", new BsonInt64(77L)).append("bo\"olean", new BsonBoolean(true)).append("date", new BsonDateTime(new Date().getTime())).append("double", new BsonDouble(12.3)).append("string", new BsonString("pinpoint")).append("objectId", new BsonObjectId(new ObjectId())).append("code", new BsonJavaScript("int i = 10;")).append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1)))).append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big")).append("symbol", new BsonSymbol("wow")).append("timestamp", new BsonTimestamp(0x12345678, 5)).append("undefined", new BsonUndefined()).append("binary1", new BsonBinary(new byte[] { (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20 })).append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[] { 1, 1, 1, 1, 1 })).append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7)))).append("document", new BsonDocument("a", new BsonInt32(77))).append("dbPointer", new BsonDbPointer("db.coll", new ObjectId())).append("null", new BsonNull()).append("decimal128", new BsonDecimal128(new Decimal128(55)));
    BasicDBObject query = new BasicDBObject();
    query.put("ComplexBson", document);
    logger.debug("document:{}", document);
    NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[] { query }, true);
    logger.debug("val:{}", stringStringValue);
    List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
    Assert.assertEquals(list.size(), 1);
    Map<String, ?> query1Map = (Map<String, ?>) list.get(0);
    checkValue(query1Map);
}
Also used : BsonString(org.bson.BsonString) BsonBoolean(org.bson.BsonBoolean) BasicDBObject(com.mongodb.BasicDBObject) BsonInt32(org.bson.BsonInt32) BsonDecimal128(org.bson.BsonDecimal128) BsonDouble(org.bson.BsonDouble) ArrayList(java.util.ArrayList) List(java.util.List) BsonJavaScriptWithScope(org.bson.BsonJavaScriptWithScope) BsonNull(org.bson.BsonNull) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonBinary(org.bson.BsonBinary) BsonDbPointer(org.bson.BsonDbPointer) Decimal128(org.bson.types.Decimal128) BsonDecimal128(org.bson.BsonDecimal128) BsonRegularExpression(org.bson.BsonRegularExpression) BsonObjectId(org.bson.BsonObjectId) BsonJavaScript(org.bson.BsonJavaScript) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) BsonInt64(org.bson.BsonInt64) BsonSymbol(org.bson.BsonSymbol) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) Map(java.util.Map) BsonUndefined(org.bson.BsonUndefined) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 2 with BsonDbPointer

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

the class JsonReader method visitDBPointerConstructor.

private BsonDbPointer visitDBPointerConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    String namespace = readStringFromExtendedJson();
    verifyToken(JsonTokenType.COMMA);
    ObjectId id = new ObjectId(readStringFromExtendedJson());
    verifyToken(JsonTokenType.RIGHT_PAREN);
    return new BsonDbPointer(namespace, id);
}
Also used : ObjectId(org.bson.types.ObjectId) BsonDbPointer(org.bson.BsonDbPointer)

Example 3 with BsonDbPointer

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

the class DBObjectCodec method readValue.

@Nullable
private Object readValue(final BsonReader reader, final DecoderContext decoderContext, @Nullable final String fieldName, final List<String> path) {
    Object initialRetVal;
    BsonType bsonType = reader.getCurrentBsonType();
    if (bsonType.isContainer() && fieldName != null) {
        // if we got into some new context like nested document or array
        path.add(fieldName);
    }
    switch(bsonType) {
        case DOCUMENT:
            initialRetVal = verifyForDBRef(readDocument(reader, decoderContext, path));
            break;
        case ARRAY:
            initialRetVal = readArray(reader, decoderContext, path);
            break;
        case // custom for driver-compat types
        JAVASCRIPT_WITH_SCOPE:
            initialRetVal = readCodeWScope(reader, decoderContext, path);
            break;
        case // custom for driver-compat types
        DB_POINTER:
            BsonDbPointer dbPointer = reader.readDBPointer();
            initialRetVal = new DBRef(dbPointer.getNamespace(), dbPointer.getId());
            break;
        case BINARY:
            initialRetVal = readBinary(reader, decoderContext);
            break;
        case NULL:
            reader.readNull();
            initialRetVal = null;
            break;
        default:
            initialRetVal = bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext);
    }
    if (bsonType.isContainer() && fieldName != null) {
        // step out of current context to a parent
        path.remove(fieldName);
    }
    return initialRetVal;
}
Also used : BsonType(org.bson.BsonType) BSONObject(org.bson.BSONObject) BsonDbPointer(org.bson.BsonDbPointer) Nullable(com.mongodb.lang.Nullable)

Example 4 with BsonDbPointer

use of org.bson.BsonDbPointer in project pinpoint by naver.

the class MongoDBITBase method createComplexDocument.

private Document createComplexDocument() {
    // insert Data
    BsonValue a = new BsonString("stest");
    BsonValue b = new BsonDouble(111);
    BsonValue c = new BsonBoolean(true);
    Document document = new Document().append("int32", new BsonInt32(12)).append("int64", new BsonInt64(77L)).append("bo\"olean", new BsonBoolean(true)).append("date", new BsonDateTime(new Date().getTime())).append("double", new BsonDouble(12.3)).append("string", new BsonString("pinpoint")).append("objectId", new BsonObjectId(new ObjectId())).append("code", new BsonJavaScript("int i = 10;")).append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1)))).append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big")).append("symbol", new BsonSymbol("wow")).append("timestamp", new BsonTimestamp(0x12345678, 5)).append("undefined", new BsonUndefined()).append("binary1", new BsonBinary(new byte[] { (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20 })).append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[] { 1, 1, 1, 1, 1 })).append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7)))).append("document", new BsonDocument("a", new BsonInt32(77))).append("dbPointer", new BsonDbPointer("db.coll", new ObjectId())).append("null", new BsonNull());
    return document;
}
Also used : BsonNull(org.bson.BsonNull) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonBinary(org.bson.BsonBinary) BsonDbPointer(org.bson.BsonDbPointer) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonRegularExpression(org.bson.BsonRegularExpression) BsonBoolean(org.bson.BsonBoolean) BsonObjectId(org.bson.BsonObjectId) BsonJavaScript(org.bson.BsonJavaScript) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonSymbol(org.bson.BsonSymbol) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble) BsonJavaScriptWithScope(org.bson.BsonJavaScriptWithScope) BsonUndefined(org.bson.BsonUndefined) BsonValue(org.bson.BsonValue)

Example 5 with BsonDbPointer

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

the class JsonReader method visitDbPointerExtendedJson.

private BsonDbPointer visitDbPointerExtendedJson() {
    verifyToken(JsonTokenType.COLON);
    verifyToken(JsonTokenType.BEGIN_OBJECT);
    String ref;
    ObjectId oid;
    String firstKey = readStringFromExtendedJson();
    if (firstKey.equals("$ref")) {
        verifyToken(JsonTokenType.COLON);
        ref = readStringFromExtendedJson();
        verifyToken(JsonTokenType.COMMA);
        verifyString("$id");
        oid = readDbPointerIdFromExtendedJson();
        verifyToken(JsonTokenType.END_OBJECT);
    } else if (firstKey.equals("$id")) {
        oid = readDbPointerIdFromExtendedJson();
        verifyToken(JsonTokenType.COMMA);
        verifyString("$ref");
        verifyToken(JsonTokenType.COLON);
        ref = readStringFromExtendedJson();
    } else {
        throw new JsonParseException("Expected $ref and $id fields in $dbPointer document but found " + firstKey);
    }
    verifyToken(JsonTokenType.END_OBJECT);
    return new BsonDbPointer(ref, oid);
}
Also used : ObjectId(org.bson.types.ObjectId) BsonDbPointer(org.bson.BsonDbPointer)

Aggregations

BsonDbPointer (org.bson.BsonDbPointer)8 ObjectId (org.bson.types.ObjectId)7 Test (org.junit.Test)4 Date (java.util.Date)2 BsonArray (org.bson.BsonArray)2 BsonBinary (org.bson.BsonBinary)2 BsonBoolean (org.bson.BsonBoolean)2 BsonDateTime (org.bson.BsonDateTime)2 BsonDocument (org.bson.BsonDocument)2 BsonDouble (org.bson.BsonDouble)2 BsonInt32 (org.bson.BsonInt32)2 BsonInt64 (org.bson.BsonInt64)2 BsonJavaScript (org.bson.BsonJavaScript)2 BsonJavaScriptWithScope (org.bson.BsonJavaScriptWithScope)2 BsonNull (org.bson.BsonNull)2 BsonObjectId (org.bson.BsonObjectId)2 BsonRegularExpression (org.bson.BsonRegularExpression)2 BsonString (org.bson.BsonString)2 BsonSymbol (org.bson.BsonSymbol)2 BsonTimestamp (org.bson.BsonTimestamp)2