| 1 |
""" |
| 2 |
Wrapper for the builtin json module that ensures compliance with the JSON spec. |
| 3 |
|
| 4 |
REST framework should always import this wrapper module in order to maintain |
| 5 |
spec-compliant encoding/decoding. Support for non-standard features should be |
| 6 |
handled by users at the renderer and parser layer. |
| 7 |
""" |
| 8 |
import functools |
| 9 |
import json # noqa |
| 10 |
|
| 11 |
|
| 12 |
def strict_constant(o): |
| 13 |
raise ValueError('Out of range float values are not JSON compliant: ' + repr(o)) |
| 14 |
|
| 15 |
|
| 16 |
@functools.wraps(json.dump) |
| 17 |
def dump(*args, **kwargs): |
| 18 |
kwargs.setdefault('allow_nan', False) |
| 19 |
return json.dump(*args, **kwargs) |
| 20 |
|
| 21 |
|
| 22 |
@functools.wraps(json.dumps) |
| 23 |
def dumps(*args, **kwargs): |
| 24 |
kwargs.setdefault('allow_nan', False) |
| 25 |
return json.dumps(*args, **kwargs) |
| 26 |
|
| 27 |
|
| 28 |
@functools.wraps(json.load) |
| 29 |
def load(*args, **kwargs): |
| 30 |
kwargs.setdefault('parse_constant', strict_constant) |
| 31 |
return json.load(*args, **kwargs) |
| 32 |
|
| 33 |
|
| 34 |
@functools.wraps(json.loads) |
| 35 |
def loads(*args, **kwargs): |
| 36 |
kwargs.setdefault('parse_constant', strict_constant) |
| 37 |
return json.loads(*args, **kwargs) |