Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

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 […]

Download files

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

Files for yapic.json, version 1.6.3
Filename, size File type Python version Upload date Hashes
Filename, size yapic.json-1.6.3-cp35-cp35m-macosx_10_9_x86_64.whl (90.6 kB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp36-cp36m-macosx_10_9_x86_64.whl (87.9 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp36-cp36m-manylinux1_i686.whl (411.1 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp36-cp36m-manylinux1_x86_64.whl (437.3 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp36-cp36m-win32.whl (57.1 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp36-cp36m-win_amd64.whl (64.1 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp37-cp37m-macosx_10_9_x86_64.whl (87.9 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp37-cp37m-manylinux1_i686.whl (411.2 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp37-cp37m-manylinux1_x86_64.whl (437.4 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp37-cp37m-win32.whl (57.1 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp37-cp37m-win_amd64.whl (64.1 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp38-cp38-macosx_10_9_x86_64.whl (88.4 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp38-cp38-manylinux1_i686.whl (413.8 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp38-cp38-manylinux1_x86_64.whl (440.8 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp38-cp38-win32.whl (57.2 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp38-cp38-win_amd64.whl (64.7 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp39-cp39-macosx_10_9_x86_64.whl (88.3 kB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp39-cp39-manylinux1_i686.whl (409.0 kB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp39-cp39-manylinux1_x86_64.whl (433.9 kB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp39-cp39-win32.whl (57.1 kB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size yapic.json-1.6.3-cp39-cp39-win_amd64.whl (64.6 kB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size yapic.json-1.6.3.tar.gz (103.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page