Exemplo n.º 1
0
    def restore_book(self, book_name):
        book_uri = self._get_book_uri(book_name)
        book_uri += "//book.json"
        with open(book_uri) as book_in:
            json_data = json.loads(book_in.read())

        return SerializerManager.deserialize(json_data)
Exemplo n.º 2
0
 def from_json(cls, json):
     obj = cls()
     #TODO jagros - check if all keys from cls._PROPERTIES_TO_SERIALIZE are available in json obj, if not, throw exception
     for field_name in cls._PROPERTIES_TO_SERIALIZE:
         json_val = json[field_name]
         val = SerializerManager.deserialize(json_val)
         setattr(obj, field_name, val)
     return obj
    def test_serialization(self):
        class OtherSerializableClass(Serializable):
            _STATIC_TYPE = "OtherSerializableClass"
            _PROPERTIES_TO_SERIALIZE = ["a"]

            def initialize(self, a):
                self.a = a
                return self

            def __eq__(self, other):
                return self.a == other.a

        class MySerializableClass(Serializable):
            _STATIC_TYPE = "MySerializableClass"
            _PROPERTIES_TO_SERIALIZE = [
                "int_val", "int_val_for_composed_class", "float_val", "a_dict",
                "a_list", "a_set"
            ]

            def initialize(self, int_val, int_val_for_composed_class,
                           float_val, a_dict, a_list, a_set):
                self.int_val = int_val
                self.int_val_for_composed_class = OtherSerializableClass(
                ).initialize(int_val_for_composed_class)
                self.float_val = float_val
                self.a_dict = a_dict
                self.a_list = a_list
                self.a_set = a_set
                return self

            def __eq__(self, other):
                return self.int_val == other.int_val \
                       and self.int_val_for_composed_class == other.int_val_for_composed_class \
                       and self.float_val == other.float_val \
                       and self.a_dict == other.a_dict \
                       and self.a_list == other.a_list \
                       and self.a_set == other.a_set

        obj = MySerializableClass().initialize(1, 2, 3.5, {
            "key1": 1,
            "key2": 2
        }, [1, 2, 3, 4], {1, 2, 3})

        serialized = SerializerManager.serialize(obj)
        deserialized = SerializerManager.deserialize(serialized)
        self.assertEqual(obj, deserialized)
Exemplo n.º 4
0
 def _from_json(self, json_dict):
     return {
         key: SerializerManager.deserialize(json_val)
         for (key, json_val) in json_dict.items()
     }
 def _from_json(self, json_list):
     return [SerializerManager.deserialize(elem) for elem in json_list]
Exemplo n.º 6
0
 def _from_json(self, json_set):
     return set(SerializerManager.deserialize(elem) for elem in json_set)