fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

✨ Add `SimpleJSONResponse` for decimal JSON encoding

Open tomy0000000 opened this issue 2 years ago • 7 comments

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.

tomy0000000 avatar Jun 11 '22 16:06 tomy0000000

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.

codecov[bot] avatar Jun 11 '22 16:06 codecov[bot]

📝 Docs preview for commit abf736a6ff5f18ff30af2b90721f7773389b9aab at: https://62a4c36223b63b38e9736ac6--fastapi.netlify.app

github-actions[bot] avatar Jun 11 '22 16:06 github-actions[bot]

📝 Docs preview for commit 1bbee018e2596e010842d05654e6f8130dfd1729 at: https://62a4c5ed1ac5a03f5530312d--fastapi.netlify.app

github-actions[bot] avatar Jun 11 '22 16:06 github-actions[bot]

Maybe you can make some tests and write some docs too

odiseo0 avatar Jun 11 '22 18:06 odiseo0

📝 Docs preview for commit 96e3b5f071f6e95287c63bf65a1a2183a41c75c6 at: https://62ab81ec445265209b184aea--fastapi.netlify.app

github-actions[bot] avatar Jun 16 '22 19:06 github-actions[bot]

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 👍🏻

tomy0000000 avatar Jun 16 '22 19:06 tomy0000000

📝 Docs preview for commit a837c23094794a5b8ccb4b2cb7ae6a1a8fdb9e15 at: https://62da846ddace10066081dd5e--fastapi.netlify.app

github-actions[bot] avatar Jul 22 '22 11:07 github-actions[bot]

I could really use this ASAP. Any chance this will get merged soon?

theRealSuperMario avatar Jul 21 '23 15:07 theRealSuperMario

@tiangolo Would you mind taking a minute to review this? Docs and tests ready, reviewed by others.

tomy0000000 avatar Jul 22 '23 15:07 tomy0000000

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! ☕

tiangolo avatar Jan 16 '24 14:01 tiangolo