def from_json(self, desired_class, dct): ''' Method copied from: https://github.com/jsonpickle/jsonpickle/issues/148#issuecomment-362508753 ''' dct[tags.OBJECT] = util.importable_name(desired_class) obj = unpickler.Unpickler().restore(dct, classes=desired_class) return obj
def from_json(json): '''load from json string''' doc_dict = jsonpickle.decode(json) entities = [] for e_dict in doc_dict['entities']: e_dict[tags.OBJECT] = util.importable_name(Entity) e = unpickler.Unpickler().restore(e_dict, classes=[Entity]) entities.append(e) return entities
def entities_from_json(json): ''' read from entities json returns entities list ''' entities_dict = jsonpickle.decode(json) entities = [] for e_dict in entities_dict: e_dict[tags.OBJECT] = util.importable_name(Entity) e = unpickler.Unpickler().restore(e_dict, classes=[Entity]) entities.append(e) return entities
def output_dict_to_json(source_dict, name, type, depth): ''' Function to write the source dictionary to a json format Output json file is named with 2 parameters name and type json pickle API is used to convert a dictionary to json format ''' p = pick.Pickler(max_depth=depth) u = unpick.Unpickler() data = p.flatten(source_dict) #clear the file contents before writing open(name + '_' + type, 'w').close() #converting the dictionary to json file with open(name + '_' + type, 'a') as json_output: json_output.write(json.dumps(data))
def extend_trace(old_trace_json, spans): u = unpickler.Unpickler() trace = u.restore(old_trace_json) span_map = {} q = deque() q.append(trace.start) while q: span = q.pop() span_map[span.context.span_id] = span for c in span.callees: q.append(c) ids = set() for span in spans: if span.context.parent_span in span_map: parent_span = span_map[span.context.parent_span] parent_span.add_callee(span) ids.add(span.db_id) return (ids, trace)
def read_json(file_name): ''' To read the given data in the form of json file ''' u = unpick.Unpickler() data_dict = {} data = [] result_dict = {} with open(file_name, 'r') as json_input: for line in json_input: data = json.loads(line) data_dict = u.restore(data) #conversion from string to int and float for keys, values in data_dict.items(): result_dict[float(keys)] = float(values) return result_dict