@@ -306,6 +306,7 @@ class SchemaSerializer:
306306 exclude_defaults : bool = False ,
307307 exclude_none : bool = False ,
308308 round_trip : bool = False ,
309+ sort_keys : bool = False ,
309310 warnings : bool | Literal ['none' , 'warn' , 'error' ] = True ,
310311 fallback : Callable [[Any ], Any ] | None = None ,
311312 serialize_as_any : bool = False ,
@@ -326,6 +327,7 @@ class SchemaSerializer:
326327 exclude_defaults: Whether to exclude fields that are equal to their default value.
327328 exclude_none: Whether to exclude fields that have a value of `None`.
328329 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.
329331 warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
330332 "error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
331333 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 ,
@@ -371,6 +374,7 @@ class SchemaSerializer:
371374 exclude_defaults: Whether to exclude fields that are equal to their default value.
372375 exclude_none: Whether to exclude fields that have a value of `None`.
373376 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.
374378 warnings: How to handle invalid fields. False/"none" ignores them, True/"warn" logs errors,
375379 "error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
376380 fallback: A function to call when an unknown value is encountered,
@@ -398,6 +402,7 @@ def to_json(
398402 by_alias : bool = True ,
399403 exclude_none : bool = False ,
400404 round_trip : bool = False ,
405+ sort_keys : bool = False ,
401406 timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
402407 bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
403408 inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -419,6 +424,7 @@ def to_json(
419424 by_alias: Whether to use the alias names of fields.
420425 exclude_none: Whether to exclude fields that have a value of `None`.
421426 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.
422428 timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
423429 bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
424430 inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
@@ -477,6 +483,7 @@ def to_jsonable_python(
477483 by_alias : bool = True ,
478484 exclude_none : bool = False ,
479485 round_trip : bool = False ,
486+ sort_keys : bool = False ,
480487 timedelta_mode : Literal ['iso8601' , 'float' ] = 'iso8601' ,
481488 bytes_mode : Literal ['utf8' , 'base64' , 'hex' ] = 'utf8' ,
482489 inf_nan_mode : Literal ['null' , 'constants' , 'strings' ] = 'constants' ,
@@ -498,6 +505,7 @@ def to_jsonable_python(
498505 by_alias: Whether to use the alias names of fields.
499506 exclude_none: Whether to exclude fields that have a value of `None`.
500507 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.
501509 timedelta_mode: How to serialize `timedelta` objects, either `'iso8601'` or `'float'`.
502510 bytes_mode: How to serialize `bytes` objects, either `'utf8'`, `'base64'`, or `'hex'`.
503511 inf_nan_mode: How to serialize `Infinity`, `-Infinity` and `NaN` values, either `'null'`, `'constants'`, or `'strings'`.
0 commit comments