Esempio n. 1
0
def serialize(data,
              requested_fieldset=None,
              serialization_format=Serializer.SERIALIZATION_TYPES.RAW,
              converter_name=None,
              allow_tags=None):
    from pyston.converters import get_default_converter_name

    converter_name = converter_name if converter_name is not None else get_default_converter_name(
    )
    try:
        converter = get_converter(
            converter_name) if converter_name != 'python' else None
    except ValueError:
        raise UnsupportedMediaTypeException

    requested_fieldset = rfs(
        requested_fieldset) if requested_fieldset is not None else None
    converted_dict = get_serializer(data).serialize(
        data,
        serialization_format,
        requested_fieldset=requested_fieldset,
        direct_serialization=True,
        allow_tags=allow_tags or (converter and converter.allow_tags))
    if converter:
        os = UniversalBytesIO()
        converter.encode_to_stream(os, converted_dict)
        return os.get_string_value()
    else:
        return serialized_data_to_python(converted_dict)
Esempio n. 2
0
def serialize(data, requested_fieldset=None, serialization_format=Serializer.SERIALIZATION_TYPES.RAW,
              converter_name=None):
    from pyston.converters import get_default_converter_name

    converter_name = converter_name if converter_name is not None else get_default_converter_name()
    requested_fieldset = rfs(requested_fieldset) if requested_fieldset is not None else None
    converted_dict = get_serializer(data).serialize(
        data, serialization_format, requested_fieldset=requested_fieldset, direct_serialization=True
    )
    if converter_name == 'python':
        return serialized_data_to_python(converted_dict)
    else:
        try:
            converter = get_converter(converter_name)
        except ValueError:
            raise UnsupportedMediaTypeException
        os = UniversalBytesIO()
        converter.encode_to_stream(os, converted_dict)
        return os.get_string_value()