快速JSON编码器

metamagic.json的Python项目详细描述


为python对象设计的json编码器的超高速python 3实现 与各种web浏览器中的本地json解码器兼容。

可以编码为python字符串(请参见dumps)或序列 字节数(请参阅dumpb)。保证dumps()返回的字符串 只有7位ascii字符[1]dumps(obj).encode('ascii') = dumpb(obj)

支持一个特殊的编码器类方法encode_hook(obj),如果存在的话,它将应用于 输入对象和其余处理将应用于encode_hook()的输出。 注意:encode_hook()应该始终返回一个对象;对于不应该 特殊编码的encode_hook()应该返回原始对象。

通过使用对象__mm_json__()或^{tt5}支持自定义编码器$ 方法(如果可用)。对于所有非本机类型__mm_json__和 然后__mm_serialize__将在尝试对对象[2]进行编码之前进行尝试。 __mm_serialize__的输出依次编码为任何其他对象(并可能依次具有 是否支持__mm_serialize__方法)。

本机支持字符串、整数、浮点、真、假、无、列表、元组, dicts,sets,frozensets,collections.ordereddicts,colections.set,集合,集合 collections.sequence[3],collections.mapping,uuid.uuids[4],decimal.decimals, DateTime.DateTime和从所有列出的对象派生的对象。

对于所有不能以任何其他方式编码的对象 试图使用^{tt10}将对象转换为可编码对象$ 方法。如果Encoder.default成功,则输出将再次编码为任何其他对象。

如果需要自定义处理本机支持的基元类型,则 {TT12} $方法存在。

示例

编码自定义对象:

>>> from metamagic.json import dumps, dumpb

>>> class Foo:
...     def __mm_serialize__(self):
...         return "Im foo"
...

>>> dumps(Foo())
'"Im foo"'

>>> dumps({Foo(): 123})
'{"Im foo":123}'

直接转储到字节:

>>> dumpb(Foo())
b'"Im foo"'

绕过json缓冲区的序列化:

>>> class JSONData:
...     def __init__(self, data):
...         self.data = data
...
...     def __mm_json__(self):
...         return self.data

>>> a = JSONData('["abc", "def"]')
>>> dumps([1,2,3,a])
'[1,2,3,["abc", "def"]]'

