!!! abstract "Usage Documentation" [`model_dump_json`](../concepts/serialization.md#json-mode) Generates a JSON representation of the model using Pydantic's `to_json` method. Args: indent: Indentation to use in the JSON output. If None is passed, the output
(
self,
*,
indent: int | None = None,
ensure_ascii: bool = False,
include: IncEx | None = None,
exclude: IncEx | None = None,
context: Any | None = None,
by_alias: bool | None = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal['none', 'warn', 'error'] = True,
fallback: Callable[[Any], Any] | None = None,
serialize_as_any: bool = False,
polymorphic_serialization: bool | None = None,
)
| 491 | ) |
| 492 | |
| 493 | def model_dump_json( |
| 494 | self, |
| 495 | *, |
| 496 | indent: int | None = None, |
| 497 | ensure_ascii: bool = False, |
| 498 | include: IncEx | None = None, |
| 499 | exclude: IncEx | None = None, |
| 500 | context: Any | None = None, |
| 501 | by_alias: bool | None = None, |
| 502 | exclude_unset: bool = False, |
| 503 | exclude_defaults: bool = False, |
| 504 | exclude_none: bool = False, |
| 505 | exclude_computed_fields: bool = False, |
| 506 | round_trip: bool = False, |
| 507 | warnings: bool | Literal['none', 'warn', 'error'] = True, |
| 508 | fallback: Callable[[Any], Any] | None = None, |
| 509 | serialize_as_any: bool = False, |
| 510 | polymorphic_serialization: bool | None = None, |
| 511 | ) -> str: |
| 512 | """!!! abstract "Usage Documentation" |
| 513 | [`model_dump_json`](../concepts/serialization.md#json-mode) |
| 514 | |
| 515 | Generates a JSON representation of the model using Pydantic's `to_json` method. |
| 516 | |
| 517 | Args: |
| 518 | indent: Indentation to use in the JSON output. If None is passed, the output will be compact. |
| 519 | ensure_ascii: If `True`, the output is guaranteed to have all incoming non-ASCII characters escaped. |
| 520 | If `False` (the default), these characters will be output as-is. |
| 521 | include: Field(s) to include in the JSON output. |
| 522 | exclude: Field(s) to exclude from the JSON output. |
| 523 | context: Additional context to pass to the serializer. |
| 524 | by_alias: Whether to serialize using field aliases. |
| 525 | exclude_unset: Whether to exclude fields that have not been explicitly set. |
| 526 | exclude_defaults: Whether to exclude fields that are set to their default value. |
| 527 | exclude_none: Whether to exclude fields that have a value of `None`. |
| 528 | exclude_computed_fields: Whether to exclude computed fields. |
| 529 | While this can be useful for round-tripping, it is usually recommended to use the dedicated |
| 530 | `round_trip` parameter instead. |
| 531 | round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T]. |
| 532 | warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors, |
| 533 | "error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError]. |
| 534 | fallback: A function to call when an unknown value is encountered. If not provided, |
| 535 | a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised. |
| 536 | serialize_as_any: Whether to serialize fields with duck-typing serialization behavior. |
| 537 | polymorphic_serialization: Whether to use model and dataclass polymorphic serialization for this call. |
| 538 | |
| 539 | Returns: |
| 540 | A JSON string representation of the model. |
| 541 | """ |
| 542 | return self.__pydantic_serializer__.to_json( |
| 543 | self, |
| 544 | indent=indent, |
| 545 | ensure_ascii=ensure_ascii, |
| 546 | include=include, |
| 547 | exclude=exclude, |
| 548 | context=context, |
| 549 | by_alias=by_alias, |
| 550 | exclude_unset=exclude_unset, |