module DataMapper
  class Property
    class Object < Property
      load_as         ::Object
      coercion_method :to_object

      # @api semipublic
      def dump(value)
        if self.class == Object
          marshal(value)
        else
          value
        end
      end

      # @api semipublic
      def load(value)
        if self.class == Object
          unmarshal(value)
        else
          typecast(value)
        end
      end

      # @api semipublic
      def marshal(value)
        return if value.nil?
        [ Marshal.dump(value) ].pack('m')
      end

      # @api semipublic
      def unmarshal(value)
        case value
          when ::String
            Marshal.load(value.unpack('m').first)
          when ::Object
            value
          end
      end

      # @api private
      def to_child_key
        self.class
      end
    end
  end
end

module DataMapper
  class Property
    class Object < Property
      load_as         ::Object
      coercion_method :to_object

      # @api semipublic
      def dump(value)
        instance_of?(Object) ? marshal(value) : value
      end

      # @api semipublic
      def load(value)
        typecast(instance_of?(Object) ? unmarshal(value) : value)
      end

      # @api semipublic
      def marshal(value)
        [ Marshal.dump(value) ].pack('m') unless value.nil?
      end

      # @api semipublic
      def unmarshal(value)
        Marshal.load(value.unpack('m').first) unless value.nil?
      end

      # @api private
      def to_child_key
        self.class
      end
    end
  end
end

Use instance_of? in Object property load/dump methods
Use instance_of? in Object property load/dump methods
