1919from dateutil .parser import isoparse
2020import geojson
2121import logging
22+ import sys
2223import frost_sta_client .model .ext .entity_list
2324
2425
@@ -30,12 +31,14 @@ def transform_entity_to_json_dict(entity):
3031 json_str = jsonpickle .encode (entity , unpicklable = False )
3132 return jsonpickle .decode (json_str )
3233
34+ def class_from_string (string ):
35+ module_name , class_name = string .rsplit ("." , 1 )
36+ return getattr (sys .modules [module_name ], class_name )
3337
3438def transform_json_to_entity (json_response , entity_class ):
35- decodable_str = '{\' py/object\' : \' ' + entity_class + '\' , \' py/state\' : ' \
36- + jsonpickle .encode (json_response , unpicklable = False ) + '}'
37- return jsonpickle .decode (decodable_str )
38-
39+ obj = entity_class ()
40+ obj .__setstate__ (json_response )
41+ return obj
3942
4043def transform_json_to_entity_list (json_response , entity_class ):
4144 entity_list = frost_sta_client .model .ext .entity_list .EntityList (entity_class )
@@ -51,9 +54,8 @@ def transform_json_to_entity_list(json_response, entity_class):
5154 response_list = json_response
5255 else :
5356 raise ValueError ("expected json as a dict or list to transform into entity list" )
54- for item in response_list :
55- result_list .append (transform_json_to_entity (item , entity_list .entity_class ))
56- entity_list .entities = result_list
57+ ent_cl = class_from_string (entity_list .entity_class )
58+ entity_list .entities = [transform_json_to_entity (item , ent_cl ) for item in response_list ]
5759 return entity_list
5860
5961
0 commit comments