Skip to content

model_dump should serialise bools as bools for json #7294

Closed
@Booplicate

Description

@Booplicate

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Currently when dumping model to json, objects of type bool are being serialised as integers in json. I think this is a bug because json natively supports booleans (true/false).

Example Code

class SomeModel(BaseModel):
    value: StrictFloat | StrictInt | StrictBool

m = SomeModel(value=True)

# Passes
assert type(m.model_dump(mode="python")["value"]) is bool
# This will fail
assert type(m.model_dump(mode="json")["value"]) is bool, "didn't serialise as bool"

Python, Pydantic & OS Version

Python 3.11.4
Pydantic 2.1.1
Pydantic Core 2.4.0
Windows 10

Selected Assignee: @davidhewitt

Metadata

Metadata

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions