Search in sources :

Example 1 with Symbol

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

the class DBObjectCodec method writeValue.

@SuppressWarnings("unchecked")
private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Object initialValue) {
    Object value = BSON.applyEncodingHooks(initialValue);
    if (value == null) {
        bsonWriter.writeNull();
    } else if (value instanceof DBRef) {
        encodeDBRef(bsonWriter, (DBRef) value);
    } else if (value instanceof Map) {
        encodeMap(bsonWriter, (Map<String, Object>) value);
    } else if (value instanceof Iterable) {
        encodeIterable(bsonWriter, (Iterable) value);
    } else if (value instanceof BSONObject) {
        encodeBsonObject(bsonWriter, ((BSONObject) value));
    } else if (value instanceof CodeWScope) {
        encodeCodeWScope(bsonWriter, (CodeWScope) value);
    } else if (value instanceof byte[]) {
        encodeByteArray(bsonWriter, (byte[]) value);
    } else if (value.getClass().isArray()) {
        encodeArray(bsonWriter, value);
    } else if (value instanceof Symbol) {
        bsonWriter.writeSymbol(((Symbol) value).getSymbol());
    } else {
        Codec codec = codecRegistry.get(value.getClass());
        codec.encode(bsonWriter, value, encoderContext);
    }
}
Also used : Codec(org.bson.codecs.Codec) CollectibleCodec(org.bson.codecs.CollectibleCodec) Symbol(org.bson.types.Symbol) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) HashMap(java.util.HashMap) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Map(java.util.Map) BsonTypeCodecMap(org.bson.codecs.BsonTypeCodecMap) CodeWScope(org.bson.types.CodeWScope)

Example 2 with Symbol

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

the class BasicBSONEncoder method _putObjectField.

/**
     * Encodes any Object type
     *
     * @param name         the field name
     * @param initialValue the value to write
     */
protected void _putObjectField(final String name, final Object initialValue) {
    if ("_transientFields".equals(name)) {
        return;
    }
    if (name.contains("\0")) {
        throw new IllegalArgumentException("Document field names can't have a NULL character. (Bad Key: '" + name + "')");
    }
    if ("$where".equals(name) && initialValue instanceof String) {
        putCode(name, new Code((String) initialValue));
    }
    Object value = BSON.applyEncodingHooks(initialValue);
    if (value == null) {
        putNull(name);
    } else if (value instanceof Date) {
        putDate(name, (Date) value);
    } else if (value instanceof Number) {
        putNumber(name, (Number) value);
    } else if (value instanceof Decimal128) {
        putDecimal128(name, (Decimal128) value);
    } else if (value instanceof Character) {
        putString(name, value.toString());
    } else if (value instanceof String) {
        putString(name, value.toString());
    } else if (value instanceof ObjectId) {
        putObjectId(name, (ObjectId) value);
    } else if (value instanceof Boolean) {
        putBoolean(name, (Boolean) value);
    } else if (value instanceof Pattern) {
        putPattern(name, (Pattern) value);
    } else if (value instanceof Iterable) {
        putIterable(name, (Iterable) value);
    } else if (value instanceof BSONObject) {
        putObject(name, (BSONObject) value);
    } else if (value instanceof Map) {
        putMap(name, (Map) value);
    } else if (value instanceof byte[]) {
        putBinary(name, (byte[]) value);
    } else if (value instanceof Binary) {
        putBinary(name, (Binary) value);
    } else if (value instanceof UUID) {
        putUUID(name, (UUID) value);
    } else if (value.getClass().isArray()) {
        putArray(name, value);
    } else if (value instanceof Symbol) {
        putSymbol(name, (Symbol) value);
    } else if (value instanceof BSONTimestamp) {
        putTimestamp(name, (BSONTimestamp) value);
    } else if (value instanceof CodeWScope) {
        putCodeWScope(name, (CodeWScope) value);
    } else if (value instanceof Code) {
        putCode(name, (Code) value);
    } else if (value instanceof DBRef) {
        BSONObject temp = new BasicBSONObject();
        DBRef dbRef = (DBRef) value;
        temp.put("$ref", dbRef.getCollectionName());
        temp.put("$id", dbRef.getId());
        if (dbRef.getDatabaseName() != null) {
            temp.put("$db", dbRef.getDatabaseName());
        }
        putObject(name, temp);
    } else if (value instanceof MinKey) {
        putMinKey(name);
    } else if (value instanceof MaxKey) {
        putMaxKey(name);
    } else if (putSpecial(name, value)) {
    // no-op
    } else {
        throw new IllegalArgumentException("Can't serialize " + value.getClass());
    }
}
Also used : Pattern(java.util.regex.Pattern) ObjectId(org.bson.types.ObjectId) Symbol(org.bson.types.Symbol) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Decimal128(org.bson.types.Decimal128) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary) UUID(java.util.UUID) Map(java.util.Map)

Example 3 with Symbol

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

the class LazyBSONObject method readValue.

