|
|
@ -271,30 +271,36 @@ class TestJSON(object): |
|
|
|
class X(object): |
|
|
|
class X(object): |
|
|
|
def __init__(self, val): |
|
|
|
def __init__(self, val): |
|
|
|
self.val = val |
|
|
|
self.val = val |
|
|
|
|
|
|
|
|
|
|
|
class MyEncoder(flask.json.JSONEncoder): |
|
|
|
class MyEncoder(flask.json.JSONEncoder): |
|
|
|
def default(self, o): |
|
|
|
def default(self, o): |
|
|
|
if isinstance(o, X): |
|
|
|
if isinstance(o, X): |
|
|
|
return '<%d>' % o.val |
|
|
|
return '<%d>' % o.val |
|
|
|
|
|
|
|
|
|
|
|
return flask.json.JSONEncoder.default(self, o) |
|
|
|
return flask.json.JSONEncoder.default(self, o) |
|
|
|
|
|
|
|
|
|
|
|
class MyDecoder(flask.json.JSONDecoder): |
|
|
|
class MyDecoder(flask.json.JSONDecoder): |
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
kwargs.setdefault('object_hook', self.object_hook) |
|
|
|
kwargs.setdefault('object_hook', self.object_hook) |
|
|
|
flask.json.JSONDecoder.__init__(self, *args, **kwargs) |
|
|
|
flask.json.JSONDecoder.__init__(self, *args, **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
def object_hook(self, obj): |
|
|
|
def object_hook(self, obj): |
|
|
|
if len(obj) == 1 and '_foo' in obj: |
|
|
|
if len(obj) == 1 and '_foo' in obj: |
|
|
|
return X(obj['_foo']) |
|
|
|
return X(obj['_foo']) |
|
|
|
|
|
|
|
|
|
|
|
return obj |
|
|
|
return obj |
|
|
|
|
|
|
|
|
|
|
|
blue = flask.Blueprint('blue', __name__) |
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
blue.json_encoder = MyEncoder |
|
|
|
bp.json_encoder = MyEncoder |
|
|
|
blue.json_decoder = MyDecoder |
|
|
|
bp.json_decoder = MyDecoder |
|
|
|
@blue.route('/bp', methods=['POST']) |
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/bp', methods=['POST']) |
|
|
|
def index(): |
|
|
|
def index(): |
|
|
|
return flask.json.dumps(flask.request.get_json()['x']) |
|
|
|
return flask.json.dumps(flask.request.get_json()['x']) |
|
|
|
|
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
app = flask.Flask(__name__) |
|
|
|
app.testing = True |
|
|
|
app.testing = True |
|
|
|
app.register_blueprint(blue) |
|
|
|
app.register_blueprint(bp) |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
c = app.test_client() |
|
|
|
rv = c.post('/bp', data=flask.json.dumps({ |
|
|
|
rv = c.post('/bp', data=flask.json.dumps({ |
|
|
|