summaryrefslogtreecommitdiff
path: root/simplejson/decoder.py
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2008-03-23 07:59:48 +0000
committerBob Ippolito <bob@redivi.com>2008-03-23 07:59:48 +0000
commitf3d503d201501aa2fe7c0914ce6a3978023d6223 (patch)
tree548ec8edfab5cfc94526f6c7b0110df3ca96769a /simplejson/decoder.py
parentddaa1c328be014f4ab3d1e9e7dc3b2484a0fd2d7 (diff)
downloadsimplejson-f3d503d201501aa2fe7c0914ce6a3978023d6223.tar.gz
parse_float, parse_int, parse_constant hooks
git-svn-id: http://simplejson.googlecode.com/svn/trunk@67 a4795897-2c25-0410-b006-0d3caba88fa1
Diffstat (limited to 'simplejson/decoder.py')
-rw-r--r--simplejson/decoder.py37
1 files changed, 32 insertions, 5 deletions
diff --git a/simplejson/decoder.py b/simplejson/decoder.py
index a1b53b2..162483c 100644
--- a/simplejson/decoder.py
+++ b/simplejson/decoder.py
@@ -44,16 +44,24 @@ _CONSTANTS = {
}
def JSONConstant(match, context, c=_CONSTANTS):
- return c[match.group(0)], None
+ s = match.group(0)
+ fn = getattr(context, 'parse_constant', None)
+ if fn is None:
+ rval = c[s]
+ else:
+ rval = fn(s)
+ return rval, None
pattern('(-?Infinity|NaN|true|false|null)')(JSONConstant)
def JSONNumber(match, context):
match = JSONNumber.regex.match(match.string, *match.span())
integer, frac, exp = match.groups()
if frac or exp:
- res = float(integer + (frac or '') + (exp or ''))
+ fn = getattr(context, 'parse_float', None) or float
+ res = fn(integer + (frac or '') + (exp or ''))
else:
- res = int(integer)
+ fn = getattr(context, 'parse_int', None) or int
+ res = fn(integer)
return res, None
pattern(r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?')(JSONNumber)
@@ -197,7 +205,7 @@ class JSONDecoder(object):
"""
Simple JSON <http://json.org> decoder
- Performs the following translations in decoding:
+ Performs the following translations in decoding by default:
+---------------+-------------------+
| JSON | Python |
@@ -226,7 +234,8 @@ class JSONDecoder(object):
_scanner = Scanner(ANYTHING)
__all__ = ['__init__', 'decode', 'raw_decode']
- def __init__(self, encoding=None, object_hook=None):
+ def __init__(self, encoding=None, object_hook=None, parse_float=None,
+ parse_int=None, parse_constant=None):
"""
``encoding`` determines the encoding used to interpret any ``str``
objects decoded by this instance (utf-8 by default). It has no
@@ -239,9 +248,27 @@ class JSONDecoder(object):
of every JSON object decoded and its return value will be used in
place of the given ``dict``. This can be used to provide custom
deserializations (e.g. to support JSON-RPC class hinting).
+
+ ``parse_float``, if specified, will be called with the string
+ of every JSON float to be decoded. By default this is equivalent to
+ float(num_str). This can be used to use another datatype or parser
+ for JSON floats (e.g. decimal.Decimal).
+
+ ``parse_int``, if specified, will be called with the string
+ of every JSON int to be decoded. By default this is equivalent to
+ int(num_str). This can be used to use another datatype or parser
+ for JSON integers (e.g. float).
+
+ ``parse_constant``, if specified, will be called with one of the
+ following strings: -Infinity, Infinity, NaN, null, true, false.
+ This can be used to raise an exception if invalid JSON numbers
+ are encountered.
"""
self.encoding = encoding
self.object_hook = object_hook
+ self.parse_float = parse_float
+ self.parse_int = parse_int
+ self.parse_constant = parse_constant
def decode(self, s, _w=WHITESPACE.match):
"""