def serialize(self, obj, level=0, objname=None, topLevelKeysToIgnore=None,
                  toBytes=True):
        """
        Create a string representation of the given object.

        Examples:
        ::
        >>> serialize("str")
        'str'
        >>> serialize([1,2,3,4,5])
        '1,2,3,4,5'
        >>> signing.serlize({1:'a', 2:'b'})
        '1:a|2:b'
        >>> signing.serlize({1:'a', 2:'b', 3:[1,{2:'k'}]})
        '1:a|2:b|3:1,2:k'

        :param obj: the object to serlize
        :param level: a parameter used internally for recursion to serialize nested
         data structures
         :param topLevelKeysToIgnore: the list of top level keys to ignore for
         serialization
        :return: a string representation of `obj`
        """
        res = None
        if not isinstance(obj, acceptableTypes):
            error("invalid type found {}: {}".format(objname, obj))
        elif isinstance(obj, str):
            res = obj
        elif isinstance(obj, dict):
            if level > 0:
                keys = list(obj.keys())
            else:
                topLevelKeysToIgnore = topLevelKeysToIgnore or []
                keys = [k for k in obj.keys() if k not in topLevelKeysToIgnore]
            keys.sort()
            strs = []
            for k in keys:
                onm = ".".join([str(objname), str(k)]) if objname else k
                strs.append(
                    str(k) + ":" + self.serialize(obj[k], level + 1, onm, toBytes=False))
            res = "|".join(strs)
        elif isinstance(obj, Iterable):
            strs = []
            for o in obj:
                strs.append(self.serialize(
                    o, level + 1, objname, toBytes=False))
            res = ",".join(strs)
        elif obj is None:
            res = ""
        else:
            res = str(obj)

        # logger.trace("serialized msg {} into {}".format(obj, res))

        if not toBytes:
            return res

        return res.encode('utf-8')
def serialize ( self , obj , level = 0 , objname = None , topLevelKeysToIgnore = None , toBytes = True ) : res = None if not isinstance ( obj , acceptableTypes ) : error ( "invalid type found {}: {}" . format ( objname , obj ) ) elif isinstance ( obj , str ) : res = obj elif isinstance ( obj , dict ) : if level > 0 : keys = list ( obj . keys ( ) ) else : topLevelKeysToIgnore = topLevelKeysToIgnore or [ ] keys = [ k for k in obj . keys ( ) if k not in topLevelKeysToIgnore ] keys . sort ( ) strs = [ ] for k in keys : onm = "." . join ( [ str ( objname ) , str ( k ) ] ) if objname else k strs . append ( str ( k ) + ":" + self . serialize ( obj [ k ] , level + 1 , onm , toBytes = False ) ) res = "|" . join ( strs ) elif isinstance ( obj , Iterable ) : strs = [ ] for o in obj : strs . append ( self . serialize ( o , level + 1 , objname , toBytes = False ) ) res = "," . join ( strs ) elif obj is None : res = "" else : res = str ( obj ) if not toBytes : return res return res . encode ( 'utf-8' )
Create a string representation of the given object.

        Examples:
        ::
        >>> serialize("str")
        'str'
        >>> serialize([1,2,3,4,5])
        '1,2,3,4,5'
        >>> signing.serlize({1:'a', 2:'b'})
        '1:a|2:b'
        >>> signing.serlize({1:'a', 2:'b', 3:[1,{2:'k'}]})
        '1:a|2:b|3:1,2:k'

        :param obj: the object to serlize
        :param level: a parameter used internally for recursion to serialize nested
         data structures
         :param topLevelKeysToIgnore: the list of top level keys to ignore for
         serialization
        :return: a string representation of `obj`