How to use the fastapi.encoders.jsonable_encoder function in fastapi

To help you get started, we’ve selected a few fastapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tiangolo / fastapi / tests / test_jsonable_encoder.py View on Github external
def test_encode_unsupported():
    unserializable = Unserializable()
    with pytest.raises(ValueError):
        jsonable_encoder(unserializable)
github tiangolo / full-stack-fastapi-couchbase / {{cookiecutter.project_slug}} / backend / app / app / crud / utils.py View on Github external
def upsert(
    bucket: Bucket, *, doc_id: str, doc_in: PydanticModel, persist_to=0, ttl=0
) -> Optional[PydanticModel]:
    doc_data = jsonable_encoder(doc_in)
    with bucket.durability(
        persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS
    ):
        result = bucket.upsert(doc_id, doc_data, ttl=ttl)
        if result.success:
            return doc_in
    return None
github tiangolo / full-stack-fastapi-couchbase / {{cookiecutter.project_slug}} / backend / app / app / crud / user.py View on Github external
def update_in_db(bucket: Bucket, *, username: str, user_in: UserUpdate, persist_to=0):
    user_doc_id = get_doc_id(username)
    stored_user = get(bucket, username=username)
    stored_user = stored_user.copy(update=user_in.dict(skip_defaults=True))
    if user_in.password:
        passwordhash = get_password_hash(user_in.password)
        stored_user.hashed_password = passwordhash
    data = jsonable_encoder(stored_user)
    with bucket.durability(
        persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS
    ):
        bucket.upsert(user_doc_id, data)
    return stored_user
github tiangolo / fastapi / fastapi / openapi / utils.py View on Github external
if path:
                    paths.setdefault(openapi_prefix + route.path_format, {}).update(
                        path
                    )
                if security_schemes:
                    components.setdefault("securitySchemes", {}).update(
                        security_schemes
                    )
                if path_definitions:
                    definitions.update(path_definitions)
    if definitions:
        components["schemas"] = {k: definitions[k] for k in sorted(definitions)}
    if components:
        output["components"] = components
    output["paths"] = paths
    return jsonable_encoder(OpenAPI(**output), by_alias=True, include_none=False)
github tiangolo / fastapi / fastapi / routing.py View on Github external
) -> Any:
    if field:
        errors = []
        if exclude_unset and isinstance(response, BaseModel):
            if PYDANTIC_1:
                response = response.dict(exclude_unset=exclude_unset)
            else:
                response = response.dict(skip_defaults=exclude_unset)  # pragma: nocover
        value, errors_ = field.validate(response, {}, loc=("response",))
        if isinstance(errors_, ErrorWrapper):
            errors.append(errors_)
        elif isinstance(errors_, list):
            errors.extend(errors_)
        if errors:
            raise ValidationError(errors, field.type_)
        return jsonable_encoder(
            value,
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
        )
    else:
        return jsonable_encoder(response)
github augerai / a2ml / a2ml / server / server.py View on Github external
def __render_json_response(data):
    res = {
        'meta': { 'status': 200 },
        'data': data,
    }

    content = jsonable_encoder(res)
    return JSONResponse(content=content)
github dmontagu / fastapi_client / example / client / api / user_api.py View on Github external
def _build_for_create_users_with_list_input(self, body: List[m.User]) -> Awaitable[None]:
        body = jsonable_encoder(body)

        return self.api_client.request(type_=None, method="POST", url="/user/createWithList", json=body)