@@ -21,7 +21,7 @@ use super::errors::{py_err_se_err, PydanticSerializationError};
21
21
use super :: extra:: { Extra , SerMode } ;
22
22
use super :: filter:: AnyFilter ;
23
23
use super :: ob_type:: ObType ;
24
- use super :: shared:: object_to_dict ;
24
+ use super :: shared:: dataclass_to_dict ;
25
25
26
26
pub ( crate ) fn infer_to_python (
27
27
value : & PyAny ,
@@ -97,29 +97,23 @@ pub(crate) fn infer_to_python_known(
97
97
Ok :: < PyObject , PyErr > ( new_dict. into_py ( py) )
98
98
} ;
99
99
100
- let serialize_with_serializer = |value : & PyAny , is_model : bool | {
101
- if let Ok ( py_serializer) = value. getattr ( intern ! ( py, "__pydantic_serializer__" ) ) {
102
- if let Ok ( serializer) = py_serializer. extract :: < SchemaSerializer > ( ) {
103
- let extra = serializer. build_extra (
104
- py,
105
- extra. mode ,
106
- extra. by_alias ,
107
- extra. warnings ,
108
- extra. exclude_unset ,
109
- extra. exclude_defaults ,
110
- extra. exclude_none ,
111
- extra. round_trip ,
112
- extra. rec_guard ,
113
- extra. serialize_unknown ,
114
- extra. fallback ,
115
- ) ;
116
- return serializer. serializer . to_python ( value, include, exclude, & extra) ;
117
- }
118
- }
119
- // Fallback to dict serialization if `__pydantic_serializer__` is not set.
120
- // This currently only affects non-pydantic dataclasses.
121
- let dict = object_to_dict ( value, is_model, extra) ?;
122
- serialize_dict ( dict)
100
+ let serialize_with_serializer = || {
101
+ let py_serializer = value. getattr ( intern ! ( py, "__pydantic_serializer__" ) ) ?;
102
+ let serializer: SchemaSerializer = py_serializer. extract ( ) ?;
103
+ let extra = serializer. build_extra (
104
+ py,
105
+ extra. mode ,
106
+ extra. by_alias ,
107
+ extra. warnings ,
108
+ extra. exclude_unset ,
109
+ extra. exclude_defaults ,
110
+ extra. exclude_none ,
111
+ extra. round_trip ,
112
+ extra. rec_guard ,
113
+ extra. serialize_unknown ,
114
+ extra. fallback ,
115
+ ) ;
116
+ serializer. serializer . to_python ( value, include, exclude, & extra)
123
117
} ;
124
118
125
119
let value = match extra. mode {
@@ -191,8 +185,8 @@ pub(crate) fn infer_to_python_known(
191
185
let py_url: PyMultiHostUrl = value. extract ( ) ?;
192
186
py_url. __str__ ( ) . into_py ( py)
193
187
}
194
- ObType :: PydanticSerializable => serialize_with_serializer ( value , true ) ?,
195
- ObType :: Dataclass => serialize_with_serializer ( value, false ) ?,
188
+ ObType :: PydanticSerializable => serialize_with_serializer ( ) ?,
189
+ ObType :: Dataclass => serialize_dict ( dataclass_to_dict ( value) ? ) ?,
196
190
ObType :: Enum => {
197
191
let v = value. getattr ( intern ! ( py, "value" ) ) ?;
198
192
infer_to_python ( v, include, exclude, extra) ?. into_py ( py)
@@ -257,8 +251,8 @@ pub(crate) fn infer_to_python_known(
257
251
}
258
252
new_dict. into_py ( py)
259
253
}
260
- ObType :: PydanticSerializable => serialize_with_serializer ( value , true ) ?,
261
- ObType :: Dataclass => serialize_with_serializer ( value, false ) ?,
254
+ ObType :: PydanticSerializable => serialize_with_serializer ( ) ?,
255
+ ObType :: Dataclass => serialize_dict ( dataclass_to_dict ( value) ? ) ?,
262
256
ObType :: Generator => {
263
257
let iter = super :: type_serializers:: generator:: SerializationIterator :: new (
264
258
value. downcast ( ) ?,
@@ -406,36 +400,6 @@ pub(crate) fn infer_serialize_known<S: Serializer>(
406
400
} } ;
407
401
}
408
402
409
- macro_rules! serialize_with_serializer {
410
- ( $py_serializable: expr, $is_model: expr) => { {
411
- let py = $py_serializable. py( ) ;
412
- if let Ok ( py_serializer) = value. getattr( intern!( py, "__pydantic_serializer__" ) ) {
413
- if let Ok ( extracted_serializer) = py_serializer. extract:: <SchemaSerializer >( ) {
414
- let extra = extracted_serializer. build_extra(
415
- py,
416
- extra. mode,
417
- extra. by_alias,
418
- extra. warnings,
419
- extra. exclude_unset,
420
- extra. exclude_defaults,
421
- extra. exclude_none,
422
- extra. round_trip,
423
- extra. rec_guard,
424
- extra. serialize_unknown,
425
- extra. fallback,
426
- ) ;
427
- let pydantic_serializer =
428
- PydanticSerializer :: new( value, & extracted_serializer. serializer, include, exclude, & extra) ;
429
- return pydantic_serializer. serialize( serializer) ;
430
- }
431
- }
432
- // Fallback to dict serialization if `__pydantic_serializer__` is not set.
433
- // This currently only affects non-pydantic dataclasses.
434
- let dict = object_to_dict( value, $is_model, extra) . map_err( py_err_se_err) ?;
435
- serialize_dict!( dict)
436
- } } ;
437
- }
438
-
439
403
let ser_result = match ob_type {
440
404
ObType :: None => serializer. serialize_none ( ) ,
441
405
ObType :: Int | ObType :: IntSubclass => serialize ! ( i64 ) ,
@@ -490,8 +454,30 @@ pub(crate) fn infer_serialize_known<S: Serializer>(
490
454
let py_url: PyMultiHostUrl = value. extract ( ) . map_err ( py_err_se_err) ?;
491
455
serializer. serialize_str ( & py_url. __str__ ( ) )
492
456
}
493
- ObType :: Dataclass => serialize_with_serializer ! ( value, false ) ,
494
- ObType :: PydanticSerializable => serialize_with_serializer ! ( value, true ) ,
457
+ ObType :: PydanticSerializable => {
458
+ let py = value. py ( ) ;
459
+ let py_serializer = value
460
+ . getattr ( intern ! ( py, "__pydantic_serializer__" ) )
461
+ . map_err ( py_err_se_err) ?;
462
+ let extracted_serializer: SchemaSerializer = py_serializer. extract ( ) . map_err ( py_err_se_err) ?;
463
+ let extra = extracted_serializer. build_extra (
464
+ py,
465
+ extra. mode ,
466
+ extra. by_alias ,
467
+ extra. warnings ,
468
+ extra. exclude_unset ,
469
+ extra. exclude_defaults ,
470
+ extra. exclude_none ,
471
+ extra. round_trip ,
472
+ extra. rec_guard ,
473
+ extra. serialize_unknown ,
474
+ extra. fallback ,
475
+ ) ;
476
+ let pydantic_serializer =
477
+ PydanticSerializer :: new ( value, & extracted_serializer. serializer , include, exclude, & extra) ;
478
+ pydantic_serializer. serialize ( serializer)
479
+ }
480
+ ObType :: Dataclass => serialize_dict ! ( dataclass_to_dict( value) . map_err( py_err_se_err) ?) ,
495
481
ObType :: Enum => {
496
482
let v = value. getattr ( intern ! ( value. py( ) , "value" ) ) . map_err ( py_err_se_err) ?;
497
483
infer_serialize ( v, serializer, include, exclude, extra)
0 commit comments