Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

912
Vistas
override default encoders for jsonable_encoder in FastAPI

I have code that looks something like this using Fast API:

class EnumTestT(Enum):
    test_t_value = 0

object = { 
    test: test_t_value
}

enum_mapping = {
    test_t_value: "Test"
}

def enum_encoder(val: EnumTestT) -> str:
    return enum_mapping[val]

custom_encoder = {
    EnumTestT: enum_encoder
}

@app.get("/test")
async def test_get():
    return jsonable_encoder(object, custom_encoder=custom_encoder)

The issue is that jsonable_encoder applies custom encoders after the defaults. Is there any way to apply them before the default encoders. Because for an Enum and any derived classes the value of the enum is reported instead of the mapped value.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

FastAPI use ENCODERS_BY_TYPE (from pydantic.json) to encode some basic data type.

ENCODERS_BY_TYPE: Dict[Type[Any], Callable[[Any], Any]] = {
    bytes: lambda o: o.decode(),
    Color: str,
    datetime.date: isoformat,
    datetime.datetime: isoformat,
    datetime.time: isoformat,
    datetime.timedelta: lambda td: td.total_seconds(),
    Decimal: decimal_encoder,
    Enum: lambda o: o.value,

so for me to override the default datetime encode, just like

 ENCODERS_BY_TYPE[datetime] = lambda date_obj: date_obj.strftime("%Y-%m-%d %H:%M:%S")

over 4 years ago · Santiago Trujillo Denunciar

0

Right now I am using custom encoder within json.dumps like this:

class TestEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, EnumTestT):
            return TestEncoder.process_enum_test_t(obj)

and response within the FastAPI app would be:

json_str = json.dumps(json_list, cls=TestEncoder).encode('utf-8')
return Response(media_type="application/json", content=json_str)

The problem with FastAPI using custom encoders is that custom encoder is invoked after all the standard encoders have been invoked and there is no way to override that order.

over 4 years ago · Santiago Trujillo Denunciar

0

inspired by coder vx

import datetime
from fastapi.responses import JSONResponse 
...
content = something to serialize 
JSONResponse(jsonable_encoder(content,
    custom_encoder={datetime.datetime: lambda date_obj: 
    date_obj.strftime("%Y-%m-%d %H:%M:%S")})))
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda