You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* msgspec first pass
* Fix typing import
* Test carefully for PyPy
* Docs
* Fix typing wrapper
* Fix PyPy CI some more
* Remove unused paramspec
* Use msgspec's datetime structurer
* More msgspec
* Ignore _cpython tests on PyPy
* More msgspec
* More doc work
* Fix
* Docs
* Fix test
* More msgspec work
* Pass through mapping to msgspec
* Fix counters
- The `include_subclasses` strategy now fetches the member hooks from the converter (making use of converter defaults) if overrides are not provided, instead of generating new hooks with no overrides.
- The {class}`orjson preconf converter <cattrs.preconf.orjson.OrjsonConverter>` now passes through dates and datetimes to orjson while unstructuring, greatly improving speed.
Copy file name to clipboardExpand all lines: docs/customizing.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
This section describes customizing the unstructuring and structuring processes in _cattrs_.
4
4
5
-
## Manual Un/structuring Hooks
5
+
## Custom Un/structuring Hooks
6
6
7
7
You can write your own structuring and unstructuring functions and register them for types using {meth}`Converter.register_structure_hook() <cattrs.BaseConverter.register_structure_hook>` and {meth}`Converter.register_unstructure_hook() <cattrs.BaseConverter.register_unstructure_hook>`.
8
8
This approach is the most flexible but also requires the most amount of boilerplate.
Copy file name to clipboardExpand all lines: docs/preconf.md
+61-13
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,9 @@ Optional install targets should match the name of the {mod}`cattrs.preconf` modu
44
44
# Using pip
45
45
$ pip install cattrs[ujson]
46
46
47
+
# Using pdm
48
+
$ pdm add cattrs[orjson]
49
+
47
50
# Using poetry
48
51
$ poetry add --extras tomlkit cattrs
49
52
```
@@ -56,15 +59,6 @@ Found at {mod}`cattrs.preconf.json`.
56
59
Bytes are serialized as base 85 strings. Counters are serialized as dictionaries. Sets are serialized as lists, and deserialized back into sets. `datetime` s and `date` s are serialized as ISO 8601 strings.
57
60
58
61
59
-
## _ujson_
60
-
61
-
Found at {mod}`cattrs.preconf.ujson`.
62
-
63
-
Bytes are serialized as base 85 strings. Sets are serialized as lists, and deserialized back into sets. `datetime` s and `date` s are serialized as ISO 8601 strings.
64
-
65
-
`ujson` doesn't support integers less than -9223372036854775808, and greater than 9223372036854775807, nor does it support `float('inf')`.
66
-
67
-
68
62
## _orjson_
69
63
70
64
Found at {mod}`cattrs.preconf.orjson`.
@@ -77,6 +71,61 @@ _orjson_ doesn't support integers less than -9223372036854775808, and greater th
77
71
_orjson_ only supports mappings with string keys so mappings will have their keys stringified before serialization, and destringified during deserialization.
78
72
79
73
74
+
## _msgspec_
75
+
76
+
Found at {mod}`cattrs.preconf.msgspec`.
77
+
Only JSON functionality is currently available, other formats supported by msgspec to follow in the future.
78
+
79
+
[_msgspec_ structs](https://jcristharif.com/msgspec/structs.html) are supported, but not composable - a struct will be handed over to _msgspec_ directly, and _msgspec_ will handle and all of its fields, recursively.
80
+
_cattrs_ may get more sophisticated handling of structs in the future.
81
+
82
+
[_msgspec_ strict mode](https://jcristharif.com/msgspec/usage.html#strict-vs-lax-mode) is used by default.
83
+
This can be customized by changing the {meth}`encoder <cattrs.preconf.msgspec.MsgspecJsonConverter.encoder>` attribute on the converter.
84
+
85
+
What _cattrs_ calls _unstructuring_ and _structuring_, _msgspec_ calls [`to_builtins` and `convert`](https://jcristharif.com/msgspec/converters.html).
86
+
What _cattrs_ refers to as _dumping_ and _loading_, _msgspec_ refers to as [`encoding` and `decoding`](https://jcristharif.com/msgspec/usage.html).
87
+
88
+
Compatibility notes:
89
+
- Bytes are un/structured as base 64 strings directly by _msgspec_ itself.
90
+
-_msgspec_[encodes special float values](https://jcristharif.com/msgspec/supported-types.html#float) (`NaN, Inf, -Inf`) as `null`.
91
+
-`datetime` s and `date` s are passed through to be unstructured into RFC 3339 by _msgspec_ itself.
92
+
-_attrs_ classes, dataclasses and sequences are handled directly by _msgspec_ if possible, otherwise by the normal _cattrs_ machinery.
93
+
This means it's possible the validation errors produced may be _msgspec_ validation errors instead of _cattrs_ validation errors.
94
+
95
+
This converter supports {meth}`get_loads_hook() <cattrs.preconf.msgspec.MsgspecJsonConverter.get_loads_hook>` and {meth}`get_dumps_hook() <cattrs.preconf.msgspec.MsgspecJsonConverter.get_loads_hook>`.
96
+
These are factories for dumping and loading functions (as opposed to unstructuring and structuring); the hooks returned by this may be further optimized to offload as much work as possible to _msgspec_.
Due to its complexity, this converter is currently _provisional_ and may slightly change as the best integration patterns are discovered.
113
+
114
+
_msgspec_ doesn't support PyPy.
115
+
116
+
```{versionadded} 24.1.0
117
+
118
+
```
119
+
120
+
## _ujson_
121
+
122
+
Found at {mod}`cattrs.preconf.ujson`.
123
+
124
+
Bytes are serialized as base 85 strings. Sets are serialized as lists, and deserialized back into sets. `datetime` s and `date` s are serialized as ISO 8601 strings.
125
+
126
+
_ujson_ doesn't support integers less than -9223372036854775808, and greater than 9223372036854775807, nor does it support `float('inf')`.
127
+
128
+
80
129
## _msgpack_
81
130
82
131
Found at {mod}`cattrs.preconf.msgpack`.
@@ -90,10 +139,6 @@ When parsing msgpack data from bytes, the library needs to be passed `strict_map
90
139
91
140
## _cbor2_
92
141
93
-
```{versionadded} 23.1.0
94
-
95
-
```
96
-
97
142
Found at {mod}`cattrs.preconf.cbor2`.
98
143
99
144
_cbor2_ implements a fully featured CBOR encoder with several extensions for handling shared references, big integers, rational numbers and so on.
@@ -112,6 +157,9 @@ Use keyword argument `canonical=True` for efficient encoding to the smallest bin
112
157
Floats can be forced to smaller output by casting to lower-precision formats by casting to `numpy` floats (and back to Python floats).
113
158
Example: `float(np.float32(value))` or `float(np.float16(value))`
0 commit comments