public static DBObject serializeFields ( ObjectMapper objectMapper , DBObject object ) { BasicDBObject serialised = null ; for ( String field : object . keySet ( ) ) { Object value = object . get ( field ) ; Object serialisedValue = serializeField ( objectMapper , value ) ; if ( value != serialisedValue ) { // It's changed if ( serialised == null ) { // Make a shallow copy of the object serialised = new BasicDBObject ( ) ; for ( String f : object . keySet ( ) ) { serialised . put ( f , object . get ( f ) ) ; } } serialised . put ( field , serialisedValue ) ; } } if ( serialised != null ) { return serialised ; } else { return object ; } }
Serialize the fields of the given object using the given object mapper . This will convert POJOs to DBObjects where necessary .