@@ -75,24 +75,32 @@ impl SerField {
75
75
}
76
76
}
77
77
78
- fn exclude_default_or_if (
78
+
79
+ fn exclude_if (
79
80
exclude_if_callable : & Option < Py < PyAny > > ,
80
81
value : & Bound < ' _ , PyAny > ,
81
- extra : & Extra ,
82
82
serializer : & CombinedSerializer ,
83
83
) -> PyResult < bool > {
84
- let py = value. py ( ) ;
85
-
86
84
if let Some ( exclude_if_callable) = exclude_if_callable {
85
+ let py = value. py ( ) ;
87
86
let result = exclude_if_callable. call1 ( py, ( value, ) ) ?;
88
87
let exclude = result. extract :: < bool > ( py) ?;
89
88
if exclude {
90
89
return Ok ( true ) ;
91
90
}
92
91
}
92
+ Ok ( false )
93
+ }
94
+
95
+
96
+ fn exclude_default (
97
+ value : & Bound < ' _ , PyAny > ,
98
+ extra : & Extra ,
99
+ serializer : & CombinedSerializer ,
100
+ ) -> PyResult < bool > {
93
101
94
102
if extra. exclude_defaults {
95
- if let Some ( default) = serializer. get_default ( py ) ? {
103
+ if let Some ( default) = serializer. get_default ( value . py ( ) ) ? {
96
104
if value. eq ( default) ? {
97
105
return Ok ( true ) ;
98
106
}
@@ -195,16 +203,20 @@ impl GeneralFieldsSerializer {
195
203
if let Some ( ( next_include, next_exclude) ) = self . filter . key_filter ( & key, include, exclude) ? {
196
204
if let Some ( field) = op_field {
197
205
if let Some ( ref serializer) = field. serializer {
198
- if !exclude_default_or_if ( & field. exclude_if , & value, & field_extra, serializer) ? {
199
- let value = serializer. to_python (
200
- & value,
201
- next_include. as_ref ( ) ,
202
- next_exclude. as_ref ( ) ,
203
- & field_extra,
204
- ) ?;
205
- let output_key = field. get_key_py ( output_dict. py ( ) , & field_extra) ;
206
- output_dict. set_item ( output_key, value) ?;
206
+ if exclude_default ( & value, & field_extra, serializer) ? {
207
+ continue ;
208
+ }
209
+ if exclude_if ( & field. exclude_if , & value, serializer) ? {
210
+ continue ;
207
211
}
212
+ let value = serializer. to_python (
213
+ & value,
214
+ next_include. as_ref ( ) ,
215
+ next_exclude. as_ref ( ) ,
216
+ & field_extra,
217
+ ) ?;
218
+ let output_key = field. get_key_py ( output_dict. py ( ) , & field_extra) ;
219
+ output_dict. set_item ( output_key, value) ?;
208
220
}
209
221
210
222
if field. required {
@@ -282,19 +294,22 @@ impl GeneralFieldsSerializer {
282
294
if let Some ( ( next_include, next_exclude) ) = filter {
283
295
if let Some ( field) = self . fields . get ( key_str) {
284
296
if let Some ( ref serializer) = field. serializer {
285
- if !exclude_default_or_if ( & field. exclude_if , & value, & field_extra, serializer)
286
- . map_err ( py_err_se_err) ?
287
- {
288
- let s = PydanticSerializer :: new (
289
- & value,
290
- serializer,
291
- next_include. as_ref ( ) ,
292
- next_exclude. as_ref ( ) ,
293
- & field_extra,
294
- ) ;
295
- let output_key = field. get_key_json ( key_str, & field_extra) ;
296
- map. serialize_entry ( & output_key, & s) ?;
297
+ if exclude_default ( & value, & field_extra, serializer)
298
+ . map_err ( py_err_se_err) ?{
299
+ continue ;
300
+ }
301
+ if exclude_if ( & field. exclude_if , & value, serializer) . map_err ( py_err_se_err) ? {
302
+ continue ;
297
303
}
304
+ let s = PydanticSerializer :: new (
305
+ & value,
306
+ serializer,
307
+ next_include. as_ref ( ) ,
308
+ next_exclude. as_ref ( ) ,
309
+ & field_extra,
310
+ ) ;
311
+ let output_key = field. get_key_json ( key_str, & field_extra) ;
312
+ map. serialize_entry ( & output_key, & s) ?;
298
313
}
299
314
} else if self . mode == FieldsMode :: TypedDictAllow {
300
315
let output_key = infer_json_key ( & key, & field_extra) . map_err ( py_err_se_err) ?;
0 commit comments