11from mongoengine .base import BaseDocument
22from bson .objectid import ObjectId
33from datetime import datetime
4+ from mongoengine .queryset .queryset import QuerySet
45
56
67class JsonSerialized :
@@ -37,17 +38,23 @@ def __resurse_collections(self, collections):
3738 if isinstance (collections , list ):
3839 col_list = list ()
3940 for collection in collections :
40- if isinstance (collection , list ):
41- col_list .append (self .__serialize (collection ))
42- else :
43- col_list .append (self .__serialize (collection ))
41+ col_list .append (self .__serialize (self .__resurse_collections (collection )))
4442 return col_list
4543 else :
4644 return self .__serialize (collections )
4745
48- @staticmethod
49- def __serialize_collection (collection ):
50- if isinstance (collection , BaseDocument ):
46+ def __serialize_collection (self , collection ):
47+ if isinstance (collection , list ):
48+ li_array = []
49+ for _ in collection :
50+ li_array .append (self .__serialize_collection (_ ))
51+ return li_array
52+ elif isinstance (collection , QuerySet ):
53+ q_arr = []
54+ for q in collection :
55+ q_arr .append (self .__serialize_collection (q ))
56+ return q_arr
57+ elif isinstance (collection , BaseDocument ):
5158 return collection .to_mongo ()
5259 elif isinstance (collection , JsonSerialized ):
5360 return collection
@@ -72,8 +79,12 @@ def __serialize(self, collection):
7279 if isinstance (value , list ):
7380 val_list = list ()
7481 for index , _ in enumerate (value ):
75- raw_collection = getattr (self .__raw_collections , key )
76- val_list .append (Serialize (raw_collection [index ]).jsonify ())
82+ print (type (_ ))
83+ if isinstance (_ , ObjectId ) or isinstance (_ , dict ) or isinstance (_ , list ):
84+ raw_collection = getattr (self .__raw_collections , key )
85+ val_list .append (Serialize (raw_collection [index ]).jsonify ())
86+ else :
87+ val_list .append (_ )
7788 setattr (json_serialized , key , val_list )
7889 else :
7990 serialized_attribute = self .__attribute_serialize (key , value )
@@ -123,9 +134,11 @@ def __dict_jsonify(self, collection):
123134 def jsonify (self ):
124135 collections = self .__collections
125136 if isinstance (collections , list ):
137+ list_col = list ()
126138 for collection in collections :
127- return self .__dict_jsonify (collection )
139+ list_col .append (self .__dict_jsonify (collection ))
140+ return list_col
128141 elif isinstance (collections , dict ):
129142 return self .__dict_jsonify (collections )
130143 else :
131- return collections
144+ return self . __dict_jsonify ( collections )
0 commit comments