static ClassMapBasedObjectSerializer addCommonSerializers() { ClassMapBasedObjectSerializer serializer = new ClassMapBasedObjectSerializer(); serializer.addObjectSerializer(Object[].class, new ObjectArraySerializer(serializer)); serializer.addObjectSerializer(Boolean.class, new ToStringSerializer()); serializer.addObjectSerializer(Code.class, new CodeSerializer(serializer)); serializer.addObjectSerializer(CodeWScope.class, new CodeWScopeSerializer(serializer)); serializer.addObjectSerializer(DBObject.class, new DBObjectSerializer(serializer)); serializer.addObjectSerializer(DBRefBase.class, new DBRefBaseSerializer(serializer)); serializer.addObjectSerializer(Iterable.class, new IterableSerializer(serializer)); serializer.addObjectSerializer(Map.class, new MapSerializer(serializer)); serializer.addObjectSerializer(MaxKey.class, new MaxKeySerializer(serializer)); serializer.addObjectSerializer(MinKey.class, new MinKeySerializer(serializer)); serializer.addObjectSerializer(Number.class, new ToStringSerializer()); serializer.addObjectSerializer(ObjectId.class, new ObjectIdSerializer(serializer)); serializer.addObjectSerializer(Pattern.class, new PatternSerializer(serializer)); serializer.addObjectSerializer(String.class, new StringSerializer()); serializer.addObjectSerializer(UUID.class, new UUIDSerializer(serializer)); return serializer; }
@Override public void serialize(Object obj, StringBuilder buf) { DBRefBase ref = (DBRefBase) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$ref", ref.getRef()); temp.put("$id", ref.getId()); serializer.serialize(temp, buf); }
protected void _putObjectField( String name , Object val ){ if ( name.equals( "_transientFields" ) ) return; if ( DEBUG ) System.out.println( "\t put thing : " + name ); if ( name.equals( "$where") && val instanceof String ){ _put( CODE , name ); _putValueString( val.toString() ); return; } val = BSON.applyEncodingHooks( val ); if ( val == null ) putNull(name); else if ( val instanceof Date ) putDate( name , (Date)val ); else if ( val instanceof Number ) putNumber(name, (Number)val ); else if ( val instanceof String ) putString(name, val.toString() ); else if ( val instanceof ObjectId ) putObjectId(name, (ObjectId)val ); else if ( val instanceof BSONObject ) putObject(name, (BSONObject)val ); else if ( val instanceof Boolean ) putBoolean(name, (Boolean)val ); else if ( val instanceof Pattern ) putPattern(name, (Pattern)val ); else if ( val instanceof Map ) putMap( name , (Map)val ); else if ( val instanceof Iterable) putIterable( name , (Iterable)val ); else if ( val instanceof byte[] ) putBinary( name , (byte[])val ); else if ( val instanceof Binary ) putBinary( name , (Binary)val ); else if ( val instanceof UUID ) putUUID( name , (UUID)val ); else if ( val.getClass().isArray() ) putArray( name , val ); else if (val instanceof Symbol) { putSymbol(name, (Symbol) val); } else if (val instanceof BSONTimestamp) { putTimestamp( name , (BSONTimestamp)val ); } else if (val instanceof CodeWScope) { putCodeWScope( name , (CodeWScope)val ); } else if (val instanceof Code) { putCode( name , (Code)val ); } else if (val instanceof DBRefBase) { BSONObject temp = new BasicBSONObject(); temp.put("$ref", ((DBRefBase)val).getRef()); temp.put("$id", ((DBRefBase)val).getId()); putObject( name, temp ); } else if ( putSpecial( name , val ) ){ // no-op } else { throw new IllegalArgumentException( "can't serialize " + val.getClass() ); } }
protected void _putObjectField( String name , Object val ){ if ( name.equals( "_transientFields" ) ) return; if ( DEBUG ) System.out.println( "\t put thing : " + name ); if ( name.contains( "\0" ) ) throw new IllegalArgumentException( "Document field names can't have a NULL character. (Bad Key: '" + name + "')" ); if ( name.equals( "$where") && val instanceof String ){ _put( CODE , name ); _putValueString( val.toString() ); return; } val = BSON.applyEncodingHooks( val ); if ( val == null ) putNull(name); else if ( val instanceof Date ) putDate( name , (Date)val ); else if ( val instanceof Number ) putNumber(name, (Number)val ); else if ( val instanceof Character ) putString(name, val.toString() ); else if ( val instanceof String ) putString(name, val.toString() ); else if ( val instanceof ObjectId ) putObjectId(name, (ObjectId)val ); else if ( val instanceof BSONObject ) putObject(name, (BSONObject)val ); else if ( val instanceof Boolean ) putBoolean(name, (Boolean)val ); else if ( val instanceof Pattern ) putPattern(name, (Pattern)val ); else if ( val instanceof Map ) putMap( name , (Map)val ); else if ( val instanceof Iterable) putIterable( name , (Iterable)val ); else if ( val instanceof byte[] ) putBinary( name , (byte[])val ); else if ( val instanceof Binary ) putBinary( name , (Binary)val ); else if ( val instanceof UUID ) putUUID( name , (UUID)val ); else if ( val.getClass().isArray() ) putArray( name , val ); else if (val instanceof Symbol) { putSymbol(name, (Symbol) val); } else if (val instanceof BSONTimestamp) { putTimestamp( name , (BSONTimestamp)val ); } else if (val instanceof CodeWScope) { putCodeWScope( name , (CodeWScope)val ); } else if (val instanceof Code) { putCode( name , (Code)val ); } else if (val instanceof DBRefBase) { BSONObject temp = new BasicBSONObject(); temp.put("$ref", ((DBRefBase)val).getRef()); temp.put("$id", ((DBRefBase)val).getId()); putObject( name, temp ); } else if ( val instanceof MinKey ) putMinKey( name ); else if ( val instanceof MaxKey ) putMaxKey( name ); else if ( putSpecial( name , val ) ){ // no-op } else { throw new IllegalArgumentException( "can't serialize " + val.getClass() ); } }