示例#1
0
 def _serialize(obj, **options):
     if options:
         log.warning('options are currently unusued')
     try:
         return msgpack.dumps(obj)
     except Exception as error:
         raise SerializationError(error)
示例#2
0
文件: json.py 项目: zeus911/ops
def serialize(obj, **options):
    """
    Serialize Python data to JSON.

    :param obj: the data structure to serialize
    :param options: options given to lower json/simplejson module.
    """

    try:
        if 'fp' in options:
            return json.dump(obj, **options)
        else:
            return json.dumps(obj, **options)
    except Exception as error:
        raise SerializationError(error)
示例#3
0
def serialize(obj, **options):
    """
    Serialize Python data to YAML.

    :param obj: the data structure to serialize
    :param options: options given to lower yaml module.
    """

    options.setdefault('Dumper', BaseDumper)
    try:
        response = yaml.dump(obj, **options)
        if response.endswith('\n...\n'):
            return response[:-5]
        if response.endswith('\n'):
            return response[:-1]
        return response
    except Exception as error:
        raise SerializationError(error)