Object readValue(final BsonBinaryReader reader) {
    switch(reader.getCurrentBsonType()) {
        case DOCUMENT:
            return readDocument(reader);
        case ARRAY:
            return readArray(reader);
        case DOUBLE:
            return reader.readDouble();
        case STRING:
            return reader.readString();
        case BINARY:
            byte binarySubType = reader.peekBinarySubType();
            if (BsonBinarySubType.isUuid(binarySubType) && reader.peekBinarySize() == 16) {
                return new UuidCodec().decode(reader, DecoderContext.builder().build());
            }
            BsonBinary binary = reader.readBinaryData();
            if (binarySubType == BINARY.getValue() || binarySubType == OLD_BINARY.getValue()) {
                return binary.getData();
            } else {
                return new Binary(binary.getType(), binary.getData());
            }
        case NULL:
            reader.readNull();
            return null;
        case UNDEFINED:
            reader.readUndefined();
            return null;
        case OBJECT_ID:
            return reader.readObjectId();
        case BOOLEAN:
            return reader.readBoolean();
        case DATE_TIME:
            return new Date(reader.readDateTime());
        case REGULAR_EXPRESSION:
            BsonRegularExpression regularExpression = reader.readRegularExpression();
            return Pattern.compile(regularExpression.getPattern(), BSON.regexFlags(regularExpression.getOptions()));
        case DB_POINTER:
            BsonDbPointer dbPointer = reader.readDBPointer();
            return callback.createDBRef(dbPointer.getNamespace(), dbPointer.getId());
        case JAVASCRIPT:
            return new Code(reader.readJavaScript());
        case SYMBOL:
            return new Symbol(reader.readSymbol());
        case JAVASCRIPT_WITH_SCOPE:
            return new CodeWScope(reader.readJavaScriptWithScope(), (BSONObject) readJavaScriptWithScopeDocument(reader));
        case INT32:
            return reader.readInt32();
        case TIMESTAMP:
            BsonTimestamp timestamp = reader.readTimestamp();
            return new BSONTimestamp(timestamp.getTime(), timestamp.getInc());
        case INT64:
            return reader.readInt64();
        case DECIMAL128:
            return reader.readDecimal128();
        case MIN_KEY:
            reader.readMinKey();
            return new MinKey();
        case MAX_KEY:
            reader.readMaxKey();
            return new MaxKey();
        default:
            throw new IllegalArgumentException("unhandled BSON type: " + reader.getCurrentBsonType());
    }
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) Symbol(org.bson.types.Symbol) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary)

Example 4 with Symbol

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

the class JSONSerializersTest method testStrictSerialization.

@Test
public void testStrictSerialization() {
    ObjectSerializer serializer = JSONSerializers.getStrict();
    // test  BINARY
    byte[] b = { 1, 2, 3, 4 };
    String base64 = DatatypeConverter.printBase64Binary(b);
    StringBuilder buf = new StringBuilder();
    serializer.serialize(new Binary(b), buf);
    assertEquals("{ \"$binary\" : \"" + base64 + "\" , \"$type\" : 0}", buf.toString());
    // test  BSON_TIMESTAMP
    buf = new StringBuilder();
    serializer.serialize(new BSONTimestamp(123, 456), buf);
    assertEquals("{ \"$timestamp\" : { \"t\" : 123 , \"i\" : 456}}", buf.toString());
    // test  BYTE_ARRAY
    buf = new StringBuilder();
    serializer.serialize(b, buf);
    assertEquals("{ \"$binary\" : \"" + base64 + "\" , \"$type\" : 0}", buf.toString());
    // test  DATE
    Date d = new Date();
    buf = new StringBuilder();
    serializer.serialize(d, buf);
    assertEquals("{ \"$date\" : " + (d.getTime()) + "}", buf.toString());
    // test  SYMBOL
    buf = new StringBuilder();
    serializer.serialize(new Symbol("test"), buf);
    assertEquals("{ \"$symbol\" : \"test\"}", buf.toString());
}
Also used : Symbol(org.bson.types.Symbol) BSONTimestamp(org.bson.types.BSONTimestamp) Binary(org.bson.types.Binary) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Symbol (org.bson.types.Symbol)4 Date (java.util.Date)3 BSONTimestamp (org.bson.types.BSONTimestamp)3 Binary (org.bson.types.Binary)3 CodeWScope (org.bson.types.CodeWScope)3 Map (java.util.Map)2 Code (org.bson.types.Code)2 MaxKey (org.bson.types.MaxKey)2 MinKey (org.bson.types.MinKey)2 DBRef (com.mongodb.DBRef)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 Pattern (java.util.regex.Pattern)1 BSONObject (org.bson.BSONObject)1 BsonTypeClassMap (org.bson.codecs.BsonTypeClassMap)1 BsonTypeCodecMap (org.bson.codecs.BsonTypeCodecMap)1 Codec (org.bson.codecs.Codec)1 CollectibleCodec (org.bson.codecs.CollectibleCodec)1 UuidCodec (org.bson.codecs.UuidCodec)1 Decimal128 (org.bson.types.Decimal128)1