MCPcopy
hub / github.com/fastapi/fastapi / jsonable_encoder

Function jsonable_encoder

fastapi/encoders.py:129–364  ·  view source on GitHub ↗

Convert any object to something that can be encoded in JSON. This is used internally by FastAPI to make sure anything you return can be encoded as JSON before it is sent to the client. You can also use it yourself, for example to convert objects before saving them in a databas

(
    obj: Annotated[
        Any,
        Doc(
            """
            The input object to convert to JSON.
            """
        ),
    ],
    include: Annotated[
        IncEx | None,
        Doc(
            """
            Pydantic's `include` parameter, passed to Pydantic models to set the
            fields to include.
            """
        ),
    ] = None,
    exclude: Annotated[
        IncEx | None,
        Doc(
            """
            Pydantic's `exclude` parameter, passed to Pydantic models to set the
            fields to exclude.
            """
        ),
    ] = None,
    by_alias: Annotated[
        bool,
        Doc(
            """
            Pydantic's `by_alias` parameter, passed to Pydantic models to define if
            the output should use the alias names (when provided) or the Python
            attribute names. In an API, if you set an alias, it's probably because you
            want to use it in the result, so you probably want to leave this set to
            `True`.
            """
        ),
    ] = True,
    exclude_unset: Annotated[
        bool,
        Doc(
            """
            Pydantic's `exclude_unset` parameter, passed to Pydantic models to define
            if it should exclude from the output the fields that were not explicitly
            set (and that only had their default values).
            """
        ),
    ] = False,
    exclude_defaults: Annotated[
        bool,
        Doc(
            """
            Pydantic's `exclude_defaults` parameter, passed to Pydantic models to define
            if it should exclude from the output the fields that had the same default
            value, even when they were explicitly set.
            """
        ),
    ] = False,
    exclude_none: Annotated[
        bool,
        Doc(
            """
            Pydantic's `exclude_none` parameter, passed to Pydantic models to define
            if it should exclude from the output any fields that have a `None` value.
            """
        ),
    ] = False,
    custom_encoder: Annotated[
        dict[Any, Callable[[Any], Any]] | None,
        Doc(
            """
            Pydantic's `custom_encoder` parameter, passed to Pydantic models to define
            a custom encoder.
            """
        ),
    ] = None,
    sqlalchemy_safe: Annotated[
        bool,
        Doc(
            """
            Exclude from the output any fields that start with the name `_sa`.

            This is mainly a hack for compatibility with SQLAlchemy objects, they
            store internal SQLAlchemy-specific state in attributes named with `_sa`,
            and those objects can't (and shouldn't be) serialized to JSON.
            """
        ),
    ] = True,
)

Source from the content-addressed store, hash-verified

127
128
129def jsonable_encoder(
130 obj: Annotated[
131 Any,
132 Doc(
133 """
134 The input object to convert to JSON.
135 """
136 ),
137 ],
138 include: Annotated[
139 IncEx | None,
140 Doc(
141 """
142 Pydantic's `include` parameter, passed to Pydantic models to set the
143 fields to include.
144 """
145 ),
146 ] = None,
147 exclude: Annotated[
148 IncEx | None,
149 Doc(
150 """
151 Pydantic's `exclude` parameter, passed to Pydantic models to set the
152 fields to exclude.
153 """
154 ),
155 ] = None,
156 by_alias: Annotated[
157 bool,
158 Doc(
159 """
160 Pydantic's `by_alias` parameter, passed to Pydantic models to define if
161 the output should use the alias names (when provided) or the Python
162 attribute names. In an API, if you set an alias, it's probably because you
163 want to use it in the result, so you probably want to leave this set to
164 `True`.
165 """
166 ),
167 ] = True,
168 exclude_unset: Annotated[
169 bool,
170 Doc(
171 """
172 Pydantic's `exclude_unset` parameter, passed to Pydantic models to define
173 if it should exclude from the output the fields that were not explicitly
174 set (and that only had their default values).
175 """
176 ),
177 ] = False,
178 exclude_defaults: Annotated[
179 bool,
180 Doc(
181 """
182 Pydantic's `exclude_defaults` parameter, passed to Pydantic models to define
183 if it should exclude from the output the fields that had the same default
184 value, even when they were explicitly set.
185 """
186 ),

Callers 15

serialize_responseFunction · 0.90
_serialize_dataFunction · 0.90
_serialize_sse_itemFunction · 0.90
get_openapiFunction · 0.90
get_swagger_ui_htmlFunction · 0.90
update_itemFunction · 0.90
update_itemFunction · 0.90

Used in the wild real call sites across dependent graphs

searching dependent graphs…