55Copyright (c) 2019-2024 Qianqian Fang <q.fang at neu.edu>
66"""
77
8- __all__ = ["encode" , "decode" , "jdtype" , "jsonfilter" ]
8+ __all__ = [
9+ "encode" ,
10+ "decode" ,
11+ "jdtype" ,
12+ "jsonfilter" ,
13+ "zlibencode" ,
14+ "zlibdecode" ,
15+ "gzipencode" ,
16+ "gzipdecode" ,
17+ "lzmaencode" ,
18+ "lzmadecode" ,
19+ "lz4encode" ,
20+ "lz4decode" ,
21+ "base64encode" ,
22+ "base64decode" ,
23+ ]
924
1025##====================================================================================
1126## dependent libraries
6782##====================================================================================
6883
6984
70- def encode (d , opt = {}):
85+ def encode (d , opt = {}, ** kwargs ):
7186 """@brief Encoding a Python data structure to portable JData-annotated dict constructs
7287
7388 This function converts complex data types (usually not JSON-serializable) into
@@ -82,6 +97,7 @@ def encode(d, opt={}):
8297 """
8398
8499 opt .setdefault ("inplace" , False )
100+ opt .update (kwargs )
85101
86102 if "compression" in opt :
87103 if opt ["compression" ] == "lzma" :
@@ -166,6 +182,7 @@ def encode(d, opt={}):
166182 elif opt ["compression" ] == "gzip" :
167183 gzipper = zlib .compressobj (wbits = (zlib .MAX_WBITS | 16 ))
168184 newobj ["_ArrayZipData_" ] = gzipper .compress (newobj ["_ArrayZipData_" ])
185+ newobj ["_ArrayZipData_" ] += gzipper .flush ()
169186 elif opt ["compression" ] == "lzma" :
170187 try :
171188 newobj ["_ArrayZipData_" ] = lzma .compress (
@@ -224,7 +241,7 @@ def encode(d, opt={}):
224241##====================================================================================
225242
226243
227- def decode (d , opt = {}):
244+ def decode (d , opt = {}, ** kwargs ):
228245 """@brief Decoding a JData-annotated dict construct into native Python data
229246
230247 This function converts portable JData-annotated dict/list constructs back to native Python
@@ -237,6 +254,7 @@ def decode(d, opt={}):
237254
238255 opt .setdefault ("inplace" , False )
239256 opt .setdefault ("maxlinklevel" , 0 )
257+ opt .update (kwargs )
240258
241259 if (
242260 (isinstance (d , str ) or type (d ) == "unicode" )
@@ -461,3 +479,112 @@ def decodelist(d0, opt={}):
461479
462480
463481# -------------------------------------------------------------------------------------
482+
483+
484+ def zlibencode (buf ):
485+ return zlib .compress (buf )
486+
487+
488+ # -------------------------------------------------------------------------------------
489+
490+
491+ def gzipencode (buf ):
492+ gzipper = zlib .compressobj (wbits = (zlib .MAX_WBITS | 16 ))
493+ newbuf = gzipper .compress (buf )
494+ newbuf += gzipper .flush ()
495+ return newbuf
496+
497+
498+ # -------------------------------------------------------------------------------------
499+
500+
501+ def lzmaencode (buf ):
502+ try :
503+ try :
504+ import lzma
505+ except ImportError :
506+ from backports import lzma
507+ except Exception :
508+ raise Exception (
509+ "JData" ,
510+ 'you must install "lzma" module to compress with this format' ,
511+ )
512+ return lzma .compress (buf , lzma .FORMAT_ALONE )
513+
514+
515+ # -------------------------------------------------------------------------------------
516+
517+
518+ def lz4encode (buf ):
519+ try :
520+ import lz4 .frame
521+ except ImportError :
522+ raise Exception (
523+ "JData" ,
524+ 'you must install "lz4" module to compress with this format' ,
525+ )
526+ return lz4 .compress (buf .tobytes (), lzma .FORMAT_ALONE )
527+
528+
529+ # -------------------------------------------------------------------------------------
530+
531+
532+ def base64encode (buf ):
533+ return base64 .b64encode (buf )
534+
535+
536+ # -------------------------------------------------------------------------------------
537+
538+
539+ def zlibdecode (buf ):
540+ return zlib .decompress (buf )
541+
542+
543+ # -------------------------------------------------------------------------------------
544+
545+
546+ def gzipdecode (buf ):
547+ return zlib .decompress (bytes (buf ), zlib .MAX_WBITS | 32 )
548+
549+
550+ # -------------------------------------------------------------------------------------
551+
552+
553+ def lzmadecode (buf ):
554+ try :
555+ try :
556+ import lzma
557+ except ImportError :
558+ from backports import lzma
559+ except Exception :
560+ raise Exception (
561+ "JData" ,
562+ 'you must install "lzma" module to compress with this format' ,
563+ )
564+ newbuf = bytearray (buf ) # set length to -1 (unknown) if EOF appears
565+ newbuf [5 :13 ] = b"\xff \xff \xff \xff \xff \xff \xff \xff "
566+ return lzma .decompress (newbuf , lzma .FORMAT_ALONE )
567+
568+
569+ # -------------------------------------------------------------------------------------
570+
571+
572+ def lz4decode (buf ):
573+ try :
574+ import lz4 .frame
575+ except ImportError :
576+ raise Exception (
577+ "JData" ,
578+ 'you must install "lz4" module to compress with this format' ,
579+ )
580+ return lz4 .frame .decompress (bytes (buf ))
581+
582+
583+ # -------------------------------------------------------------------------------------
584+
585+
586+ def base64decode (buf ):
587+ return base64 .b64decode (buf )
588+
589+
590+ # -------------------------------------------------------------------------------------
0 commit comments