Skip to main content

Ultra fast JSON encoder and decoder for Python with NumPy support

Project description

nujson: The UltraJSON Fork That Supports Numpy Serialization That Actually Includes Python Wheels

Inspired by Pandas' ujson

Build Status Version Status

How to install

Python versions: Python 3.5+

Using pip:

pip install shaped-nujson

If you get an error like this:

ERROR: Could not find a version that satisfies the requirement numpy>=1.16.4 (from nujson) (from versions: 1.9.3)
ERROR: No matching distribution found for numpy>=1.16.4 (from nujson)

Try this:

pip uninstall numpy
pip install numpy==1.16.4
pip install shaped-nujson

Example

>>> import numpy as np
>>> import nujson as ujson
>>> a = {"a": np.int64(100)}
>>> ujson.dumps(a)
'{"a":100}'
>>> a["b"] = np.float64(10.9)
>>> ujson.dumps(a)
'{"a":100,"b":10.9}'
>>> a["c"] = np.str_("12")
>>> ujson.dumps(a)
'{"a":100,"b":10.9,"c":"12"}'
>>> a["d"] = np.array(list(range(10)))
>>> ujson.dumps(a)
'{"a":100,"b":10.9,"c":"12","d":[0,1,2,3,4,5,6,7,8,9]}'
>>> a["e"] = np.repeat(3.9, 4)
>>> ujson.dumps(a)
'{"a":100,"b":10.9,"c":"12","d":[0,1,2,3,4,5,6,7,8,9],"e":[3.9,3.9,3.9,3.9]}'

Why make such a package and what is modified?

On Python 3, some data types of NumPy are not serializable. Here are some references:

One solution is type conversion like: int(numpy.int64) and numpy.array.tolist(). But it's not good for performance. After searching the Internet, we found an unmaintained project Komnomnomnom/ultrajson which recommends to use Pandas' ujson.

We tried but found Pandas is too heavy for our projects. So we decided to build our own lightweight fork. Currently, esn's ujson master branch has some problems we needed to solve, and the master branch is based on the the v1.35 ujson.

The main point is to convert NumPy data types in C, with calling NumPy's header. Commit 187bd15 has the most changes we made to support NumPy, and commit afedc42 fixes a build issue on macOS caused by Clang.

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

shaped-nujson-1.35.2.tar.gz (186.6 kB view hashes)

Uploaded Source

Built Distributions

shaped_nujson-1.35.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (79.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.28+ x86-64

shaped_nujson-1.35.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (80.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.28+ x86-64

shaped_nujson-1.35.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (77.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.28+ x86-64

shaped_nujson-1.35.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (76.9 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.28+ 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