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,15 @@ 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+ cl = class_from_string ( entity_class )
40+ obj = cl ()
41+ obj . __setstate__ ( json_response )
42+ return obj
3943
4044def transform_json_to_entity_list (json_response , entity_class ):
4145 entity_list = frost_sta_client .model .ext .entity_list .EntityList (entity_class )
@@ -51,9 +55,7 @@ def transform_json_to_entity_list(json_response, entity_class):
5155 response_list = json_response
5256 else :
5357 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
58+ entity_list .entities = [transform_json_to_entity (item , entity_list .entity_class ) for item in response_list ]
5759 return entity_list
5860
5961
0 commit comments