[BUG] jsonable_encoder does not use custom_encoder if Config.json_encoders is not defined.
See original GitHub issueDescribe the bug
With factory_boy and faker, I wanted to define a Factory, here is the code…
from datetime import datetime
import factory
import faker.utils.datetime_safe
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
class MyModel(BaseModel):
dt_field: datetime
class MyModelFactory(factory.Factory):
dt_field = factory.Faker("date_time") # should return a datetime
class Meta:
model = MyModel
inst = MyModelFactory.build()
assert isinstance(inst, MyModel)
assert isinstance(inst.dt_field, faker.utils.datetime_safe.datetime)
assert isinstance(inst.dt_field, datetime)
inst_encoded = jsonable_encoder(inst)
assert isinstance(inst_encoded["dt_field"], dict)
assert inst_encoded["dt_field"] != inst.dt_field.isoformat()
inst_encoded = jsonable_encoder(
inst, custom_encoder={faker.utils.datetime_safe.datetime: lambda o: o.isoformat()}
)
assert isinstance(inst_encoded["dt_field"], str)
assert inst_encoded["dt_field"] == inst.dt_field.isoformat()
To Reproduce Use my script
Expected behavior
custom_encoder
is not used…
Environment:
- OS: Linux
- FastAPI Version [e.g. 0.3.0], get it with:
import fastapi
print(fastapi.__version__)
FastAPI: 0.42.0
- Python version, get it with:
python --version
Python: 3.8.0
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top Results From Across the Web
override default encoders for jsonable_encoder in FastAPI
Right now I am using custom encoder within json.dumps like this: class TestEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, ...
Read more >json — JSON encoder and decoder — Python 3.11.1 ...
To use a custom JSONDecoder subclass, specify it with the cls kwarg; otherwise JSONDecoder is used. Additional keyword arguments will be passed to...
Read more >JSON Compatible Encoder - FastAPI
You can use jsonable_encoder for that. It receives an object, like a Pydantic model, and returns a JSON compatible version: Python 3.6 and...
Read more >Python JSON Serialize Set - PYnative
You are here because when you try to dump or encode Python set into JSON, you received an error, TypeError: Object of type...
Read more >Overriding default JSON encoding of floating point values?
Using JSONEncoder /JSONSerialization, floating point values will be ... bad request with an error message telling me that it's not a float if...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can we assume this issue is fixed with the merged changed in 0.47.1?
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.