numpy-json


Namenumpy-json JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/featrix/numpy-json
SummaryJSON encoder for NumPy arrays and extended Python data types
upload_time2025-10-08 02:15:15
maintainerNone
docs_urlNone
authorFeatrix, Inc.
requires_python>=3.8
licenseMIT
keywords numpy json encoder serialization array scientific computing data science
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # numpy-json

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)

A robust JSON encoder for NumPy arrays and extended Python data types. No more writing custom serialization wrappers — just drop in `NumpyJSONEncoder` and go.

## Features

**numpy-json** seamlessly handles:

- **NumPy arrays** → JSON lists
- **NumPy scalars** (int, float, bool) → native JSON types
- **NumPy datetime64/timedelta64** → ISO 8601 strings
- **Python datetime/date/time** → ISO 8601 strings
- **UUID** → string representation
- **Decimal** → float (default) or string (configurable)
- **set/tuple** → JSON lists
- **bytes/bytearray** → base64 encoded strings (configurable)
- **pathlib.Path** → string paths
- **pandas types** (optional, no hard dependency) → appropriate JSON types

## Installation

```bash
pip install numpy-json
```

Or install from source:

```bash
git clone https://github.com/featrix/numpy-json.git
cd numpy-json
pip install -e .
```

## Quick Start

```python
import json
import numpy as np
from numpy_json import NumpyJSONEncoder

# Your data with NumPy arrays
data = {
    "array": np.array([1, 2, 3, 4, 5]),
    "matrix": np.array([[1, 2], [3, 4]]),
    "scalar": np.float64(3.14159),
    "date": np.datetime64('2025-01-15'),
}

# Encode to JSON - it just works!
json_str = json.dumps(data, cls=NumpyJSONEncoder)
print(json_str)
```

**Output:**
```json
{
  "array": [1, 2, 3, 4, 5],
  "matrix": [[1, 2], [3, 4]],
  "scalar": 3.14159,
  "date": "2025-01-15"
}
```

## Usage

### Basic Usage

Simply pass `NumpyJSONEncoder` as the `cls` parameter to `json.dumps()`:

```python
import json
from numpy_json import NumpyJSONEncoder

json_str = json.dumps(your_data, cls=NumpyJSONEncoder)
```

### Configuration Options

The encoder provides class-level configuration:

#### Decimal Handling

```python
from numpy_json import NumpyJSONEncoder
import decimal

# Default: convert Decimal to float (lossy for very precise decimals)
data = {"price": decimal.Decimal("19.99")}
json.dumps(data, cls=NumpyJSONEncoder)  # → {"price": 19.99}

# Alternative: preserve as string
NumpyJSONEncoder.DECIMAL_AS_STR = True
json.dumps(data, cls=NumpyJSONEncoder)  # → {"price": "19.99"}
```

#### Binary Data Handling

```python
from numpy_json import NumpyJSONEncoder

data = {"binary": b"hello world"}

# Default: base64 encoding
json.dumps(data, cls=NumpyJSONEncoder)  
# → {"binary": "aGVsbG8gd29ybGQ="}

# Alternative: emit as integer list
NumpyJSONEncoder.BASE64_BYTES = False
json.dumps(data, cls=NumpyJSONEncoder)
# → {"binary": [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]}
```

### Handling NaN and Infinity

By default, Python's `json` module allows `NaN` and `Infinity` values (which are not valid in RFC 8259 JSON). If you need strict JSON compliance:

```python
import json
import numpy as np
from numpy_json import NumpyJSONEncoder, sanitize_nans

data = {
    "valid": np.array([1.0, 2.0, 3.0]),
    "invalid": np.array([1.0, np.nan, np.inf, -np.inf]),
}

# Clean the data first
clean_data = sanitize_nans(data)
# NaN/Inf values are replaced with None

# Then encode with allow_nan=False for strict JSON
json_str = json.dumps(clean_data, cls=NumpyJSONEncoder, allow_nan=False)
```

**Result:**
```json
{
  "valid": [1.0, 2.0, 3.0],
  "invalid": [1.0, null, null, null]
}
```

### Advanced Example

```python
import json
import numpy as np
from datetime import datetime
from uuid import uuid4
from pathlib import Path
from numpy_json import NumpyJSONEncoder

complex_data = {
    "id": uuid4(),
    "timestamp": datetime.now(),
    "measurements": np.array([1.5, 2.3, 3.7, 4.1]),
    "matrix": np.random.rand(3, 3),
    "metadata": {
        "path": Path("/tmp/data.csv"),
        "tags": {"ml", "experiment", "2025"},
        "coordinates": (40.7128, -74.0060),
    },
    "config": {
        "enabled": np.bool_(True),
        "threshold": np.float32(0.85),
        "max_items": np.int64(1000),
    }
}

json_str = json.dumps(complex_data, cls=NumpyJSONEncoder, indent=2)
print(json_str)
```

