Пример #1
0
    def test_image_jsonify_legacy(self):
        s = Serializer()

        example_png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5/hPwAIAgL/4d1j8wAAAABJRU5ErkJggg=="
        test_str_2 = s.jsonify('image/png', example_png)

        assert test_str_2 == f"data:image/png;base64,{example_png}"
Пример #2
0
    def test_image_jsonify_not_legacy(self):
        s = Serializer()

        example_png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5/hPwAIAgL/4d1j8wAAAABJRU5ErkJggg=="

        test_bytes = s.serialize('image/png', example_png)
        assert type(test_bytes) == bytes

        test_str = s.deserialize('image/png', test_bytes)

        test_str_2 = s.jsonify('image/png', test_str)
        assert test_str_2 == f"data:image/jpeg;base64,{test_str}"
Пример #3
0
    def jsonify_data(self) -> Dict[str, Any]:
        """Method to convert just the data to JSON safe dictionary

        Returns:
            dict
        """
        dict_data: dict = dict()

        # jsonify the data
        serializer_obj = Serializer()
        for mime_type in self.data:
            dict_data[mime_type] = serializer_obj.jsonify(
                mime_type, self.data[mime_type])

        # At this point everything in dict_data should be ready to go for JSON serialization
        return dict_data
Пример #4
0
    def test_text_plain(self):
        """Test the text/plain serializer"""
        s = Serializer()

        start_text = "This is a \n\n string with some \r stuff in it23894590*AS^&90R32UXZ02.66"

        test_bytes = s.serialize('text/plain', start_text)
        assert type(test_bytes) == bytes

        test_str = s.deserialize('text/plain', test_bytes)

        assert start_text == test_str
        assert type(test_str) == str

        test_str_2 = s.jsonify('text/plain', start_text)

        assert start_text == test_str_2
        assert type(test_str_2) == str
Пример #5
0
    def to_json(self) -> str:
        """Method to convert to a single dictionary of data, that will serialize to JSON

        Returns:
            dict
        """
        # Get base dict
        dict_data = self.to_dict()

        # jsonify the data
        serializer_obj = Serializer()
        for mime_type in dict_data['data']:
            dict_data['data'][mime_type] = serializer_obj.jsonify(
                mime_type, dict_data['data'][mime_type])

        # At this point everything in dict_data should be ready to go for JSON serialization
        return json.dumps(dict_data,
                          cls=ActivityDetailRecordEncoder,
                          separators=(',', ':'))