JSON encoding/decoding for Numpy arrays and scalars
Project description
json-numpy
Description
json-numpy
provides lossless and quick JSON encoding/decoding for NumPy arrays and scalars.
json-numpy
follows Semantic Versioning.
Installation
json-numpy
can be installed using pip
:
$ pip install json-numpy
Usage
For a quick start, json_numpy
can be used as a simple 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.
Tests
The simplest way to run tests is:
$ python -m unittest
As a more robust alternative, you can install tox
to automatically test across the supported python versions, then run:
$ tox
Issue tracker
Please report any bugs or enhancement ideas using the issue tracker.
License
json-numpy
is licensed under the terms of the MIT License (see 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
File details
Details for the file json_numpy-2.1.0.tar.gz
.
File metadata
- Download URL: json_numpy-2.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b2cf6e54c69c769f88480d12d0361f207c39d7626961b4b1ee92f3156425a15 |
|
MD5 | 75219fa61649cffea66fdaa92194a198 |
|
BLAKE2b-256 | 52dd62a50cdb5dad32be516dfddb40ef2f623d9525f5c42cf3738e01edeff872 |
File details
Details for the file json_numpy-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: json_numpy-2.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 366b5c38fe38f22f604e0f930abf1dee0028b06c1ae297302ebf945d0901e1a2 |
|
MD5 | 08f5ecec0408dfed28cd2bcc0bfccd1e |
|
BLAKE2b-256 | 2dceaa15955ac7035c53a6852fadc3049506e91c89641e814a5711264a8dc856 |