I want to have Pydantic write booleans as either "y" or "n". The Pydantic documentation on encoders suggests that I can configure a way to encode values based on their data type, in my case bool.
from pydantic import BaseModel
class MyStuff(BaseModel):
do_it: bool = False
class Config:
json_encoders = {
bool: lambda b: "y" if b else "n",
}
ms = MyStuff()
print(ms.json(exclude_defaults=False))
When I run this, I get
{"do_it": false}
when I expected
{"do_it": "n"}
Other tries:
It does not matter whether I specify a default or not.
I cannot get it to work for other basic data types like
inteither, but it does work forpathlib.Path.It does not work either if I replace the
lambdawith a function like this:
def convert_my_bool(b: bool) -> str:
if b:
return "y"
return "n"
class MyStuff(BaseModel):
do_it: bool
class Config:
json_encoders = {
bool: convert_my_bool,
}
What am I missing here? How do I need to write my Config to write booleans in my model as "y" or "n"?