json2 FastAPI 의 jsonable_encoder 들여다보기 jsonable_encoder 이란? FastAPI 에서 제공하는 인코더 함수로, 보통은 클라이언트로 전송하기 전에 응답하는 객체를 json 으로 인코딩할 수 있도록 변환해 주는 역할을 합니다. (참고로 json 으로 변환해 주는 것은 아닙니다!) 실제로 FastAPI 는 jsonable_encoder 를 어떻게 사용할까요? 아래 코드는 요청이 유효하지 않을 때 에러를 응답하는 함수입니다. # fastapi.exception_handlers.py async def request_validation_exception_handler( request: Request, exc: RequestValidationError ) -> JSONResponse: return JSONResponse( status_code=.. 2024. 1. 18. Python JSON 직렬화 TypeError 핸들링하기 json 변환 시 타입에러 발생 아래와 같이 Decimal 타입을 json 으로 변환하는 코드가 있다고 해보자. import json from decimal import Decimal data = {"value": Decimal("123.456")} json_str = json.dumps(data) print(json_str) 이를 실행하면 다음의 에러가 발생한다. raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Decimal is not JSON serializable 왜냐하면 json 라이브러리는 기본적으로 다음 타입들만 직렬화를 지원하기 때문이다. dict list, tuple str int, fl.. 2023. 9. 26. 이전 1 다음