我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用jsonpickle.loads()。
def deserialize(cls, json_string): """ Builds a Graph from a JSON representation :param json_string: JSON str :return: datamodel.base.graph.Graph instance """ try: graph_dict = json.loads(json_string) result = graph.Graph(name=graph_dict['name']) nodes = dict() for item in graph_dict['nodes']: nodes[item['id']] = NodeSerializer.from_dict(item['data']) result.add_nodes(nodes.values()) for e in graph_dict['edges']: result.connect(nodes[e['id_node_from']], nodes[e['id_node_to']], e['output_label']) return result except Exception as e: raise #raise GraphDeserializationException(e.message)
def listen(self, rcv_timeout=0.01): message = True while message: try: message = self._pubsub.get_message(timeout=rcv_timeout) except ConnectionError: log.error("lost connection to Redis") time.sleep(1) break if message: log.debug("%s - receied pub message: %s" % (self._cli_id, message)) if message['type'] == 'message': try: return jsonpickle.loads(message['data']) except(ValueError, KeyError): return message['data'] return None
def process_event(self, event): name, args, kwargs = jsonpickle.loads(event) func = self.functions.get(name) if not func: logger.info("processing event - missing function name: %s", name) raise Exception("Missing function") try: func(*args, **kwargs) except Exception as e: return False return True
def from_dict(cls, node_dict): """ Creates a node from a dict representation :param node_dict: dict :return: datamodel.base.node.Node """ # import classes, so we can instantiate them at need import robograph.datamodel # retrieve the class object so we can instantiate the node klass = eval(node_dict['class']) node = klass(name=node_dict['name']) # now retrieve the parameters parameters = dict() for p in node_dict['params']: parameters[p] = node_dict['params'][p] # are we deserializing a pickled function? if isinstance(node_dict['params'][p], unicode): if "py/bytes" in node_dict['params'][p]: parameters[p] = cloudpickle.loads(jsonpickle.loads(node_dict['params'][p])) node.input(parameters) node.set_output_label(node_dict['output_label']) return node
def deserialize(cls, json_string): """ Builds a Node from a JSON representation :param json_string: JSON str :return: datamodel.base.node.Node instance """ try: node_dict = json.loads(json_string) return NodeSerializer.from_dict(node_dict) except Exception as e: raise NodeDeserializationException(e.message)
def get_extra_data(self): """ Return extra data that was saved """ if not self.extra_data: return {} else: return json.loads(self.extra_data)
def get_attachments(self): if self.attachments: return jsonpickle.loads(self.attachments) else: return None
def load(file_name): if file_name.endswith('.json'): with open(file_name, 'r') as f: return jsonpickle.loads(f.read()) if file_name.endswith('.npy'): return np.load(file_name) return joblib.load(file_name)
def set_state(self, state): delayed_orders = jsonpickle.loads(state.decode('utf-8')) for account in self._accounts.values(): for o in account.daily_orders.values(): if not o._is_final(): if o.order_id in delayed_orders: self._delayed_orders.append((account, o)) else: self._open_orders.append((account, o))
def set_state(self, state): state = jsonpickle.loads(state.decode('utf-8')) for key, value in six.iteritems(state): try: self._objects[key].set_state(value) except KeyError: system_log.warn('core object state for {} ignored'.format(key))
def set_state(self, state): value = jsonpickle.loads(state.decode('utf-8')) for v in value['open_orders']: o = Order() o.set_state(v) account = self._env.get_account(o.order_book_id) self._open_orders.append((account, o)) for v in value['delayed_orders']: o = Order() o.set_state(v) account = self._env.get_account(o.order_book_id) self._delayed_orders.append((account, o))
def wrap_if_needed(exception): try: jsonpickle.loads(jsonpickle.dumps(exception)) return exception except BaseException: return _WrappedException(type(exception).__name__, str(exception))
def _recv_message(connection): # Retrieving the length of the msg to come. def _unpack(conn): return struct.unpack(_INT_FMT, _recv_bytes(conn, _INT_SIZE))[0] msg_metadata_len = _unpack(connection) msg = _recv_bytes(connection, msg_metadata_len) return jsonpickle.loads(msg)
def _main(): arguments_json_path = sys.argv[1] with open(arguments_json_path) as f: arguments = pickle.loads(f.read()) # arguments_json_path is a temporary file created by the parent process. # so we remove it here os.remove(arguments_json_path) task_id = arguments['task_id'] port = arguments['port'] messenger = _Messenger(task_id=task_id, port=port) function = arguments['function'] operation_arguments = arguments['operation_arguments'] context_dict = arguments['context'] strict_loading = arguments['strict_loading'] try: ctx = context_dict['context_cls'].instantiate_from_dict(**context_dict['context']) except BaseException as e: messenger.failed(e) return try: messenger.started() task_func = imports.load_attribute(function) aria.install_aria_extensions(strict_loading) for decorate in process_executor.decorate(): task_func = decorate(task_func) task_func(ctx=ctx, **operation_arguments) ctx.close() messenger.succeeded() except BaseException as e: ctx.close() messenger.failed(e)
def test_wrapping_required1(self): e = NonJsonPickleableException(_ARG1, _ARG2) wrapped_e = exceptions.wrap_if_needed(e) wrapped_e = jsonpickle.loads(jsonpickle.dumps(wrapped_e)) assert isinstance(wrapped_e, exceptions._WrappedException) assert wrapped_e.exception_type == type(e).__name__ assert wrapped_e.exception_str == str(e)
def test_wrapping_required2(self): e = NonJsonPickleableException(arg1=_ARG1, arg2=_ARG2) wrapped_e = exceptions.wrap_if_needed(e) wrapped_e = jsonpickle.loads(jsonpickle.dumps(wrapped_e)) assert isinstance(wrapped_e, exceptions._WrappedException) assert wrapped_e.exception_type == type(e).__name__ assert wrapped_e.exception_str == str(e)
def set_state(self, state): self._open_orders = [] self._delayed_orders = [] value = jsonpickle.loads(state.decode('utf-8')) for v in value['open_orders']: o = Order() o.set_state(v) account = self._env.get_account(o.order_book_id) self._open_orders.append((account, o)) for v in value['delayed_orders']: o = Order() o.set_state(v) account = self._env.get_account(o.order_book_id) self._delayed_orders.append((account, o))