@@ -44,7 +44,7 @@ Call `to_json` to serialize `Foo` object into JSON string and `from_json` to des
44
44
>> > from serde.json import to_json, from_json
45
45
46
46
>> > to_json(Foo(i = 10 , s = ' foo' , f = 100.0 , b = True ))
47
- {" i" : 10 , " s" : " foo" , " f" : 100.0 , " b" : true}
47
+ ' {"i": 10, "s": "foo", "f": 100.0, "b": true}'
48
48
49
49
>> > from_json(Foo, ' {"i": 10, "s": "foo", "f": 100.0, "b": true}' )
50
50
Foo(i = 10 , s = ' foo' , f = 100.0 , b = True )
@@ -98,3 +98,75 @@ b'\x84\xa1i\n\xa1s\xa3foo\xa1f\xcb@Y\x00\x00\x00\x00\x00\x00\xa1b\xc3'
98
98
>> > from_msgpack(Foo, b ' \x84\xa1 i\n\xa1 s\xa3 foo\xa1 f\xcb @Y\x00\x00\x00\x00\x00\x00\xa1 b\xc3 ' )
99
99
Foo(i = 10 , s = ' foo' , f = 100.0 , b = True )
100
100
```
101
+
102
+ ## Numpy
103
+
104
+ All of the above (de)serialization methods can transparently handle most numpy
105
+ types with the "numpy" extras package.
106
+
107
+ ``` python
108
+ import numpy as np
109
+ import numpy.typing as npt
110
+
111
+ @serde
112
+ class NPFoo :
113
+ i: np.int32
114
+ j: np.int64
115
+ f: np.float32
116
+ g: np.float64
117
+ h: np.bool_
118
+ u: np.ndarray
119
+ v: npt.NDArray
120
+ w: npt.NDArray[np.int32]
121
+ x: npt.NDArray[np.int64]
122
+ y: npt.NDArray[np.float32]
123
+ z: npt.NDArray[np.float64]
124
+
125
+ npfoo = NPFoo(
126
+ np.int32(1 ),
127
+ np.int64(2 ),
128
+ np.float32(3.0 ),
129
+ np.float64(4.0 ),
130
+ np.bool_(False ),
131
+ np.array([1 , 2 ]),
132
+ np.array([3 , 4 ]),
133
+ np.array([np.int32(i) for i in [1 , 2 ]]),
134
+ np.array([np.int64(i) for i in [3 , 4 ]]),
135
+ np.array([np.float32(i) for i in [5.0 , 6.0 ]]),
136
+ np.array([np.float64(i) for i in [7.0 , 8.0 ]]),
137
+ )
138
+ ```
139
+
140
+ ``` python
141
+ >> > from serde.json import to_json, from_json
142
+
143
+ >> > to_json(npfoo)
144
+ ' {"i": 1, "j": 2, "f": 3.0, "g": 4.0, "h": false, "u": [1, 2], "v": [3, 4], "w": [1, 2], "x": [3, 4], "y": [5.0, 6.0], "z": [7.0, 8.0]}'
145
+
146
+ >> > from_json(NPFoo, to_json(npfoo))
147
+ NPFoo(i = 1 , j = 2 , f = 3.0 , g = 4.0 , h = False , u = array([1 , 2 ]), v = array([3 , 4 ]), w = array([1 , 2 ], dtype = int32), x = array([3 , 4 ]), y = array([5 ., 6 .], dtype = float32), z = array([7 ., 8 .]))
148
+
149
+ >> > from serde.yaml import from_yaml, to_yaml
150
+
151
+ >> > to_yaml(npfoo)
152
+ ' f: 3.0\n g: 4.0\n h: false\n i: 1\n j: 2\n u:\n - 1\n - 2\n v:\n - 3\n - 4\n w:\n - 1\n - 2\n x:\n - 3\n - 4\n y:\n - 5.0\n - 6.0\n z:\n - 7.0\n - 8.0\n '
153
+
154
+ >> > from_yaml(NPFoo, to_yaml(npfoo))
155
+ NPFoo(i = 1 , j = 2 , f = 3.0 , g = 4.0 , h = False , u = array([1 , 2 ]), v = array([3 , 4 ]), w = array([1 , 2 ], dtype = int32), x = array([3 , 4 ]), y = array([5 ., 6 .], dtype = float32), z = array([7 ., 8 .]))
156
+
157
+ >> > from serde.toml import from_toml, to_toml
158
+
159
+ >> > to_toml(npfoo)
160
+ ' i = 1\n j = 2\n f = 3.0\n g = 4.0\n h = false\n u = [ 1, 2,]\n v = [ 3, 4,]\n w = [ 1, 2,]\n x = [ 3, 4,]\n y = [ 5.0, 6.0,]\n z = [ 7.0, 8.0,]\n '
161
+
162
+ >> > from_toml(NPFoo, to_toml(npfoo))
163
+ NPFoo(i = 1 , j = 2 , f = 3.0 , g = 4.0 , h = False , u = array([1 , 2 ]), v = array([3 , 4 ]), w = array([1 , 2 ], dtype = int32), x = array([3 , 4 ]), y = array([5 ., 6 .], dtype = float32), z = array([7 ., 8 .]))
164
+
165
+ >> > from serde.msgpack import from_msgpack, to_msgpack
166
+
167
+ >> > to_msgpack(npfoo)
168
+ b ' \x8b\xa1 i\x01\xa1 j\x02\xa1 f\xcb @\x08\x00\x00\x00\x00\x00\x00\xa1 g\xcb @\x10\x00\x00\x00\x00\x00\x00\xa1 h\xc2\xa1 u\x92\x01\x02\xa1 v\x92\x03\x04\xa1 w\x92\x01\x02\xa1 x\x92\x03\x04\xa1 y\x92\xcb @\x14\x00\x00\x00\x00\x00\x00\xcb @\x18\x00\x00\x00\x00\x00\x00\xa1 z\x92\xcb @\x1c\x00\x00\x00\x00\x00\x00\xcb @ \x00\x00\x00\x00\x00\x00 '
169
+
170
+ >> > from_msgpack(NPFoo, to_msgpack(npfoo))
171
+ NPFoo(i = 1 , j = 2 , f = 3.0 , g = 4.0 , h = False , u = array([1 , 2 ]), v = array([3 , 4 ]), w = array([1 , 2 ], dtype = int32), x = array([3 , 4 ]), y = array([5 ., 6 .], dtype = float32), z = array([7 ., 8 .]))
172
+ ```
0 commit comments