0001"""
0002Implementation of JSONDecoder
0003"""
0004import re
0005
0006from simplejson.scanner import Scanner, pattern
0007
0008FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
0009
0010def _floatconstants():
0011    import struct
0012    import sys
0013    _BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
0014    if sys.byteorder != 'big':
0015        _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
0016    nan, inf = struct.unpack('dd', _BYTES)
0017    return nan, inf, -inf
0018
0019NaN, PosInf, NegInf = _floatconstants()
0020
0021def linecol(doc, pos):
0022    lineno = doc.count('\n', 0, pos) + 1
0023    if lineno == 1:
0024        colno = pos
0025    else:
0026        colno = pos - doc.rindex('\n', 0, pos)
0027    return lineno, colno
0028
0029def errmsg(msg, doc, pos, end=None):
0030    lineno, colno = linecol(doc, pos)
0031    if end is None:
0032        return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
0033    endlineno, endcolno = linecol(doc, end)
0034    return '%s: line %d column %d - line %d column %d (char %d - %d)' % (
0035        msg, lineno, colno, endlineno, endcolno, pos, end)
0036
0037def JSONInfinity(match, context):
0038    return PosInf, None
0039pattern('Infinity')(JSONInfinity)
0040
0041def JSONNegInfinity(match, context):
0042    return NegInf, None
0043pattern('-Infinity')(JSONNegInfinity)
0044
0045def JSONNaN(match, context):
0046    return NaN, None
0047pattern('NaN')(JSONNaN)
0048
0049def JSONTrue(match, context):
0050    return True, None
0051pattern('true')(JSONTrue)
0052
0053def JSONFalse(match, context):
0054    return False, None
0055pattern('false')(JSONFalse)
0056
0057def JSONNull(match, context):
0058    return None, None
0059pattern('null')(JSONNull)
0060
0061def JSONNumber(match, context):
0062    match = JSONNumber.regex.match(match.string, *match.span())
0063    integer, frac, exp = match.groups()
0064    if frac or exp:
0065        res = float(integer + (frac or '') + (exp or ''))
0066    else:
0067        res = int(integer)
0068    return res, None
0069pattern(r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?')(JSONNumber)
0070
0071STRINGCHUNK = re.compile(r'("|\\|[^"\\]+)', FLAGS)
0072STRINGBACKSLASH = re.compile(r'([\\/bfnrt"]|u[A-Fa-f0-9]{4})', FLAGS)
0073BACKSLASH = {
0074    '"': u'"', '\\': u'\\', '/': u'/',
0075    'b': u'\b', 'f': u'\f', 'n': u'\n', 'r': u'\r', 't': u'\t',
0076}
0077
0078DEFAULT_ENCODING = "utf-8"
0079
0080def scanstring(s, end, encoding=None):
0081    if encoding is None:
0082        encoding = DEFAULT_ENCODING
0083    chunks = []
0084    while 1:
0085        chunk = STRINGCHUNK.match(s, end)
0086        end = chunk.end()
0087        m = chunk.group(1)
0088        if m == '"':
0089            break
0090        if m == '\\':
0091            chunk = STRINGBACKSLASH.match(s, end)
0092            if chunk is None:
0093                raise ValueError(errmsg("Invalid \\escape", s, end))
0094            end = chunk.end()
0095            esc = chunk.group(1)
0096            try:
0097                m = BACKSLASH[esc]
0098            except KeyError:
0099                m = unichr(int(esc[1:], 16))
0100        if not isinstance(m, unicode):
0101            m = unicode(m, encoding)
0102        chunks.append(m)
0103    return u''.join(chunks), end
0104
0105def JSONString(match, context):
0106    encoding = getattr(context, 'encoding', None)
0107    return scanstring(match.string, match.end(), encoding)
0108pattern(r'"')(JSONString)
0109
0110WHITESPACE = re.compile(r'\s+', FLAGS)
0111
0112def skipwhitespace(s, end):
0113    m = WHITESPACE.match(s, end)
0114    if m is not None:
0115        return m.end()
0116    return end
0117
0118def JSONObject(match, context):
0119    pairs = {}
0120    s = match.string
0121    end = skipwhitespace(s, match.end())
0122    nextchar = s[end:end + 1]
0123    # trivial empty object
0124    if nextchar == '}':
0125        return pairs, end + 1
0126    if nextchar != '"':
0127        raise ValueError(errmsg("Expecting property name", s, end))
0128    end += 1
0129    encoding = getattr(context, 'encoding', None)
0130    while True:
0131        key, end = scanstring(s, end, encoding)
0132        end = skipwhitespace(s, end)
0133        if s[end:end + 1] != ':':
0134            raise ValueError(errmsg("Expecting : delimiter", s, end))
0135        end = skipwhitespace(s, end + 1)
0136        try:
0137            value, end = JSONScanner.iterscan(s, idx=end).next()
0138        except StopIteration:
0139            raise ValueError(errmsg("Expecting object", s, end))
0140        pairs[key] = value
0141        end = skipwhitespace(s, end)
0142        nextchar = s[end:end + 1]
0143        end += 1
0144        if nextchar == '}':
0145            break
0146        if nextchar != ',':
0147            raise ValueError(errmsg("Expecting , delimiter", s, end - 1))
0148        end = skipwhitespace(s, end)
0149        nextchar = s[end:end + 1]
0150        end += 1
0151        if nextchar != '"':
0152            raise ValueError(errmsg("Expecting property name", s, end - 1))
0153    return pairs, end
0154pattern(r'{')(JSONObject)
0155
0156def JSONArray(match, context):
0157    values = []
0158    s = match.string
0159    end = skipwhitespace(s, match.end())
0160    # look-ahead for trivial empty array
0161    nextchar = s[end:end + 1]
0162    if nextchar == ']':
0163        return values, end + 1
0164    while True:
0165        try:
0166            value, end = JSONScanner.iterscan(s, idx=end).next()
0167        except StopIteration:
0168            raise ValueError(errmsg("Expecting object", s, end))
0169        values.append(value)
0170        end = skipwhitespace(s, end)
0171        nextchar = s[end:end + 1]
0172        end += 1
0173        if nextchar == ']':
0174            break
0175        if nextchar != ',':
0176            raise ValueError(errmsg("Expecting , delimiter", s, end))
0177        end = skipwhitespace(s, end)
0178    return values, end
0179pattern(r'\[')(JSONArray)
0180
0181ANYTHING = [
0182    JSONTrue,
0183    JSONFalse,
0184    JSONNull,
0185    JSONNaN,
0186    JSONInfinity,
0187    JSONNegInfinity,
0188    JSONNumber,
0189    JSONString,
0190    JSONArray,
0191    JSONObject,
0192]
0193
0194JSONScanner = Scanner(ANYTHING)
0195
0196class JSONDecoder(object):
0197    """
0198    Simple JSON <http://json.org> decoder
0199
0200    Performs the following translations in decoding:
0201    
0202    +---------------+-------------------+
0203    | JSON          | Python            |
0204    +===============+===================+
0205    | object        | dict              |
0206    +---------------+-------------------+
0207    | array         | list              |
0208    +---------------+-------------------+
0209    | string        | unicode           |
0210    +---------------+-------------------+
0211    | number (int)  | int, long         |
0212    +---------------+-------------------+
0213    | number (real) | float             |
0214    +---------------+-------------------+
0215    | true          | True              |
0216    +---------------+-------------------+
0217    | false         | False             |
0218    +---------------+-------------------+
0219    | null          | None              |
0220    +---------------+-------------------+
0221
0222    It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as
0223    their corresponding ``float`` values, which is outside the JSON spec.
0224    """
0225
0226    _scanner = Scanner(ANYTHING)
0227    __all__ = ['__init__', 'decode', 'raw_decode']
0228
0229    def __init__(self, encoding=None):
0230        """
0231        ``encoding`` determines the encoding used to interpret any ``str``
0232        objects decoded by this instance (utf-8 by default).  It has no
0233        effect when decoding ``unicode`` objects.
0234        
0235        Note that currently only encodings that are a superset of ASCII work,
0236        strings of other encodings should be passed in as ``unicode``.
0237        """
0238        self.encoding = encoding
0239
0240    def decode(self, s):
0241        """
0242        Return the Python representation of ``s`` (a ``str`` or ``unicode``
0243        instance containing a JSON document)
0244        """
0245        obj, end = self.raw_decode(s, idx=skipwhitespace(s, 0))
0246        end = skipwhitespace(s, end)
0247        if end != len(s):
0248            raise ValueError(errmsg("Extra data", s, end, len(s)))
0249        return obj
0250
0251    def raw_decode(self, s, **kw):
0252        """
0253        Decode a JSON document from ``s`` (a ``str`` or ``unicode`` beginning
0254        with a JSON document) and return a 2-tuple of the Python
0255        representation and the index in ``s`` where the document ended.
0256
0257        This can be used to decode a JSON document from a string that may
0258        have extraneous data at the end.
0259        """
0260        kw.setdefault('context', self)
0261        try:
0262            obj, end = self._scanner.iterscan(s, **kw).next()
0263        except StopIteration:
0264            raise ValueError("No JSON object could be decoded")
0265        return obj, end
0266
0267__all__ = ['JSONDecoder']