## API Reference

### `NumpyJSONEncoder`

**Class Attributes:**
- `DECIMAL_AS_STR` (bool): If `True`, encode `Decimal` as string. Default: `False`
- `BASE64_BYTES` (bool): If `True`, encode bytes as base64. Default: `True`

**Methods:**
- `default(obj)`: Override method that handles type conversion

### `sanitize_nans(obj)`

Recursively replace `NaN`, `Inf`, and `-Inf` values with `None`.

**Parameters:**
- `obj`: Any Python object (dict, list, NumPy array, etc.)

**Returns:**
- Object with all NaN/Inf values replaced with `None`

## Type Conversion Table

| Python/NumPy Type | JSON Output | Notes |
|-------------------|-------------|-------|
| `np.ndarray` | Array | Recursive conversion via `.tolist()` |
| `np.int*` | Number | All NumPy integer types |
| `np.float*` | Number | All NumPy float types |
| `np.bool_` | Boolean | NumPy boolean |
| `np.datetime64` | String | ISO 8601 format |
| `np.timedelta64` | String | Duration string |
| `datetime`/`date`/`time` | String | ISO 8601 format |
| `uuid.UUID` | String | Standard UUID string |
| `decimal.Decimal` | Number or String | Configurable via `DECIMAL_AS_STR` |
| `set`/`tuple` | Array | Converted to list |
| `bytes`/`bytearray` | String or Array | Configurable via `BASE64_BYTES` |
| `pathlib.Path` | String | String representation |
| pandas types | String or `null` | Optional support, no hard dependency |

## Requirements

- Python 3.8+
- NumPy

## Development

### Setup

```bash
git clone https://github.com/featrix/numpy-json.git
cd numpy-json
pip install -e ".[dev]"
```

### Running Tests

```bash
pytest tests/
```

### Code Style

This project uses:
- `black` for code formatting
- `flake8` for linting
- `mypy` for type checking

