The # -*- coding:utf-8 -*-' test proves that Bson and JSON require a similar number of bytes, or more. The effect of the compression algorithm depends on its degree of redundancy. "' From __future__ import divisionimport collections # from python standard library.import datetimetest = {"name": "Lemo", "Age":12, "Address":{ "City": "Suzhou", "Country": "China", "code":215000} , # "Timestamp":d atetime.datetime.now (), "scores": [{"Name": "中文版", "Grade": 3.0, "code":215000}, {"name": "Chinese", "Grade": 2.0, "code":215000}, {"name": "Kerea", "Grade": 3.0, "code":232000}, {"Name": "France", "Grade": 4.0, "code":235000}, {"name": "Japanese", "Grade": 4.0, "code":235000}, {"name": "Kerea", "Grade": 4.0, " Code ": 235000}, {" name ":" Kerea "," Grade ": 4.0," code ": 235300} ]} #import bson#from bson.codec_options import codecoptions#data = bson. Bson.encode (test) #print ("BSON:", len (data) ) #decoded_doc = bson. Bson.decode (data) import jsonjson_str = json.dumps ( test ) print ("JSON:", Len (JSON _STR) ) ### import bz2compressor = bz2. Bz2compressor () compressor.compress (JSON_str.encode (' Utf-8 ')) bz2_str = compressor.flush () print ("bz2:", Len (BZ2_STR), len (BZ2_STR)/len (json_str) ) Import sysif sys.version_info.major*1000+ sys.version_ Info.minor>=3003: import lzma lzc = lzma. Lzmacompressor () out1 = lzc.compress (Json_str.encode (' Utf-8 ') ) out2 = lzc.flush () # concatenate all the partial results: result = b "". Join ([Out1, out2]) print ("Lzma:", Len (Result), len (Result)/len (JSON_STR) ) lzd = lzma. Lzmadecompressor () dec_str = lzd.decompress (result ) assert json_str.encode (' Utf-8 ') == dec_str### msgpackimport datetimeimport msgpack#useful_dict = { # "id": 1, # "created": Datetime.datetime.now (), #}def decode_datetime (obj): if b ' __datetime__ ' In obj: obj = datetime.datetime.strptime (obj["as _str "], "%y%m%dt%h:%m:%s.%f ") return objdef encode_datetime (obj): if isinstance (obj, datetime.datetime): return {' __datetime__ ': true, ' as_str ': obj.strftime ("%y%m%dt%h:%m:%s.%f")}    RETURN OBJPACKED_DICT = MSGPACK.PACKB (Test, default=encode_datetime) print ("Msgpack:", Len (packed_dict), len (packed_dict)/len (json_str) ) #compressor = bz2. Bz2compressor (compresslevel=1) #compressor. Compress (packed_dict) #bz2_str = compressor.flush () #print ("msgpack_bz2:", Len (BZ2_STR), len (BZ2_STR)/len (json_str) ) this_dict_again = MSGPACK.UNPACKB (Packed_dict, object_hook=decode_datetime)
Initial test of encoding efficiency in various formats under Python (Json,bson, bz2, Lzma,msgpack)