def run(): print('== Original Python native data ==') newdata = data.copy() print(newdata) print('== JData-annotated data ==') print(jd.show(jd.encode(newdata), indent=4, default=jd.jsonfilter)) print('== JData-annotated data exported to JSON with zlib compression ==') newdata = data.copy() print( jd.show(jd.encode(newdata, { 'compression': 'zlib', 'base64': True }), indent=4, default=jd.jsonfilter)) print('== Decoding a JData-encoded data and printed in JSON format ==') newdata = data.copy() print( jd.show(jd.decode(jd.encode(newdata)), indent=4, default=jd.jsonfilter)) print('== Saving encoded data to test.json ==') jd.save(data, 'test.json') print('== Loading data from test.json and decode ==') newdata = jd.load('test.json') print(jd.show(newdata, indent=4, default=jd.jsonfilter))
def test_module(self): data = OrderedDict() data['const'] = [ 2.0, 1, True, False, None, float('nan'), float('-inf') ] data['shortarray'] = [1, 2, 3] data['a_complex'] = 1 + 2.0j data['object'] = [[[1], [2], [3]], None, False] data['a_typedarray'] = np.asarray([9, 9, 9, 9], dtype=np.uint8) data['a_ndarray'] = np.arange(1, 10, dtype=np.int32).reshape(3, 3) data['a_biginteger'] = 9007199254740991 data['a_map'] = { float('nan'): 'one', 2: 'two', "k": 'three' } print('== Original Python native data ==') newdata = data.copy() print(newdata) print('== JData-annotated data ==') print(jd.show(jd.encode(newdata), indent=4, default=jd.jsonfilter)) print( '== JData-annotated data exported to JSON with zlib compression ==' ) newdata = data.copy() print( jd.show(jd.encode(newdata, { 'compression': 'zlib', 'base64': True }), indent=4, default=jd.jsonfilter)) print('== Decoding a JData-encoded data and printed in JSON format ==') newdata = data.copy() print( jd.show(jd.decode(jd.encode(newdata)), indent=4, default=jd.jsonfilter)) print('== Saving encoded data to test.json ==') jd.save(data, 'test.json') print('== Loading data from test.json and decode ==') newdata = jd.load('test.json') print(jd.show(newdata, indent=4, default=jd.jsonfilter))
def loadt(fname, opt={}, **kwargs): """@brief Loading a text-based (JSON) JData file and decode it to native Python data @param[in] fname: a text JData (JSON based) file name @param[in] opt: options, if opt['decode']=True or 1 (default), call jdata.decode() after loading """ kwargs.setdefault('strict',False); kwargs.setdefault('object_pairs_hook',OrderedDict); opt.setdefault('decode',True); with open(fname, "r") as fid: data=json.load(fid, **kwargs); if(opt['decode']): data=jd.decode(data,opt); return data
def loadb(fname, opt={}, **kwargs): """@brief Loading a binary (UBJSON) JData file and decode it to native Python data @param[in] fname: a binary (UBJSON based) JData file name @param[in] opt: options, if opt['decode']=True or 1 (default), call jdata.decode() before saving """ opt.setdefault('decode',True) try: import ubjson except ImportError: raise ImportError('To read/write binary JData files, you must install the py-ubjson module by "pip install py-ubjson"') else: with open(fname, "r") as fid: data=ubjson.load(fid,**kwargs); if(opt['decode']): data=jd.decode(data,opt); return data