(self, obj, output, setReferencePosition=False)
return output
Serializes the given object to the output. Returns output.
           If setReferencePosition is True, will set the position the
           object was written.
Serializes the given object to the output. Returns output.
           If setReferencePosition is True, will set the position the
           object was written.
Serializes the given object to the output . Returns output . If setReferencePosition is True will set the position the object was written .
def writeObject(self, obj, output, setReferencePosition=False):
        """Serializes the given object to the output. Returns output.
           If setReferencePosition is True, will set the position the
           object was written.
        """
        def proc_variable_length(format, length):
            result = ''
            if length > 0b1110:
                result += pack('!B', (format << 4) | 0b1111)
                result = self.writeObject(length, result)
            else:
                result += pack('!B', (format << 4) | length)
            return result
        
        if setReferencePosition:
            self.referencePositions[obj] = len(output)
        
        if obj is None:
            output += pack('!B', 0b00000000)
        elif type(obj) == bool:
            if obj is False:
                output += pack('!B', 0b00001000)
            else:
                output += pack('!B', 0b00001001)
        elif isinstance(obj, Uid):
            size = self.intSize(obj)
            output += pack('!B', (0b1000 << 4) | size - 1)
            output += self.binaryInt(Uid)
        elif isinstance(obj, (int, long)):
            bytes = self.intSize(obj)
            root = math.log(bytes, 2)
            output += pack('!B', (0b0001 << 4) | int(root))
            output += self.binaryInt(obj)
        elif isinstance(obj, float):
            # just use doubles
            output += pack('!B', (0b0010 << 4) | 3)
            output += self.binaryReal(obj)
        elif isinstance(obj, datetime.datetime):
            timestamp = time.mktime(obj.timetuple())
            timestamp -= apple_reference_date_offset
            output += pack('!B', 0b00110011)
            output += pack('!d', float(timestamp))
        elif isinstance(obj, Data):
            output += proc_variable_length(0b0100, len(obj))
            output += obj
        elif isinstance(obj, (str, unicode)):
            if isinstance(obj, unicode):
                bytes = obj.encode('utf_16_be')
                output += proc_variable_length(0b0110, len(bytes)/2)
                output += bytes
            else:
                bytes = obj
                output += proc_variable_length(0b0101, len(bytes))
                output += bytes
        elif isinstance(obj, HashableWrapper):
            obj = obj.value
            if isinstance(obj, (set, list, tuple)):
                if isinstance(obj, set):
                    output += proc_variable_length(0b1100, len(obj))
                else:
                    output += proc_variable_length(0b1010, len(obj))
            
                objectsToWrite = []
                for objRef in obj:
                    (isNew, output) = self.writeObjectReference(objRef, output)
                    if isNew:
                        objectsToWrite.append(objRef)
                for objRef in obj:
                    output = self.writeObject(objRef, output, setReferencePosition=True)
            elif isinstance(obj, dict):
                output += proc_variable_length(0b1101, len(obj))
                keys = []
                values = []
                objectsToWrite = []
                for key, value in obj.iteritems():
                    keys.append(key)
                    values.append(value)
                for key in keys:
                    (isNew, output) = self.writeObjectReference(key, output)
                    if isNew:
                        objectsToWrite.append(key)
                for value in values:
                    (isNew, output) = self.writeObjectReference(value, output)
                    if isNew:
                        objectsToWrite.append(value)
                for objRef in objectsToWrite:
                    output = self.writeObject(objRef, output, setReferencePosition=True)
        return output
def writeObject ( self , obj , output , setReferencePosition = False ) : def proc_variable_length ( format , length ) : result = '' if length > 0b1110 : result += pack ( '!B' , ( format << 4 ) | 0b1111 ) result = self . writeObject ( length , result ) else : result += pack ( '!B' , ( format << 4 ) | length ) return result if setReferencePosition : self . referencePositions [ obj ] = len ( output ) if obj is None : output += pack ( '!B' , 0b00000000 ) elif type ( obj ) == bool : if obj is False : output += pack ( '!B' , 0b00001000 ) else : output += pack ( '!B' , 0b00001001 ) elif isinstance ( obj , Uid ) : size = self . intSize ( obj ) output += pack ( '!B' , ( 0b1000 << 4 ) | size - 1 ) output += self . binaryInt ( Uid ) elif isinstance ( obj , ( int , long ) ) : bytes = self . intSize ( obj ) root = math . log ( bytes , 2 ) output += pack ( '!B' , ( 0b0001 << 4 ) | int ( root ) ) output += self . binaryInt ( obj ) elif isinstance ( obj , float ) : # just use doubles output += pack ( '!B' , ( 0b0010 << 4 ) | 3 ) output += self . binaryReal ( obj ) elif isinstance ( obj , datetime . datetime ) : timestamp = time . mktime ( obj . timetuple ( ) ) timestamp -= apple_reference_date_offset output += pack ( '!B' , 0b00110011 ) output += pack ( '!d' , float ( timestamp ) ) elif isinstance ( obj , Data ) : output += proc_variable_length ( 0b0100 , len ( obj ) ) output += obj elif isinstance ( obj , ( str , unicode ) ) : if isinstance ( obj , unicode ) : bytes = obj . encode ( 'utf_16_be' ) output += proc_variable_length ( 0b0110 , len ( bytes ) / 2 ) output += bytes else : bytes = obj output += proc_variable_length ( 0b0101 , len ( bytes ) ) output += bytes elif isinstance ( obj , HashableWrapper ) : obj = obj . value if isinstance ( obj , ( set , list , tuple ) ) : if isinstance ( obj , set ) : output += proc_variable_length ( 0b1100 , len ( obj ) ) else : output += proc_variable_length ( 0b1010 , len ( obj ) ) objectsToWrite = [ ] for objRef in obj : ( isNew , output ) = self . writeObjectReference ( objRef , output ) if isNew : objectsToWrite . append ( objRef ) for objRef in obj : output = self . writeObject ( objRef , output , setReferencePosition = True ) elif isinstance ( obj , dict ) : output += proc_variable_length ( 0b1101 , len ( obj ) ) keys = [ ] values = [ ] objectsToWrite = [ ] for key , value in obj . iteritems ( ) : keys . append ( key ) values . append ( value ) for key in keys : ( isNew , output ) = self . writeObjectReference ( key , output ) if isNew : objectsToWrite . append ( key ) for value in values : ( isNew , output ) = self . writeObjectReference ( value , output ) if isNew : objectsToWrite . append ( value ) for objRef in objectsToWrite : output = self . writeObject ( objRef , output , setReferencePosition = True ) return output