@@ -305,6 +305,7 @@ class SchemaSerializer:
305
305
exclude_defaults : bool = False ,
306
306
exclude_none : bool = False ,
307
307
round_trip : bool = False ,
308
+ sort_keys : bool = False ,
308
309
warnings : bool | Literal ['none' , 'warn' , 'error' ] = True ,
309
310
fallback : Callable [[Any ], Any ] | None = None ,
310
311
serialize_as_any : bool = False ,
@@ -325,6 +326,7 @@ class SchemaSerializer:
325
326
exclude_defaults: Whether to exclude fields that are equal to their default value.
326
327
exclude_none: Whether to exclude fields that have a value of `None`.
327
328
round_trip: Whether to enable serialization and validation round-trip support.
329
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
328
330
warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
329
331
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
330
332
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 ,
@@ -373,6 +376,7 @@ class SchemaSerializer:
373
376
exclude_defaults: Whether to exclude fields that are equal to their default value.
374
377
exclude_none: Whether to exclude fields that have a value of `None`.
375
378
round_trip: Whether to enable serialization and validation round-trip support.
379
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
376
380
warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
377
381
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
378
382
fallback: A function to call when an unknown value is encountered,
@@ -401,6 +405,7 @@ def to_json(
401
405
by_alias : bool = True ,
402
406
exclude_none : bool = False ,
403
407
round_trip : bool = False ,
408
+ sort_keys : bool = False ,
404
409
timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
405
410
bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
406
411
inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -424,6 +429,7 @@ def to_json(
424
429
by_alias: Whether to use the alias names of fields.
425
430
exclude_none: Whether to exclude fields that have a value of `None`.
426
431
round_trip: Whether to enable serialization and validation round-trip support.
432
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
427
433
timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
428
434
bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
429
435
inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
@@ -482,6 +488,7 @@ def to_jsonable_python(
482
488
by_alias : bool = True ,
483
489
exclude_none : bool = False ,
484
490
round_trip : bool = False ,
491
+ sort_keys : bool = False ,
485
492
timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
486
493
bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
487
494
inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -503,6 +510,7 @@ def to_jsonable_python(
503
510
by_alias: Whether to use the alias names of fields.
504
511
exclude_none: Whether to exclude fields that have a value of `None`.
505
512
round_trip: Whether to enable serialization and validation round-trip support.
513
+ sort_keys: Whether to sort dictionary keys. If True, all dictionary keys will be sorted alphabetically, including nested dictionaries.
506
514
timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
507
515
bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
508
516
inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
0 commit comments