@@ -305,6 +305,7 @@ class SchemaSerializer:
305305 exclude_defaults : bool = False ,
306306 exclude_none : bool = False ,
307307 round_trip : bool = False ,
308+ sort_keys : bool = False ,
308309 warnings : bool | Literal ['none' , 'warn' , 'error' ] = True ,
309310 fallback : Callable [[Any ], Any ] | None = None ,
310311 serialize_as_any : bool = False ,
@@ -325,6 +326,7 @@ class SchemaSerializer:
325326 exclude_defaults: Whether to exclude fields that are equal to their default value.
326327 exclude_none: Whether to exclude fields that have a value of `None`.
327328 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.
328330 warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
329331 "error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
330332 fallback: A function to call when an unknown value is encountered,
@@ -352,6 +354,7 @@ class SchemaSerializer:
352354 exclude_defaults : bool = False ,
353355 exclude_none : bool = False ,
354356 round_trip : bool = False ,
357+ sort_keys : bool = False ,
355358 warnings : bool | Literal ['none' , 'warn' , 'error' ] = True ,
356359 fallback : Callable [[Any ], Any ] | None = None ,
357360 serialize_as_any : bool = False ,
@@ -373,6 +376,7 @@ class SchemaSerializer:
373376 exclude_defaults: Whether to exclude fields that are equal to their default value.
374377 exclude_none: Whether to exclude fields that have a value of `None`.
375378 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.
376380 warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
377381 "error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
378382 fallback: A function to call when an unknown value is encountered,
@@ -401,6 +405,7 @@ def to_json(
401405 by_alias : bool = True ,
402406 exclude_none : bool = False ,
403407 round_trip : bool = False ,
408+ sort_keys : bool = False ,
404409 timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
405410 bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
406411 inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -424,6 +429,7 @@ def to_json(
424429 by_alias: Whether to use the alias names of fields.
425430 exclude_none: Whether to exclude fields that have a value of `None`.
426431 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.
427433 timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
428434 bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
429435 inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
@@ -482,6 +488,7 @@ def to_jsonable_python(
482488 by_alias : bool = True ,
483489 exclude_none : bool = False ,
484490 round_trip : bool = False ,
491+ sort_keys : bool = False ,
485492 timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
486493 bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
487494 inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -503,6 +510,7 @@ def to_jsonable_python(
503510 by_alias: Whether to use the alias names of fields.
504511 exclude_none: Whether to exclude fields that have a value of `None`.
505512 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.
506514 timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
507515 bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
508516 inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
0 commit comments