Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(media): add an example for using a custom JSON encoder #2035

Merged
merged 8 commits into from Mar 8, 2022

Conversation

maxking
Copy link
Contributor

@maxking maxking commented Mar 6, 2022

Summary of Changes

Adds an example on how to override the default json encoder used by Falcon.

Pull Request Checklist

This is just a reminder about the most common mistakes. Please make sure that you tick all appropriate boxes. But please read our contribution guide at least once; it will save you a few review cycles!

If an item doesn't apply to your pull request, check it anyway to make it apparent that there's nothing to do.

  • Applied changes to both WSGI and ASGI code paths and interfaces (where applicable).
  • Added tests for changed code.
  • Prefixed code comments with GitHub nick and an appropriate prefix.
  • Coding style is consistent with the rest of the framework.
  • Updated documentation for changed code.
    • Added docstrings for any new classes, functions, or modules.
    • Updated docstrings for any modifications to existing code.
    • Updated both WSGI and ASGI docs (where applicable).
    • Added references to new classes, functions, or modules to the relevant RST file under docs/.
    • Updated all relevant supporting documentation files under docs/.
    • A copyright notice is included at the top of any new modules (using your own name or the name of your organization).
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Changes (and possible deprecations) have towncrier news fragments under docs/_newsfragments/, with the file name format {issue_number}.{fragment_type}.rst. (Run towncrier --draft to ensure it renders correctly.)

@codecov
Copy link

codecov bot commented Mar 6, 2022

Codecov Report

Merging #2035 (d5418c0) into master (43a8e80) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2035   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           63        63           
  Lines         6707      6707           
  Branches      1238      1238           
=========================================
  Hits          6707      6707           
Impacted Files Coverage Δ
falcon/media/json.py 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 43a8e80...d5418c0. Read the comment docs.

@vytas7 vytas7 changed the title Add an example for using a custom json encoder with media handlers. docs(media): add an example for using a custom JSON encoder Mar 7, 2022
Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

Thanks for this improvement to our docs @maxking!

This is not critical to pass review, but while at it, could you also link to the new paragraph from this FAQ answer: How can I use resp.media with types like datetime?

falcon/media/json.py Outdated Show resolved Hide resolved
@vytas7 vytas7 requested review from CaselIT and kgriffs March 7, 2022 20:07
@maxking
Copy link
Contributor Author

maxking commented Mar 7, 2022

Working on adding a reference back from FAQ, Sphinx doesn't seem to like sections inside the docstrings and I am trying to see how to create a reference without a section in a paragraph. Will update the PR as soon as I can figure that out. :D

CaselIT
CaselIT previously approved these changes Mar 7, 2022
falcon/media/json.py Outdated Show resolved Hide resolved
Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

This LGTM bar one typo.

And wrt linking to a target inside a docstring, it is apparently possible using some kind of marker that isn't a section (as sections are disallowed inside docstring), for instance, a rubric.

The following seems to work, but it adds visible rubrics that we don't use much elsewhere, so probably not worth the effort:

diff --git a/docs/user/faq.rst b/docs/user/faq.rst
index 9d3f09d..60c104f 100644
--- a/docs/user/faq.rst
+++ b/docs/user/faq.rst
@@ -1239,3 +1239,8 @@ Alternatively, you can set the Cookie header directly as demonstrated in this ve
 To include multiple values, simply use ``"; "`` to separate each name-value
 pair. For example, if you were to pass ``{'Cookie': 'xxx=yyy; hello=world'}``,
 you would get ``{'cookies': {'xxx': 'yyy', 'hello': 'world'}}``.
+
+How do I link to a rubric inside a docstring
+--------------------------------------------
+
+Trying... :ref:`target_to_paragraph`
diff --git a/falcon/media/json.py b/falcon/media/json.py
index e6cb0cf..a0359ae 100644
--- a/falcon/media/json.py
+++ b/falcon/media/json.py
@@ -179,6 +179,10 @@ class JSONHandlerWS(TextBaseHandlerWS):
         app = falcon.asgi.App()
         app.ws_options.media_handlers[falcon.WebSocketPayloadType.TEXT] = json_handler
 
+    .. _target_to_paragraph:
+
+    .. rubric:: About ensure_ascii
+
     By default, ``ensure_ascii`` is passed to the ``json.dumps`` function.
     If you override the ``dumps`` function, you will need to explicitly set
     ``ensure_ascii`` to ``False`` in order to enable the serialization of

docs/user/faq.rst Outdated Show resolved Hide resolved
@maxking
Copy link
Contributor Author

maxking commented Mar 7, 2022

Thanks @vytas7, updated the PR.

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

LGTM 👍
(I've taken the liberty of tweaking those rubrics a bit to my liking.)

Thanks for this improvement to our docs, @maxking!

@vytas7 vytas7 requested a review from CaselIT March 8, 2022 19:34
@vytas7 vytas7 merged commit 89a615a into falconry:master Mar 8, 2022
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.

None yet

3 participants