def test_jsonext_dumps(): assert '"foo"' == jsonext.dumps('foo') assert '{"foo": ["bar"]}' == jsonext.dumps( {'foo': sn.defer(['bar']).evaluate()}) assert '{"foo":["bar"]}' == jsonext.dumps( {'foo': sn.defer(['bar']).evaluate()}, separators=(',', ':')) assert '{"(1, 2, 3)": 1}' == jsonext.dumps({(1, 2, 3): 1})
def test_jsonext_load(tmp_path): c = _C(1, 'foo') c.x += 1 c.y = 'foobar' c.z = _Z() c.z.a += 1 c.z.b = 'barfoo' c.t = _T() c.t.t = 5 json_dump = tmp_path / 'test.json' with open(json_dump, 'w') as fp: jsonext.dump(c, fp, indent=2) with open(json_dump, 'r') as fp: print(fp.read()) with open(json_dump, 'r') as fp: c_restored = jsonext.load(fp) assert c == c_restored assert c is not c_restored # Do the same with dumps() and loads() c_restored = jsonext.loads(jsonext.dumps(c)) assert c == c_restored assert c is not c_restored
def _update_check_extras(self): '''Return a dictionary with all the check-specific information.''' exclude_check_attrs = { 'build_job', 'current_environ', 'current_partition', 'current_system', 'job' } if self.check is None: return for attr, val in util.attrs(self.check).items(): if not attr.startswith('_') and attr not in exclude_check_attrs: self.extra[f'check_{attr}'] = val self.extra['check_info'] = self.check.info() # Treat special cases if self.check.current_system: self.extra['check_system'] = self.check.current_system.name if self.check.current_partition: cp = self.check.current_partition.fullname self.extra['check_partition'] = self.check.current_partition.name # When logging performance references, we need only those of the # current system self.extra['check_reference'] = jsonext.dumps( self.check.reference.scope(cp)) if self.check.current_environ: self.extra['check_environ'] = self.check.current_environ.name if self.check.job: # Create extras for job attributes for attr, val in util.attrs(self.check.job).items(): if not attr.startswith('_'): self.extra[f'check_job_{attr}'] = val # Treat aliases self.extra['check_jobid'] = self.extra['check_job_jobid'] if self.check.job.completion_time: # Here we preformat the `check_job_completion_time`, because # the Graylog handler does not use a formatter ct = self.check.job.completion_time ct_formatted = _format_time_rfc3339(time.localtime(ct), '%FT%T%:z') self.extra['check_job_completion_time_unix'] = ct self.extra['check_job_completion_time'] = ct_formatted
def _dofmt(v): if isinstance(v, str): return v else: return jsonext.dumps(v)