biocore / biocore/microsetta-public-api

Custom JSON encoder for nan and inf

Open
#23 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2
Forks
4
PR merge metrics
No merged PRs in 30d

Description

See #21 for a discussion. At some point we may want to protect against serializing `NaN` and `inf` in JSON returned from flask, so we could do something like create a custom JSON encoder that replaces these values with `null`:

```
import flask

def safe_json_encoder(replace_nan=None, replace_inf=None):
class JSONEncoder(flask.json.JSONEncoder):

def default(self, o):
# some function that replaces nan with `replace_nan` and inf with `replace_inf`

return JSONEncoder

app = # create flask app
app.json_encoder = safe_json_encoder()

```

For more details see:
* [Pass user built JSON Encoder into Flask's jsonify](https://stackoverflow.com/questions/44146087/pass-user-built-json-encoder-into-flasks-jsonify)
* [Flask JSONEncoder documentation](https://flask.palletsprojects.com/en/1.1.x/api/#flask.json.JSONEncoder)
* [Sending NaN in json](https://stackoverflow.com/questions/6601812/sending-nan-in-json/6602204)
* [Loop through all nested dictionary values](https://stackoverflow.com/questions/10756427/loop-through-all-nested-dictionary-values) Note: there may be some intricacies in this step, given JSON structure.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with discussion #21 and Flask's JSONEncoder documentation, then trace how the API configures JSON responses. Define the expected behavior for NaN and inf, including nested JSON structures, and review the linked references before determining how it should be tested.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.