poulet_py.tools.serializers module#
- json_serializer(data, file=None)[source]#
Serialize data to JSON.
Features: - Efficient serialization using orjson (faster than standard json module) - Built-in numpy array support via orjson’s native serialization - Returns bytes if no file provided, writes to file otherwise
- Return type:
bytes|None
- Args:
- data: Dictionary containing data to serialize.
Numpy arrays are automatically supported.
- file: Output file
If None, returns serialized bytes instead of writing to file. Must end with ‘.json’ extension if provided.
- Returns:
bytes | None: Serialized JSON as bytes if file is None, otherwise None.
- Raises:
ValueError: If provided file doesn’t end with ‘.json’ extension TypeError: If data contains non-serializable types JSONEncodeError: If serialization fails for other reasons
- Example:
>>> data = {"array": np.array([1, 2, 3])} >>> # Write to file >>> json_serializer(data, "output.json") >>> # Get bytes >>> json_bytes = json_serializer(data)