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

Use flask.json as default encoder #874

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

juannorris
Copy link

Fix for #873.

Use flask.json instead of json standard library as the default JSON encoder.

Overriding behavior remains, but this adds the ability for flask_restful to pick up the encoder defined for the app, or the api blueprint.

from flask_restful import Api
from flask import Blueprint
from flask import json

class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        return 'custom_flask_encoder'

blueprint = Blueprint('test', __name__)
blueprint.json_encoder = CustomJSONEncoder
api = flask_restful.Api(blueprint)
app = Flask(__name__)
app.register_blueprint(blueprint, url_prefix='/blueprint')

or

app = Flask(__name__)
app.json_encoder = CustomJSONEncoder
api = flask_restful.Api(app)

Fix for flask-restful#873.

Use `flask.json` instead of `json` standard library as the default JSON encoder.

Overriding behavior remains, but this adds the ability for `flask_restful` to pick up the encoder defined for the app, or the api blueprint.

```python
from flask_restful import Api
from flask import Blueprint
from flask import json

class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        return 'custom_flask_encoder'

blueprint = Blueprint('test', __name__)
blueprint.json_encoder = CustomJSONEncoder
api = flask_restful.Api(blueprint)
app = Flask(__name__)
app.register_blueprint(blueprint, url_prefix='/blueprint')
```

or

```python
app = Flask(__name__)
app.json_encoder = CustomJSONEncoder
api = flask_restful.Api(app)
```
@juannorris juannorris changed the title Use flask.json as default encoder Use flask.json as default encoder Mar 17, 2020
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

1 participant