|
2 | 2 |
|
3 | 3 | Re-exports common JSON encoding and decoding functions from the core |
4 | 4 | serialization module for convenient access. |
| 5 | +
|
| 6 | +Provides NumPy array serialization hooks for framework integrations |
| 7 | +that support custom type encoders and decoders (e.g., Litestar). |
5 | 8 | """ |
6 | 9 |
|
7 | 10 | from typing import Any, Literal, overload |
8 | 11 |
|
9 | 12 | from sqlspec._serialization import decode_json, encode_json |
| 13 | +from sqlspec.typing import NUMPY_INSTALLED |
10 | 14 |
|
11 | 15 |
|
12 | 16 | @overload |
@@ -55,4 +59,109 @@ def from_json(data: str | bytes, *, decode_bytes: bool = True) -> Any: |
55 | 59 | return decode_json(data) |
56 | 60 |
|
57 | 61 |
|
58 | | -__all__ = ("from_json", "to_json") |
| 62 | +def numpy_array_enc_hook(value: Any) -> Any: |
| 63 | + """Encode NumPy array to JSON-compatible list. |
| 64 | +
|
| 65 | + Converts NumPy ndarrays to Python lists for JSON serialization. |
| 66 | + Gracefully handles cases where NumPy is not installed by returning |
| 67 | + the original value unchanged. |
| 68 | +
|
| 69 | + Args: |
| 70 | + value: Value to encode (checked for ndarray type). |
| 71 | +
|
| 72 | + Returns: |
| 73 | + List representation if value is ndarray, original value otherwise. |
| 74 | +
|
| 75 | + Example: |
| 76 | + >>> import numpy as np |
| 77 | + >>> arr = np.array([1.0, 2.0, 3.0]) |
| 78 | + >>> numpy_array_enc_hook(arr) |
| 79 | + [1.0, 2.0, 3.0] |
| 80 | +
|
| 81 | + >>> # Multi-dimensional arrays work automatically |
| 82 | + >>> arr_2d = np.array([[1, 2], [3, 4]]) |
| 83 | + >>> numpy_array_enc_hook(arr_2d) |
| 84 | + [[1, 2], [3, 4]] |
| 85 | + """ |
| 86 | + if not NUMPY_INSTALLED: |
| 87 | + return value |
| 88 | + |
| 89 | + import numpy as np |
| 90 | + |
| 91 | + if isinstance(value, np.ndarray): |
| 92 | + return value.tolist() |
| 93 | + return value |
| 94 | + |
| 95 | + |
| 96 | +def numpy_array_dec_hook(value: Any) -> "Any": |
| 97 | + """Decode list to NumPy array. |
| 98 | +
|
| 99 | + Converts Python lists to NumPy arrays when appropriate. |
| 100 | + Works best with typed schemas (Pydantic, msgspec) that expect ndarray. |
| 101 | +
|
| 102 | + Args: |
| 103 | + value: List to potentially convert to ndarray. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + NumPy array if conversion successful, original value otherwise. |
| 107 | +
|
| 108 | + Note: |
| 109 | + Dtype is inferred by NumPy and may differ from original array. |
| 110 | + For explicit dtype control, construct arrays manually in application code. |
| 111 | +
|
| 112 | + Example: |
| 113 | + >>> numpy_array_dec_hook([1.0, 2.0, 3.0]) |
| 114 | + array([1., 2., 3.]) |
| 115 | +
|
| 116 | + >>> # Returns original value if NumPy not installed |
| 117 | + >>> # (when NUMPY_INSTALLED is False) |
| 118 | + >>> numpy_array_dec_hook([1, 2, 3]) |
| 119 | + [1, 2, 3] |
| 120 | + """ |
| 121 | + if not NUMPY_INSTALLED: |
| 122 | + return value |
| 123 | + |
| 124 | + import numpy as np |
| 125 | + |
| 126 | + if isinstance(value, list): |
| 127 | + try: |
| 128 | + return np.array(value) |
| 129 | + except Exception: |
| 130 | + return value |
| 131 | + return value |
| 132 | + |
| 133 | + |
| 134 | +def numpy_array_predicate(value: Any) -> bool: |
| 135 | + """Check if value is NumPy array instance. |
| 136 | +
|
| 137 | + Type checker for decoder registration in framework plugins. |
| 138 | + Returns False when NumPy is not installed. |
| 139 | +
|
| 140 | + Args: |
| 141 | + value: Value to type-check. |
| 142 | +
|
| 143 | + Returns: |
| 144 | + True if value is ndarray, False otherwise. |
| 145 | +
|
| 146 | + Example: |
| 147 | + >>> import numpy as np |
| 148 | + >>> numpy_array_predicate(np.array([1, 2, 3])) |
| 149 | + True |
| 150 | +
|
| 151 | + >>> numpy_array_predicate([1, 2, 3]) |
| 152 | + False |
| 153 | +
|
| 154 | + >>> # Returns False when NumPy not installed |
| 155 | + >>> # (when NUMPY_INSTALLED is False) |
| 156 | + >>> numpy_array_predicate([1, 2, 3]) |
| 157 | + False |
| 158 | + """ |
| 159 | + if not NUMPY_INSTALLED: |
| 160 | + return False |
| 161 | + |
| 162 | + import numpy as np |
| 163 | + |
| 164 | + return isinstance(value, np.ndarray) |
| 165 | + |
| 166 | + |
| 167 | +__all__ = ("from_json", "numpy_array_dec_hook", "numpy_array_enc_hook", "numpy_array_predicate", "to_json") |
0 commit comments