fastapi
fastapi copied to clipboard
✨ Add `SimpleJSONResponse` for decimal JSON encoding
As mentioned in #4998. I believe simplejson
is the best option to accomplish high decimal points real number encoding, hence the need of SimpleJSONResponse
. I followed some of the parameters used in starlette
's original JSONResponse
here to make sure the output are the most consistent as possible.
If interested, I'm also willing to draft a page of doc detailing the best practice of handling decimal in FastAPI I've come up so far. Just let me know if that's something you'd like to include.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Comparison is base (
1876ebc
) 100.00% compared to head (a837c23
) 100.00%. Report is 1385 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #5023 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 532 533 +1
Lines 13672 13740 +68
=========================================
+ Hits 13672 13740 +68
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
📝 Docs preview for commit abf736a6ff5f18ff30af2b90721f7773389b9aab at: https://62a4c36223b63b38e9736ac6--fastapi.netlify.app
📝 Docs preview for commit 1bbee018e2596e010842d05654e6f8130dfd1729 at: https://62a4c5ed1ac5a03f5530312d--fastapi.netlify.app
Maybe you can make some tests and write some docs too
📝 Docs preview for commit 96e3b5f071f6e95287c63bf65a1a2183a41c75c6 at: https://62ab81ec445265209b184aea--fastapi.netlify.app
Hi folks,
as suggested, I've written a page detailing on how to make use of simpleJSONResponse
and also some tips on how to make Decimal
works with FastAPI.
This is my first time authoring a complete doc for open source project, feedbacks are welcomed 👍🏻
📝 Docs preview for commit a837c23094794a5b8ccb4b2cb7ae6a1a8fdb9e15 at: https://62da846ddace10066081dd5e--fastapi.netlify.app
I could really use this ASAP. Any chance this will get merged soon?
@tiangolo Would you mind taking a minute to review this? Docs and tests ready, reviewed by others.
Thank you @tomy0000000! 🍰
Nevertheless, instead of providing another type of response, I prefer to teach people how to create their own, in particular when creating a new response takes an effort equivalent to learning a new one included, also as not everyone will need it, but if it's included, everything learning could expect to need to learn it. And as it's around 2 or 3 lines of code, I prefer to teach how to use it: https://fastapi.tiangolo.com/advanced/custom-response/#custom-response-class
But you could, for example, write a blog post about using decimal values in JSON responses with your use case, and how to create the SimpleJSONResponse
. 🤓
For now, I'll pass on this one, but thanks for the effort! ☕