```bash
black numpy_json/
flake8 numpy_json/
mypy numpy_json/
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Copyright

Copyright (c) 2025 Featrix, Inc.

## Acknowledgments

- Built to solve real-world data serialization challenges in scientific computing and machine learning workflows
- Inspired by the need for seamless NumPy integration with JSON APIs

## Support

For issues, questions, or contributions, please visit the [GitHub repository](https://github.com/featrix/numpy-json).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/featrix/numpy-json",
    "name": "numpy-json",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "\"Featrix, Inc.\" <info@featrix.com>",
    "keywords": "numpy, json, encoder, serialization, array, scientific computing, data science",
    "author": "Featrix, Inc.",
    "author_email": "\"Featrix, Inc.\" <info@featrix.com>",
    "download_url": "https://files.pythonhosted.org/packages/ba/d7/7103a6e64dce6a37ef04c0439b7c10ed843c526bfb108b7be0c0d34868bd/numpy_json-0.1.0.tar.gz",
    "platform": null,
    "description": "# numpy-json\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)\n\nA robust JSON encoder for NumPy arrays and extended Python data types. No more writing custom serialization wrappers \u2014 just drop in `NumpyJSONEncoder` and go.\n\n## Features\n\n**numpy-json** seamlessly handles:\n\n- **NumPy arrays** \u2192 JSON lists\n- **NumPy scalars** (int, float, bool) \u2192 native JSON types\n- **NumPy datetime64/timedelta64** \u2192 ISO 8601 strings\n- **Python datetime/date/time** \u2192 ISO 8601 strings\n- **UUID** \u2192 string representation\n- **Decimal** \u2192 float (default) or string (configurable)\n- **set/tuple** \u2192 JSON lists\n- **bytes/bytearray** \u2192 base64 encoded strings (configurable)\n- **pathlib.Path** \u2192 string paths\n- **pandas types** (optional, no hard dependency) \u2192 appropriate JSON types\n\n## Installation\n\n```bash\npip install numpy-json\n```\n\nOr install from source:\n\n```bash\ngit clone https://github.com/featrix/numpy-json.git\ncd numpy-json\npip install -e .\n```\n\n## Quick Start\n\n```python\nimport json\nimport numpy as np\nfrom numpy_json import NumpyJSONEncoder\n\n# Your data with NumPy arrays\ndata = {\n    \"array\": np.array([1, 2, 3, 4, 5]),\n    \"matrix\": np.array([[1, 2], [3, 4]]),\n    \"scalar\": np.float64(3.14159),\n    \"date\": np.datetime64('2025-01-15'),\n}\n\n# Encode to JSON - it just works!\njson_str = json.dumps(data, cls=NumpyJSONEncoder)\nprint(json_str)\n```\n\n**Output:**\n```json\n{\n  \"array\": [1, 2, 3, 4, 5],\n  \"matrix\": [[1, 2], [3, 4]],\n  \"scalar\": 3.14159,\n  \"date\": \"2025-01-15\"\n}\n```\n\n## Usage\n\n### Basic Usage\n\nSimply pass `NumpyJSONEncoder` as the `cls` parameter to `json.dumps()`:\n\n```python\nimport json\nfrom numpy_json import NumpyJSONEncoder\n\njson_str = json.dumps(your_data, cls=NumpyJSONEncoder)\n```\n\n### Configuration Options\n\nThe encoder provides class-level configuration:\n\n#### Decimal Handling\n\n```python\nfrom numpy_json import NumpyJSONEncoder\nimport decimal\n\n# Default: convert Decimal to float (lossy for very precise decimals)\ndata = {\"price\": decimal.Decimal(\"19.99\")}\njson.dumps(data, cls=NumpyJSONEncoder)  # \u2192 {\"price\": 19.99}\n\n# Alternative: preserve as string\nNumpyJSONEncoder.DECIMAL_AS_STR = True\njson.dumps(data, cls=NumpyJSONEncoder)  # \u2192 {\"price\": \"19.99\"}\n```\n\n#### Binary Data Handling\n\n```python\nfrom numpy_json import NumpyJSONEncoder\n\ndata = {\"binary\": b\"hello world\"}\n\n# Default: base64 encoding\njson.dumps(data, cls=NumpyJSONEncoder)  \n# \u2192 {\"binary\": \"aGVsbG8gd29ybGQ=\"}\n\n# Alternative: emit as integer list\nNumpyJSONEncoder.BASE64_BYTES = False\njson.dumps(data, cls=NumpyJSONEncoder)\n# \u2192 {\"binary\": [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]}\n```\n\n### Handling NaN and Infinity\n\nBy default, Python's `json` module allows `NaN` and `Infinity` values (which are not valid in RFC 8259 JSON). If you need strict JSON compliance:\n\n```python\nimport json\nimport numpy as np\nfrom numpy_json import NumpyJSONEncoder, sanitize_nans\n\ndata = {\n    \"valid\": np.array([1.0, 2.0, 3.0]),\n    \"invalid\": np.array([1.0, np.nan, np.inf, -np.inf]),\n}\n\n# Clean the data first\nclean_data = sanitize_nans(data)\n# NaN/Inf values are replaced with None\n\n# Then encode with allow_nan=False for strict JSON\njson_str = json.dumps(clean_data, cls=NumpyJSONEncoder, allow_nan=False)\n```\n\n**Result:**\n```json\n{\n  \"valid\": [1.0, 2.0, 3.0],\n  \"invalid\": [1.0, null, null, null]\n}\n```\n\n### Advanced Example\n\n```python\nimport json\nimport numpy as np\nfrom datetime import datetime\nfrom uuid import uuid4\nfrom pathlib import Path\nfrom numpy_json import NumpyJSONEncoder\n\ncomplex_data = {\n    \"id\": uuid4(),\n    \"timestamp\": datetime.now(),\n    \"measurements\": np.array([1.5, 2.3, 3.7, 4.1]),\n    \"matrix\": np.random.rand(3, 3),\n    \"metadata\": {\n        \"path\": Path(\"/tmp/data.csv\"),\n        \"tags\": {\"ml\", \"experiment\", \"2025\"},\n        \"coordinates\": (40.7128, -74.0060),\n    },\n    \"config\": {\n        \"enabled\": np.bool_(True),\n        \"threshold\": np.float32(0.85),\n        \"max_items\": np.int64(1000),\n    }\n}\n\njson_str = json.dumps(complex_data, cls=NumpyJSONEncoder, indent=2)\nprint(json_str)\n```\n\n## API Reference\n\n### `NumpyJSONEncoder`\n\n**Class Attributes:**\n- `DECIMAL_AS_STR` (bool): If `True`, encode `Decimal` as string. Default: `False`\n- `BASE64_BYTES` (bool): If `True`, encode bytes as base64. Default: `True`\n\n**Methods:**\n- `default(obj)`: Override method that handles type conversion\n\n### `sanitize_nans(obj)`\n\nRecursively replace `NaN`, `Inf`, and `-Inf` values with `None`.\n\n**Parameters:**\n- `obj`: Any Python object (dict, list, NumPy array, etc.)\n\n**Returns:**\n- Object with all NaN/Inf values replaced with `None`\n\n## Type Conversion Table\n\n| Python/NumPy Type | JSON Output | Notes |\n|-------------------|-------------|-------|\n| `np.ndarray` | Array | Recursive conversion via `.tolist()` |\n| `np.int*` | Number | All NumPy integer types |\n| `np.float*` | Number | All NumPy float types |\n| `np.bool_` | Boolean | NumPy boolean |\n| `np.datetime64` | String | ISO 8601 format |\n| `np.timedelta64` | String | Duration string |\n| `datetime`/`date`/`time` | String | ISO 8601 format |\n| `uuid.UUID` | String | Standard UUID string |\n| `decimal.Decimal` | Number or String | Configurable via `DECIMAL_AS_STR` |\n| `set`/`tuple` | Array | Converted to list |\n| `bytes`/`bytearray` | String or Array | Configurable via `BASE64_BYTES` |\n| `pathlib.Path` | String | String representation |\n| pandas types | String or `null` | Optional support, no hard dependency |\n\n## Requirements\n\n- Python 3.8+\n- NumPy\n\n## Development\n\n### Setup\n\n```bash\ngit clone https://github.com/featrix/numpy-json.git\ncd numpy-json\npip install -e \".[dev]\"\n```\n\n### Running Tests\n\n```bash\npytest tests/\n```\n\n### Code Style\n\nThis project uses:\n- `black` for code formatting\n- `flake8` for linting\n- `mypy` for type checking\n\n```bash\nblack numpy_json/\nflake8 numpy_json/\nmypy numpy_json/\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Copyright\n\nCopyright (c) 2025 Featrix, Inc.\n\n## Acknowledgments\n\n- Built to solve real-world data serialization challenges in scientific computing and machine learning workflows\n- Inspired by the need for seamless NumPy integration with JSON APIs\n\n## Support\n\nFor issues, questions, or contributions, please visit the [GitHub repository](https://github.com/featrix/numpy-json).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "JSON encoder for NumPy arrays and extended Python data types",
    "version": "0.1.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/featrix/numpy-json/issues",
        "Documentation": "https://github.com/featrix/numpy-json#readme",
        "Homepage": "https://github.com/featrix/numpy-json",
        "Repository": "https://github.com/featrix/numpy-json"
    },
    "split_keywords": [
        "numpy",
        " json",
        " encoder",
        " serialization",
        " array",
        " scientific computing",
        " data science"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1d50657ee74d759fd7b050542f4c4c7252304c6fbb07a732991108b5b0a928d8",
                "md5": "f858b9e5d2998e7d63657f09845eb443",
                "sha256": "598aba0886791eee94ffe125684a09b8ba3e3079e282959100a609089444a31b"
            },
            "downloads": -1,
            "filename": "numpy_json-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f858b9e5d2998e7d63657f09845eb443",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6982,
            "upload_time": "2025-10-08T02:15:14",
            "upload_time_iso_8601": "2025-10-08T02:15:14.659697Z",
            "url": "https://files.pythonhosted.org/packages/1d/50/657ee74d759fd7b050542f4c4c7252304c6fbb07a732991108b5b0a928d8/numpy_json-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "bad77103a6e64dce6a37ef04c0439b7c10ed843c526bfb108b7be0c0d34868bd",
                "md5": "b7601811277ebbf6f58df020d3a2be13",
                "sha256": "59f1e5d9247d4796af12fcf7c8e18c2a7bdcc084567d402aa6f71674b0c4d86f"
            },
            "downloads": -1,
            "filename": "numpy_json-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b7601811277ebbf6f58df020d3a2be13",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 7898,
            "upload_time": "2025-10-08T02:15:15",
            "upload_time_iso_8601": "2025-10-08T02:15:15.954534Z",
            "url": "https://files.pythonhosted.org/packages/ba/d7/7103a6e64dce6a37ef04c0439b7c10ed843c526bfb108b7be0c0d34868bd/numpy_json-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-08 02:15:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "featrix",
    "github_project": "numpy-json",
    "github_not_found": true,
    "lcname": "numpy-json"
}
        
Elapsed time: 1.01340s