def bson2json(bson_data): # thanks to # http://stackoverflow.com/questions/16586180/typeerror-objectid-is-not-json-serializable import json from bson.objectid import ObjectId class MyJSONEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, ObjectId): return str(o) if isinstance(o, datetime.datetime): seconds = time.mktime(o.timetuple()) seconds += (o.microsecond / 1000000.0) return seconds return json.JSONEncoder.default(self, o) return ru.parse_json(MyJSONEncoder().encode(bson_data))
def bson2json (bson_data) : # thanks to # http://stackoverflow.com/questions/16586180/typeerror-objectid-is-not-json-serializable import json from bson.objectid import ObjectId class MyJSONEncoder (json.JSONEncoder) : def default (self, o): if isinstance (o, ObjectId) : return str (o) if isinstance (o, datetime.datetime) : seconds = time.mktime (o.timetuple ()) seconds += (o.microsecond / 1000000.0) return seconds return json.JSONEncoder.default (self, o) return ru.parse_json (MyJSONEncoder ().encode (bson_data))
def parse (self, workload_description): """ Parse the given json into task and relation descriptions, and create a workload out of them. """ troy._logger.info ("parsing workload") tasks = None relations = None workload_dict = ru.parse_json (workload_description) if 'tasks' in workload_dict : tasks = workload_dict['tasks'] if 'relations' in workload_dict : relations = workload_dict['relations'] if not tasks and not relations : raise ValueError ("Cannot handle workload description") task_descriptions = list() relation_descriptions = list() if tasks : for task_descr in tasks : # make sure we use all current information ru.dict_stringexpand (task_descr, self.session.cfg) task_descriptions.append (troy.TaskDescription (task_descr)) if relations : for relation_descr in relations : # make sure we use all current information ru.dict_stringexpand (relation_descr, self.session.cfg) relation_descriptions.append (troy.RelationDescription (relation_descr)) return task_descriptions, relation_descriptions
def test_read_json(): ''' Test json parser ''' # -------------------------------------------------------------------------- # default xcase data = {'test_1': 1, 'test_2': 'one', 'test_3': [1, 'one']} filename = _write_json(json.dumps(data)) data_copy = ru.read_json(filename) assert(data_copy) for key in data: assert(key in data_copy) assert(data[key] == data_copy[key]) for key in data_copy: assert(key in data) assert(data[key] == data_copy[key]) # --------------------------------------------------------------------------- # string read data_copy = ru.read_json_str(filename) assert(isinstance(data_copy['test_2'], str)) # --------------------------------------------------------------------------- # arg switching ru.write_json(filename, data_copy) ru.write_json(data_copy, filename) data_copy = ru.read_json_str(filename) assert(len(data_copy) == 3) os.unlink(filename) # -------------------------------------------------------------------------- # manual parse data = '''{ "test_1": 1, "test_2": "one", "test_3": [1, "one"] }''' data_copy = ru.parse_json(data, filter_comments=False) assert(len(data_copy) == 3) assert(data_copy['test_2'] == 'one') # -------------------------------------------------------------------------- # forced str conversion on manual parse data_copy = ru.parse_json_str(data) assert(len(data_copy) == 3) assert(isinstance(data_copy['test_2'], str)) # --------------------------------------------------------------------------- # faulty json file filename = _write_raw(b'{"foo": [False]}') with pytest.raises(ValueError): ru.read_json(filename)