2424# POSSIBILITY OF SUCH DAMAGE.
2525
2626
27- __all__ = ("BSONObjectIdConverter" , "JSONEncoder " )
27+ __all__ = ("BSONObjectIdConverter" , "BSONProvider " )
2828
29- from bson import json_util , SON
29+ from bson import json_util
3030from bson .errors import InvalidId
3131from bson .objectid import ObjectId
32- from flask import abort , json as flask_json
33- from six import iteritems , string_types
32+ from flask import abort
33+ from flask . json . provider import JSONProvider
3434from werkzeug .routing import BaseConverter
3535import pymongo
36-
37- if pymongo .version_tuple >= (3 , 5 , 0 ):
38- from bson .json_util import RELAXED_JSON_OPTIONS
39- DEFAULT_JSON_OPTIONS = RELAXED_JSON_OPTIONS
40- else :
41- DEFAULT_JSON_OPTIONS = None
36+ from bson .json_util import RELAXED_JSON_OPTIONS
4237
4338
4439def _iteritems (obj ):
@@ -83,7 +78,7 @@ def to_url(self, value):
8378 return str (value )
8479
8580
86- class JSONEncoder ( flask_json . JSONEncoder ):
81+ class BSONProvider ( JSONProvider ):
8782
8883 """A JSON encoder that uses :mod:`bson.json_util` for MongoDB documents.
8984
@@ -101,54 +96,23 @@ def json_route(cart_id):
10196 differently than you expect. See :class:`~bson.json_util.JSONOptions`
10297 for details on the particular serialization that will be used.
10398
104- A :class:`~flask_pymongo.helpers.JSONEncoder ` is automatically
99+ A :class:`~flask_pymongo.helpers.JSONProvider ` is automatically
105100 automatically installed on the :class:`~flask_pymongo.PyMongo`
106101 instance at creation time, using
107- :const:`~bson.json_util.RELAXED_JSON_OPTIONS`. You can change the
108- :class:`~bson.json_util.JSONOptions` in use by passing
109- ``json_options`` to the :class:`~flask_pymongo.PyMongo`
110- constructor.
111-
112- .. note::
113-
114- :class:`~bson.json_util.JSONOptions` is only supported as of
115- PyMongo version 3.4. For older versions of PyMongo, you will
116- have less control over the JSON format that results from calls
117- to :func:`~flask.json.jsonify`.
118-
119- .. versionadded:: 2.4.0
120-
102+ :const:`~bson.json_util.RELAXED_JSON_OPTIONS`.
121103 """
122104
123- def __init__ (self , json_options , * args , ** kwargs ):
124- if json_options is None :
125- json_options = DEFAULT_JSON_OPTIONS
126- if json_options is not None :
127- self ._default_kwargs = {"json_options" : json_options }
128- else :
129- self ._default_kwargs = {}
105+ def __init__ (self , app ):
106+ self ._default_kwargs = {"json_options" : RELAXED_JSON_OPTIONS }
130107
131- super (JSONEncoder , self ).__init__ (* args , ** kwargs )
108+ super ().__init__ (app )
132109
133- def default (self , obj ):
110+ def dumps (self , obj ):
134111 """Serialize MongoDB object types using :mod:`bson.json_util`.
112+ """
113+ return json_util .dumps (obj )
135114
136- Falls back to Flask's default JSON serialization for all other types.
137-
138- This may raise ``TypeError`` for object types not recognized.
139-
140- .. versionadded:: 2.4.0
141-
115+ def loads (self , str_obj ):
116+ """Deserialize MongoDB object types using :mod:`bson.json_util`.
142117 """
143- if hasattr (obj , "iteritems" ) or hasattr (obj , "items" ):
144- return SON ((k , self .default (v )) for k , v in iteritems (obj ))
145- elif hasattr (obj , "__iter__" ) and not isinstance (obj , string_types ):
146- return [self .default (v ) for v in obj ]
147- else :
148- try :
149- return json_util .default (obj , ** self ._default_kwargs )
150- except TypeError :
151- # PyMongo couldn't convert into a serializable object, and
152- # the Flask default JSONEncoder won't; so we return the
153- # object itself and let stdlib json handle it if possible
154- return obj
118+ return json_util .loads (str_obj )
0 commit comments