1919from dateutil .parser import isoparse
2020import geojson
2121import logging
22+ import sys
2223import frost_sta_client .model .ext .entity_list
2324
2425
@@ -33,12 +34,15 @@ def transform_entity_to_json_dict(entity):
3334 json_str = jsonpickle .encode (entity , unpicklable = False )
3435 return jsonpickle .decode (json_str )
3536
37+ def class_from_string (string ):
38+ module_name , class_name = string .rsplit ("." , 1 )
39+ return getattr (sys .modules [module_name ], class_name )
3640
3741def transform_json_to_entity (json_response , entity_class ):
38- decodable_str = '{ \' py/object \' : \' ' + entity_class + ' \' , \' py/state \' : ' \
39- + jsonpickle . encode ( json_response , unpicklable = False ) + '}'
40- return jsonpickle . decode ( decodable_str )
41-
42+ cl = class_from_string ( entity_class )
43+ obj = cl ()
44+ obj . __setstate__ ( json_response )
45+ return obj
4246
4347def transform_json_to_entity_list (json_response , entity_class ):
4448 entity_list = frost_sta_client .model .ext .entity_list .EntityList (entity_class )
@@ -54,9 +58,7 @@ def transform_json_to_entity_list(json_response, entity_class):
5458 response_list = json_response
5559 else :
5660 raise ValueError ("expected json as a dict or list to transform into entity list" )
57- for item in response_list :
58- result_list .append (transform_json_to_entity (item , entity_list .entity_class ))
59- entity_list .entities = result_list
61+ entity_list .entities = [transform_json_to_entity (item , entity_list .entity_class ) for item in response_list ]
6062 return entity_list
6163
6264
0 commit comments