@@ -306,6 +306,7 @@ class SchemaSerializer:
306
306
exclude_defaults : bool = False ,
307
307
exclude_none : bool = False ,
308
308
round_trip : bool = False ,
309
+ sort_keys : bool = False ,
309
310
warnings : bool | Literal ['none' , 'warn' , 'error' ] = True ,
310
311
fallback : Callable [[Any ], Any ] | None = None ,
311
312
serialize_as_any : bool = False ,
@@ -326,6 +327,7 @@ class SchemaSerializer:
326
327
exclude_defaults: Whether to exclude fields that are equal to their default value.
327
328
exclude_none: Whether to exclude fields that have a value of `None`.
328
329
round_trip: Whether to enable serialization and validation round-trip support.
330
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
329
331
warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
330
332
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
331
333
fallback: A function to call when an unknown value is encountered,
@@ -352,6 +354,7 @@ class SchemaSerializer:
352
354
exclude_defaults : bool = False ,
353
355
exclude_none : bool = False ,
354
356
round_trip : bool = False ,
357
+ sort_keys : bool = False ,
355
358
warnings : bool | Literal ['none' , 'warn' , 'error' ] = True ,
356
359
fallback : Callable [[Any ], Any ] | None = None ,
357
360
serialize_as_any : bool = False ,
@@ -371,6 +374,7 @@ class SchemaSerializer:
371
374
exclude_defaults: Whether to exclude fields that are equal to their default value.
372
375
exclude_none: Whether to exclude fields that have a value of `None`.
373
376
round_trip: Whether to enable serialization and validation round-trip support.
377
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
374
378
warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
375
379
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
376
380
fallback: A function to call when an unknown value is encountered,
@@ -398,6 +402,7 @@ def to_json(
398
402
by_alias : bool = True ,
399
403
exclude_none : bool = False ,
400
404
round_trip : bool = False ,
405
+ sort_keys : bool = False ,
401
406
timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
402
407
bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
403
408
inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -419,6 +424,7 @@ def to_json(
419
424
by_alias: Whether to use the alias names of fields.
420
425
exclude_none: Whether to exclude fields that have a value of `None`.
421
426
round_trip: Whether to enable serialization and validation round-trip support.
427
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
422
428
timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
423
429
bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
424
430
inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
@@ -477,6 +483,7 @@ def to_jsonable_python(
477
483
by_alias : bool = True ,
478
484
exclude_none : bool = False ,
479
485
round_trip : bool = False ,
486
+ sort_keys : bool = False ,
480
487
timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
481
488
bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
482
489
inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -498,6 +505,7 @@ def to_jsonable_python(
498
505
by_alias: Whether to use the alias names of fields.
499
506
exclude_none: Whether to exclude fields that have a value of `None`.
500
507
round_trip: Whether to enable serialization and validation round-trip support.
508
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
501
509
timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
502
510
bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
503
511
inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
0 commit comments