Skip to main content

Fastest JSON encode / decode library.

Project description

AppVeyor CircleCI Travis PyPI - Downloads

yapic.json is an extreamly fast json encoder / decoder package for python. Encoding and decoding output fully compatible with python.json package.

Features

  • Extreamly fast (see benchmark results in ‘/benchmark’ directory)

  • Fully compatible output with Python json package

  • Builtin object serialization method __json__ (see below)

  • Strict JSON (RFC 4627) expected: Infinity, NaN (JavaScript compatible infinity and not a number symbols)

  • UTF-8 encoding & decoding support

  • Accurate float encoding & decoding

  • date / datetime / time encondig & decoding support (can encode subclasses)

  • uuid.UUID encoding support

  • ItemsView encoding support

    from collections.abc import ItemsView
    
    class MyDictGenerator(ItemsView):
       def __iter__(self):
          yield ("some_key", "some_value")
          yield ("meaning_of_life", 42)
    
    json.dumps(MyDictGenerator()) == '{"some_key":"some_value","meaning_of_life":42}'

Requirements

  • Only works with Python 3.5 or greater

  • c++ 11 comaptible compiler. (only if u want to build from source)

    Wheels provided for windows x86/x64 and linux x86/x64 and osx x64

Usage

Very similar that python.json, let’s see some example

Json data to python

from yapic import json

>>> json.loads('"Hello World"')
"Hello World"

Python object to json data

from yapic import json

>>> json.dumps("Hello World")
'"Hello World"'

class Point:
   def __json__(self):
      return {"x":1, "y":2}

>>> json.dumps(Point())
'{"x":1,"y":2}'

Functions

  • loads (s: bytes, str, *, object_hook: Callable[[dict], Any]]=None, parse_float: Callable[[str], Any]]=None, parse_date: bool=True)

    object_hook example:

    >>> from yapic import json
    >>> def hook(dict_):
    ...     if "__complex__" in dict_:
    ...         return complex(dict_["real"], dict_["imag"])
    ...
    >>> json.loads('{"__complex__":true, "real":1, "imag":2}',
    >>>     object_hook=hook)
    (1+2j)

    parse_float example:

    >>> from yapic import json
    >>> from decimal import Decimal
    >>> json.loads("1.2", parse_float=Decimal)
    Decimal('1.2')
  • dumps (obj: Any, *, default: Callable[[Any], JSONT]=None, tojson: str="__json__", ensure_ascii: bool=True, encode_datetime: bool=True) -> str

    default example:

    >>> from yapic import json
    >>> def default_func(o):
    ...     if isinstance(o, complex):
    ...         return {"__complex__": True, "real": 1, "imag": 2}
    ...
    >>> json.dumps(1 + 2j, default=default_func)
    '{"__complex__":true,"real":1,"imag":2}'

    tojson example:

    >>> from yapic import json
    >>> class Point(object):
    ...     def __init__(self, x, y):
    ...         self.x = x
    ...         self.y = y
    ...     def __json__(self):
    ...         return {"x": self.x, "y": self.y}
    ...
    >>> json.dumps(Point(10, 20))
    '{"x":10,"y":20}'
  • dumpb (obj: Any, *, default: Callable[[Any], JSONT]=None, tojson: str="__json__", ensure_ascii: bool=True, encode_datetime: bool=True) -> bytes

    Same as dumps, but this function is return bytes insted of str

Exceptions

  • yapic.json.JsonError: base exception class

  • yapic.json.JsonEncodeError: exception class for encoding errors

  • yapic.json.JsonDecodeError: exception class for decoding errors

Json to Python translations

Json

Python

“string”

str

42

int

4.2, 4e2

float (unless you specify parse_float)

Infinity

float(“inf”)

NaN

float(“NaN”)

true

True

false

False

null

None

2000-01-01 12:34:56

datetime without timezone

2000-01-01 12:34:56Z

datetime with utc timezone

2000-01-01 12:34:56+0300

datetime with custom timezone

2000-01-01

date

10:12:34

time without timezone

10:12:34+0300

time with custom timezone

{…}

dict (unless you specify object_hook)

[…]

list

Python to Json translations

Python

Json

str

“…”

int(42)

42

float(4.2), Decimal(4.2)

4.2

float(“inf”), Decimal(“inf”)

Infinity

float(“nan”), Decimal(“nan”)

NaN

True

true

False

false

None

null

datetime

“2000-01-01 12:34:56”, “2000-01-01T12:34:56+0300”

date

“2000-01-01”

time

“12:34:56”, “12:34:56+0300”

UUID

“aba04c17-6ea3-48c1-8dcd-74f0a9b79bee”

dict, ItemsView

{…}

list, tuple, set, iterable

[…]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yapic.json-1.5.1.tar.gz (102.5 kB view hashes)

Uploaded Source

Built Distributions

yapic.json-1.5.1-cp38-cp38-win_amd64.whl (66.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

yapic.json-1.5.1-cp38-cp38-win32.whl (58.9 kB view hashes)

Uploaded CPython 3.8 Windows x86

yapic.json-1.5.1-cp38-cp38-manylinux1_x86_64.whl (435.1 kB view hashes)

Uploaded CPython 3.8

yapic.json-1.5.1-cp38-cp38-manylinux1_i686.whl (423.4 kB view hashes)

Uploaded CPython 3.8

yapic.json-1.5.1-cp38-cp38-macosx_10_9_x86_64.whl (104.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

yapic.json-1.5.1-cp37-cp37m-win_amd64.whl (66.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

yapic.json-1.5.1-cp37-cp37m-win32.whl (58.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

yapic.json-1.5.1-cp37-cp37m-manylinux1_x86_64.whl (427.4 kB view hashes)

Uploaded CPython 3.7m

yapic.json-1.5.1-cp37-cp37m-manylinux1_i686.whl (412.1 kB view hashes)

Uploaded CPython 3.7m

yapic.json-1.5.1-cp37-cp37m-macosx_10_9_x86_64.whl (103.4 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

yapic.json-1.5.1-cp36-cp36m-win_amd64.whl (66.2 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

yapic.json-1.5.1-cp36-cp36m-win32.whl (58.6 kB view hashes)

Uploaded CPython 3.6m Windows x86

yapic.json-1.5.1-cp36-cp36m-manylinux1_x86_64.whl (427.7 kB view hashes)

Uploaded CPython 3.6m

yapic.json-1.5.1-cp36-cp36m-manylinux1_i686.whl (412.1 kB view hashes)

Uploaded CPython 3.6m

yapic.json-1.5.1-cp36-cp36m-macosx_10_9_x86_64.whl (103.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

yapic.json-1.5.1-cp35-cp35m-win_amd64.whl (67.3 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

yapic.json-1.5.1-cp35-cp35m-win32.whl (59.2 kB view hashes)

Uploaded CPython 3.5m Windows x86

yapic.json-1.5.1-cp35-cp35m-manylinux1_x86_64.whl (436.4 kB view hashes)

Uploaded CPython 3.5m

yapic.json-1.5.1-cp35-cp35m-manylinux1_i686.whl (412.3 kB view hashes)

Uploaded CPython 3.5m

yapic.json-1.5.1-cp35-cp35m-macosx_10_9_x86_64.whl (103.8 kB view hashes)

Uploaded CPython 3.5m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page