Skip to content

Conversation

samuelcolvin
Copy link
Member

an alternative to #220. Needs a few more tests and docs but otherwise complete.

basically, I've added a json_encoders Config settings, allowing custom typing encoding defined on a model:

class Model(BaseModel):
    x: datetime.timedelta

    class Config:
        json_encoders = {datetime.timedelta: timedelta_isoformat}

m = Model(x=123)
assert m.json() == '{"x": "P0DT0H2M3.000000S"}'

@Gr1N and @cfkanesan I'd appreciate your feedback on this.

@codecov
Copy link

codecov bot commented Aug 20, 2018

Codecov Report

Merging #247 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #247   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          11     11           
  Lines        1463   1478   +15     
  Branches      270    273    +3     
=====================================
+ Hits         1463   1478   +15

@samuelcolvin samuelcolvin force-pushed the timedelta-json-encoding branch from d6c6bbc to b620510 Compare August 20, 2018 10:16
@@ -134,11 +136,16 @@ def __new__(mcs, name, bases, namespace):
)

vg.check_for_unused()
if config.json_encoders:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify 4 lines into one, because pydantic_encoder will be called inside custom_pydantic_encoder in case if custom encoder not found:

json_encoder = partial(custom_pydantic_encoder, config.json_encoders)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this won't be slow? I don't really know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be slow, agree

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that it's not much extra code and json_encoder could be called a lot, let's keep it like this.

pydantic/json.py Outdated
return pydantic_encoder(obj)


def timedelta_isoformat(td: datetime.timedelta):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing returning type hint

@Gr1N
Copy link
Contributor

Gr1N commented Aug 20, 2018

Overall looks good to me.

@samuelcolvin samuelcolvin force-pushed the timedelta-json-encoding branch from bd666a1 to 110c7e1 Compare August 25, 2018 15:55
@samuelcolvin samuelcolvin merged commit f46dc0c into master Aug 25, 2018
@samuelcolvin samuelcolvin deleted the timedelta-json-encoding branch August 25, 2018 16:06
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
* build binaries for 3.11

* [full-build] remove 3.11 from windows builds

* print commit message for debugging

* try ci on push

* trigger full build

* trigger full build, try again

* trigger full build, try again 2

* [skip ci] remove lint prints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants