@@ -45,7 +45,7 @@ def __serialize_type_of(self, collection):
4545 return collection , None
4646
4747 def __serialize_collection (self , collections ):
48- if isinstance (collections , QuerySet ) or isinstance ( collections , list ):
48+ if isinstance (collections , ( QuerySet , list , tuple ) ):
4949 return [self .__serialize_collection (_ ) for _ in collections ]
5050 else :
5151 type_of_mongo , type_of_raw = self .__serialize_type_of (collections )
@@ -63,10 +63,11 @@ def __filter_serialize(self, collections, raw):
6363 new_collection = dict ()
6464 for index , collection in enumerate (collections .items ()):
6565 key , value = collection
66- raw_collection = getattr (raw , self .__get_raw_name (key ), collection )
67- if isinstance (value , list ):
66+ # if no raw passed. get the raw collection in class level attribute as default value
67+ raw_collection = getattr (raw , self .__get_raw_name (key ), self .__raw_collections )
68+ if isinstance (value , (list , tuple )):
6869 serialized_list = list ()
69- for raw_col_item in raw_collection :
70+ for raw_col_item in raw_collection [ key ] :
7071 type_off_mongo , type_off_raw = self .__serialize_type_of (raw_col_item )
7172 filtered = self .__filter_serialize (type_off_mongo , type_off_raw )
7273 serialized_list .append (filtered )
@@ -79,7 +80,10 @@ def __filter_serialize(self, collections, raw):
7980 new_key , new_value = self .__attribute_serialize (key , value )
8081 new_collection .update (self .__serialize (new_key , new_value , raw_collection ))
8182 else :
82- print (self .__serialize_collection (raw_collection ))
83+ # TODO:: repeat for now. will do deep serialization in the future
84+ new_key , new_value = self .__attribute_serialize (key , value )
85+ new_collection .update (self .__serialize (new_key , new_value , raw_collection ))
86+ # print(self.__serialize_collection(raw_collection))
8387 # new_collection.update(self.__serialize(new_key, new_value, type_off_raw))
8488 else :
8589 new_collection .update (self .__serialize (key , value , raw_collection ))
@@ -111,15 +115,15 @@ def __extract_list_to_exclude(self, items, *to_exclude):
111115 _new_dict = dict ()
112116 for item in items .items ():
113117 k , v = item
114- if isinstance (v , list ):
118+ if isinstance (v , ( list , tuple ) ):
115119 serialized_list = self .__extract_list_to_exclude (v , * to_exclude )
116120 _new_dict .update (dict .fromkeys ((k ,), serialized_list ))
117121 else :
118122 _new_dict .update (dict .fromkeys ((k ,), v ))
119123 serialized = JsonSerialized (** _new_dict )
120124 serialized .exclude_attributes (* to_exclude )
121125 return serialized .to_json ()
122- if isinstance (items , list ):
126+ if isinstance (items , ( list , tuple ) ):
123127 return [self .__extract_list_to_exclude (_ , * to_exclude ) for _ in items ]
124128 else :
125129 return items
@@ -150,10 +154,9 @@ def __dict_jsonify(collection):
150154 return collection
151155
152156 def jsonify (self ):
153- collections = self .__collections
154- if isinstance (collections , (list , tuple )):
155- return [self .__dict_jsonify (_ ) for _ in collections ]
156- elif isinstance (collections , dict ):
157- return self .__dict_jsonify (collections )
157+ if isinstance (self .__collections , (list , tuple )):
158+ return [self .__dict_jsonify (_ ) for _ in self .__collections ]
159+ elif isinstance (self .__collections , dict ):
160+ return self .__dict_jsonify (self .__collections )
158161 else :
159- return self .__dict_jsonify (collections )
162+ return self .__dict_jsonify (self . __collections )
0 commit comments