引发异常

  • dumps()dumpb()都会引发不支持对象的类型错误,并且 对于不是字符串(或uuid[5])的所有字典键 它们的__mm_serialize__方法不能表示为字符串(或uuid)。
  • default()为所有不支持的对象引发typeerror,并覆盖default() 还应为它不支持的所有对象引发Typeerror。
  • 当编码整数时,dumps()dumpb()如果整数 值大于JavaScript支持的最大整型值 (9007199254740992,请参阅http://ecma262-5.com/ELS5_HTML.htm#Section_8.5)。
  • 对嵌套对象进行编码时,如果深度超过 允许的嵌套级别(默认为100,可以通过传递 作为dumps()dumpb()方法的第二个参数的所需值

基准

Array with 256 short ascii strings:
  std json:   2.6581 sec,      11286 req/sec
 mm c json:  0.90321 sec,      33214 req/sec  ( 2.9x )
 mm c 1ini:  0.85578 sec,      35055 req/sec  ( 3.1x )
mm c dumpb:  0.84517 sec,      35495 req/sec  ( 3.1x )
   marshal:  1.77567 sec,      16895 req/sec

Array with 2048 3-char ascii strings:
  std json:  0.47259 sec,       4231 req/sec
 mm c json:  0.12481 sec,      16024 req/sec  ( 3.8x )
 mm c 1ini:  0.12261 sec,      16311 req/sec  ( 3.9x )
mm c dumpb:  0.11732 sec,      17047 req/sec  ( 4.0x )
   marshal:  0.41334 sec,       4838 req/sec

Array with 256 long ascii strings:
  std json:  1.30821 sec,       3822 req/sec
 mm c json:  0.47144 sec,      10605 req/sec  ( 2.8x )
 mm c 1ini:  0.44159 sec,      11322 req/sec  ( 3.0x )
mm c dumpb:   0.4346 sec,      11504 req/sec  ( 3.0x )
   marshal:  0.85593 sec,       5841 req/sec

Array with 256 long utf-8 strings:
  std json:  1.52419 sec,       1312 req/sec
 mm c json:   1.4438 sec,       1385 req/sec  ( 1.1x )
 mm c 1ini:  1.43666 sec,       1392 req/sec  ( 1.1x )
mm c dumpb:  1.40142 sec,       1427 req/sec  ( 1.1x )
   marshal:   1.3413 sec,       1491 req/sec

Medium complex object:
  std json:   3.5078 sec,       2850 req/sec
 mm c json:  1.45764 sec,       6860 req/sec  ( 2.4x )
 mm c 1ini:  1.43357 sec,       6975 req/sec  ( 2.4x )
mm c dumpb:  1.47626 sec,       6773 req/sec  ( 2.4x )
   marshal:  1.04175 sec,       9599 req/sec

Array with 256 doubles:
  std json:  3.37919 sec,       2959 req/sec
 mm c json:  2.23615 sec,       4471 req/sec  ( 1.5x )
 mm c 1ini:  2.48201 sec,       4028 req/sec  ( 1.4x )
mm c dumpb:  2.23184 sec,       4480 req/sec  ( 1.5x )
   marshal:  0.14098 sec,      70932 req/sec

Array with 256 ints:
  std json:   1.0185 sec,      19636 req/sec
 mm c json:   0.2752 sec,      72674 req/sec  ( 3.7x )
 mm c 1ini:  0.25349 sec,      78898 req/sec  ( 4.0x )
mm c dumpb:  0.28252 sec,      70791 req/sec  ( 3.6x )
   marshal:  0.15442 sec,     129516 req/sec

Array with 256 small ints:
  std json:  1.04397 sec,     191576 req/sec
 mm c json:  0.28152 sec,     710429 req/sec  ( 3.7x )
 mm c 1ini:  0.09222 sec,    2168726 req/sec  ( 11.3x )
mm c dumpb:  0.27627 sec,     723929 req/sec  ( 3.8x )
   marshal:  0.08306 sec,    2407897 req/sec

Array with 256 Decimals:
  std json:     failed to serialize
 mm c json:  0.77161 sec,      10367 req/sec  ( 0.0x )
 mm c 1ini:  0.76022 sec,      10523 req/sec  ( 0.0x )
mm c dumpb:  0.78671 sec,      10168 req/sec  ( 0.0x )
   marshal:     failed to serialize

Array with 256 True values:
  std json:  2.08432 sec,      38381 req/sec
 mm c json:  0.47159 sec,     169638 req/sec  ( 4.4x )
 mm c 1ini:  0.39814 sec,     200934 req/sec  ( 5.2x )
mm c dumpb:  0.45191 sec,     177026 req/sec  ( 4.6x )
   marshal:  0.24776 sec,     322893 req/sec

Array with 256 False values:
  std json:   2.0099 sec,      39802 req/sec
 mm c json:  0.50992 sec,     156887 req/sec  ( 3.9x )
 mm c 1ini:  0.43001 sec,     186042 req/sec  ( 4.7x )
mm c dumpb:  0.50839 sec,     157359 req/sec  ( 4.0x )
   marshal:  0.25551 sec,     313099 req/sec

Array with 256 dict{string, int} pairs:
  std json:  1.96227 sec,       4076 req/sec
 mm c json:  0.36569 sec,      21876 req/sec  ( 5.4x )
 mm c 1ini:  0.34565 sec,      23144 req/sec  ( 5.7x )
mm c dumpb:  0.36583 sec,      21868 req/sec  ( 5.4x )
   marshal:  0.51862 sec,      15425 req/sec

Array with 256 dict-based{string, int} pairs:
  std json:  4.20194 sec,       1903 req/sec
 mm c json:  3.74071 sec,       2138 req/sec  ( 1.1x )
 mm c 1ini:  3.70554 sec,       2158 req/sec  ( 1.1x )
mm c dumpb:  3.77039 sec,       2121 req/sec  ( 1.1x )
   marshal:     failed to serialize

Array with 256 orderedDict{string, int} pairs:
  std json:  2.31765 sec,        431 req/sec
 mm c json:  0.70724 sec,       1413 req/sec  ( 3.3x )
 mm c 1ini:  0.69506 sec,       1438 req/sec  ( 3.3x )
mm c dumpb:  0.70373 sec,       1420 req/sec  ( 3.3x )
   marshal:     failed to serialize

Dict with 256 arrays with 256 dict{string, int} pairs:
  std json:  3.78828 sec,         13 req/sec
 mm c json:  0.69496 sec,         71 req/sec  ( 5.5x )
 mm c 1ini:  0.69522 sec,         71 req/sec  ( 5.4x )
mm c dumpb:  0.68382 sec,         73 req/sec  ( 5.5x )
   marshal:  1.02776 sec,         48 req/sec

测试

^运行测试需要{tt23}$。

[1]All characters required to be escaped by the JSON spec @ http://json.org are escaped
[2]If present, encode_hook() is applied before and independently of all other encoders
[3]To avoid errors in the metamagic framework ^{tt24}$, ^{tt25}$ and derived classes are deliberately not encoded using the built-in sequence encoder; the only way to encode these objects is to either overwrite the encoders’ default() method or to provide __mm_serialize__ method in the object being serialized.
[4]UUIDs and Decimals are encoded as strings.
[5]JSON specification only supports string dictionary keys; since UUIDs are also encoded to strings and are a common key in the metamagic framework, this encoder also supports UUIDs as dictionary keys.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我想访问同一类的不同函数中的函数变量   创建字段/值哈希映射的java反射   java Velocity工具向后兼容性?   cmd JAVA运行时错误:发生JNI错误   java spring引导示例hateoas不工作   java为什么Android Studio和gradle可以使用不同的类路径进行单元测试?   java Selenium Firefox驱动程序初始化   java如何在jar中重新定位gradle依赖项的包   java为什么我的单线程hello world应用程序使用22个OS线程?   正则表达式替换Java字符串中的特殊字符   在任何派生类的构造函数之后运行方法的java   java从输入字符串中找出日期   带有libGdx(Java)音乐的安卓studio可以在Android模拟器上工作,但不能在桌面上工作   java我在getconnection的参数方面有错误吗?   java使用JFileChooser访问选定文件   java如何将Json解析为另一个活动   java使用Resources for html和rest server   java更新列表,列表内容来自循环中的方法   java如何在GLSL中读取完整范围的32位整数纹理