24
24
# POSSIBILITY OF SUCH DAMAGE.
25
25
26
26
27
- __all__ = ("BSONObjectIdConverter" , "JSONEncoder " )
27
+ __all__ = ("BSONObjectIdConverter" , "BSONProvider " )
28
28
29
- from bson import json_util , SON
29
+ from bson import json_util
30
30
from bson .errors import InvalidId
31
31
from 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
34
34
from werkzeug .routing import BaseConverter
35
35
import 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
42
37
43
38
44
39
def _iteritems (obj ):
@@ -83,7 +78,7 @@ def to_url(self, value):
83
78
return str (value )
84
79
85
80
86
- class JSONEncoder ( flask_json . JSONEncoder ):
81
+ class BSONProvider ( JSONProvider ):
87
82
88
83
"""A JSON encoder that uses :mod:`bson.json_util` for MongoDB documents.
89
84
@@ -101,54 +96,23 @@ def json_route(cart_id):
101
96
differently than you expect. See :class:`~bson.json_util.JSONOptions`
102
97
for details on the particular serialization that will be used.
103
98
104
- A :class:`~flask_pymongo.helpers.JSONEncoder ` is automatically
99
+ A :class:`~flask_pymongo.helpers.JSONProvider ` is automatically
105
100
automatically installed on the :class:`~flask_pymongo.PyMongo`
106
101
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`.
121
103
"""
122
104
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 }
130
107
131
- super (JSONEncoder , self ).__init__ (* args , ** kwargs )
108
+ super ().__init__ (app )
132
109
133
- def default (self , obj ):
110
+ def dumps (self , obj ):
134
111
"""Serialize MongoDB object types using :mod:`bson.json_util`.
112
+ """
113
+ return json_util .dumps (obj )
135
114
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`.
142
117
"""
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