forked from orbit-oss/flask
parent
74b3f7e04c
commit
46f83665ef
4 changed files with 30 additions and 17 deletions
|
|
@ -271,30 +271,36 @@ class TestJSON(object):
|
|||
class X(object):
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
|
||||
class MyEncoder(flask.json.JSONEncoder):
|
||||
def default(self, o):
|
||||
if isinstance(o, X):
|
||||
return '<%d>' % o.val
|
||||
|
||||
return flask.json.JSONEncoder.default(self, o)
|
||||
|
||||
class MyDecoder(flask.json.JSONDecoder):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs.setdefault('object_hook', self.object_hook)
|
||||
flask.json.JSONDecoder.__init__(self, *args, **kwargs)
|
||||
|
||||
def object_hook(self, obj):
|
||||
if len(obj) == 1 and '_foo' in obj:
|
||||
return X(obj['_foo'])
|
||||
|
||||
return obj
|
||||
|
||||
blue = flask.Blueprint('blue', __name__)
|
||||
blue.json_encoder = MyEncoder
|
||||
blue.json_decoder = MyDecoder
|
||||
@blue.route('/bp', methods=['POST'])
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
bp.json_encoder = MyEncoder
|
||||
bp.json_decoder = MyDecoder
|
||||
|
||||
@bp.route('/bp', methods=['POST'])
|
||||
def index():
|
||||
return flask.json.dumps(flask.request.get_json()['x'])
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.register_blueprint(blue)
|
||||
app.register_blueprint(bp)
|
||||
|
||||
c = app.test_client()
|
||||
rv = c.post('/bp', data=flask.json.dumps({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue