JSON encoding/decoding for Numpy arrays and scalars
Project description
json-numpy
Description
This package provides lossless and quick JSON encoding/decoding for numpy
arrays and scalars.
Installation
json-numpy
can be installed using pip
:
$ pip install json-numpy
Alternatively, you can download the repository and run the following command from within the source directory:
$ python setup.py install
Usage
For a quick start, json_numpy
can be used as a basic drop-in replacement of the built-in json
module.
The dump()
, load()
, dumps()
and loads()
methods are implemented by wrapping the original methods and replacing the default encoder and decoder.
More information on the usage can be found in the json
module's documentation.
import numpy as np
import json_numpy
arr = np.array([0, 1, 2])
encoded_arr_str = json_numpy.dumps(arr)
decoded_arr = json_numpy.loads(encoded_arr_str)
Another way of using json_numpy
is to explicitly use the provided encoder and decoder functions in conjunction with the json
module.
import json
import numpy as np
from json_numpy import default, object_hook
arr = np.array([0, 1, 2])
encoded_arr_str = json.dumps(arr, default=default)
decoded_arr = json.loads(encoded_arr_str, object_hook=object_hook)
Finally, the last way of using json_numpy
is by monkey patching the json
module after importing it first:
import json
import numpy as np
import json_numpy
json_numpy.patch()
arr = np.array([0, 1, 2])
encoded_arr_str = json.dumps(arr)
decoded_arr = json.loads(encoded_arr_str)
This method can be used to change the behavior of a module depending on the json
module without editing its code.
Development
The latest source code can be obtained from GitHub.
json-numpy
maintains compatibility with python versions >= 3.6.
Install tox
to support testing across multiple python versions in your development environment.
If you use conda
to install python
use tox-conda
to automatically manage
testing across all supported python versions.
# Using a system python
$ pip install tox
# Additionally, using a conda-provided python
$ pip install tox tox-conda
Execute tests across supported python versions:
$ tox
License
json-numpy
is licensed under the terms of the MIT License (see the file LICENSE.txt for more information).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for json_numpy-1.0.0a1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 232eef3ca033019a5736e058bd953b2d981d638a00ff7b92f0a08468183c5f51 |
|
MD5 | 4a18889cfd64111786e41d15467ab5d1 |
|
BLAKE2b-256 | ae4ba60ff20e31b367a84858683f5ba704cff53f5070826a59bc9b13c98aa563 |