json_numpy_encoder#

class json_numpy_encoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: JSONEncoder

JSON encoder that handles NumPy data types.

This class extends the standard JSON encoder to properly serialize NumPy integers, floats, and arrays to their Python equivalents.

Examples

>>> import json
>>> import numpy as np
>>> from RuleTree.utils.data_utils import json_numpy_encoder
>>> json.dumps({"a": np.arange(3), "b": np.float32(0.5)}, cls=json_numpy_encoder)
'{"a": [0, 1, 2], "b": 0.5}'
default(obj)[source]#

Convert NumPy types to standard Python types for JSON serialization.

Parameters:

obj (object) – Object to be serialized

Returns:

Python equivalent of NumPy type

Return type:

object

Raises:

TypeError – If object is not a NumPy type and cannot be serialized by the parent class