@@ -94,6 +94,7 @@ def jsonable_encoder( # noqa: PLR0911
94
94
exclude_unset = exclude_unset ,
95
95
exclude_defaults = exclude_defaults ,
96
96
exclude_none = exclude_none ,
97
+ custom_serializer = custom_serializer ,
97
98
)
98
99
99
100
# Enums
@@ -115,8 +116,9 @@ def jsonable_encoder( # noqa: PLR0911
115
116
include = include ,
116
117
exclude = exclude ,
117
118
by_alias = by_alias ,
118
- exclude_none = exclude_none ,
119
119
exclude_unset = exclude_unset ,
120
+ exclude_none = exclude_none ,
121
+ custom_serializer = custom_serializer ,
120
122
)
121
123
122
124
# Sequences
@@ -129,6 +131,7 @@ def jsonable_encoder( # noqa: PLR0911
129
131
exclude_none = exclude_none ,
130
132
exclude_defaults = exclude_defaults ,
131
133
exclude_unset = exclude_unset ,
134
+ custom_serializer = custom_serializer ,
132
135
)
133
136
134
137
# Other types
@@ -152,6 +155,7 @@ def jsonable_encoder( # noqa: PLR0911
152
155
exclude_none = exclude_none ,
153
156
exclude_unset = exclude_unset ,
154
157
exclude_defaults = exclude_defaults ,
158
+ custom_serializer = custom_serializer ,
155
159
)
156
160
except ValueError as exc :
157
161
raise SerializationError (
@@ -201,9 +205,15 @@ def _dump_dict(
201
205
by_alias : bool = True ,
202
206
exclude_unset : bool = False ,
203
207
exclude_none : bool = False ,
208
+ custom_serializer : Optional [Callable [[Any ], str ]] = None ,
204
209
) -> Dict [str , Any ]:
205
210
"""
206
211
Dump a dict to a dict, using the same parameters as jsonable_encoder
212
+
213
+ Parameters
214
+ ----------
215
+ custom_serializer : Callable, optional
216
+ A custom serializer to use for encoding the object, when everything else fails.
207
217
"""
208
218
encoded_dict = {}
209
219
allowed_keys = set (obj .keys ())
@@ -222,12 +232,14 @@ def _dump_dict(
222
232
by_alias = by_alias ,
223
233
exclude_unset = exclude_unset ,
224
234
exclude_none = exclude_none ,
235
+ custom_serializer = custom_serializer ,
225
236
)
226
237
encoded_value = jsonable_encoder (
227
238
value ,
228
239
by_alias = by_alias ,
229
240
exclude_unset = exclude_unset ,
230
241
exclude_none = exclude_none ,
242
+ custom_serializer = custom_serializer ,
231
243
)
232
244
encoded_dict [encoded_key ] = encoded_value
233
245
return encoded_dict
@@ -242,9 +254,10 @@ def _dump_sequence(
242
254
exclude_unset : bool = False ,
243
255
exclude_none : bool = False ,
244
256
exclude_defaults : bool = False ,
257
+ custom_serializer : Optional [Callable [[Any ], str ]] = None ,
245
258
) -> List [Any ]:
246
259
"""
247
- Dump a sequence to a list, using the same parameters as jsonable_encoder
260
+ Dump a sequence to a list, using the same parameters as jsonable_encoder.
248
261
"""
249
262
encoded_list = []
250
263
for item in obj :
@@ -257,6 +270,7 @@ def _dump_sequence(
257
270
exclude_unset = exclude_unset ,
258
271
exclude_defaults = exclude_defaults ,
259
272
exclude_none = exclude_none ,
273
+ custom_serializer = custom_serializer ,
260
274
),
261
275
)
262
276
return encoded_list
@@ -271,6 +285,7 @@ def _dump_other(
271
285
exclude_unset : bool = False ,
272
286
exclude_none : bool = False ,
273
287
exclude_defaults : bool = False ,
288
+ custom_serializer : Optional [Callable [[Any ], str ]] = None ,
274
289
) -> Any :
275
290
"""
276
291
Dump an object to a hashable object, using the same parameters as jsonable_encoder
@@ -292,6 +307,7 @@ def _dump_other(
292
307
exclude_unset = exclude_unset ,
293
308
exclude_defaults = exclude_defaults ,
294
309
exclude_none = exclude_none ,
310
+ custom_serializer = custom_serializer ,
295
311
)
296
312
297
313
0 commit comments