app/simplejson/decoder.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 30 Jan 2009 22:01:27 +0000
changeset 1095 0122dc66e5d2
parent 975 295d67509412
permissions -rw-r--r--
Add access control to document model and view The access checks are not yet written, but at least the model is stable from now on. Also converted the document view to dynaform while at it. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
975
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
"""Implementation of JSONDecoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
"""
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
import re
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
import sys
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
import struct
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
from simplejson.scanner import make_scanner
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
    from simplejson._speedups import scanstring as c_scanstring
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
except ImportError:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
    c_scanstring = None
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
__all__ = ['JSONDecoder']
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
def _floatconstants():
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
    _BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
    if sys.byteorder != 'big':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
        _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
    nan, inf = struct.unpack('dd', _BYTES)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
    return nan, inf, -inf
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
NaN, PosInf, NegInf = _floatconstants()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
def linecol(doc, pos):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
    lineno = doc.count('\n', 0, pos) + 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
    if lineno == 1:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
        colno = pos
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
    else:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
        colno = pos - doc.rindex('\n', 0, pos)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
    return lineno, colno
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
def errmsg(msg, doc, pos, end=None):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
    # Note that this function is called from _speedups
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
    lineno, colno = linecol(doc, pos)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    39
    if end is None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    40
        return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
    endlineno, endcolno = linecol(doc, end)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    42
    return '%s: line %d column %d - line %d column %d (char %d - %d)' % (
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    43
        msg, lineno, colno, endlineno, endcolno, pos, end)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    44
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    45
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
_CONSTANTS = {
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
    '-Infinity': NegInf,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
    'Infinity': PosInf,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    49
    'NaN': NaN,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    50
}
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    52
STRINGCHUNK = re.compile(r'(.*?)(["\\\x00-\x1f])', FLAGS)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    53
BACKSLASH = {
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    54
    '"': u'"', '\\': u'\\', '/': u'/',
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    55
    'b': u'\b', 'f': u'\f', 'n': u'\n', 'r': u'\r', 't': u'\t',
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    56
}
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    57
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    58
DEFAULT_ENCODING = "utf-8"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
def py_scanstring(s, end, encoding=None, strict=True, _b=BACKSLASH, _m=STRINGCHUNK.match):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
    """Scan the string s for a JSON string. End is the index of the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
    character in s after the quote that started the JSON string.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
    Unescapes all valid JSON string escape sequences and raises ValueError
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
    on attempt to decode an invalid string. If strict is False then literal
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    65
    control characters are allowed in the string.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    66
    
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
    Returns a tuple of the decoded string and the index of the character in s
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    68
    after the end quote."""
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    69
    if encoding is None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    70
        encoding = DEFAULT_ENCODING
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    71
    chunks = []
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
    _append = chunks.append
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    73
    begin = end - 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    74
    while 1:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    75
        chunk = _m(s, end)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    76
        if chunk is None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    77
            raise ValueError(
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    78
                errmsg("Unterminated string starting at", s, begin))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    79
        end = chunk.end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    80
        content, terminator = chunk.groups()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    81
        # Content is contains zero or more unescaped string characters
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    82
        if content:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    83
            if not isinstance(content, unicode):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    84
                content = unicode(content, encoding)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    85
            _append(content)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    86
        # Terminator is the end of string, a literal control character,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    87
        # or a backslash denoting that an escape sequence follows
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    88
        if terminator == '"':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    89
            break
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    90
        elif terminator != '\\':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    91
            if strict:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    92
                msg = "Invalid control character %r at" % (terminator,)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    93
                raise ValueError(msg, s, end)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    94
            else:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    95
                _append(terminator)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    96
                continue
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    97
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    98
            esc = s[end]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    99
        except IndexError:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   100
            raise ValueError(
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   101
                errmsg("Unterminated string starting at", s, begin))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   102
        # If not a unicode escape sequence, must be in the lookup table
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   103
        if esc != 'u':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   104
            try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   105
                char = _b[esc]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   106
            except KeyError:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   107
                raise ValueError(
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   108
                    errmsg("Invalid \\escape: %r" % (esc,), s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   109
            end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   110
        else:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   111
            # Unicode escape sequence
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   112
            esc = s[end + 1:end + 5]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   113
            next_end = end + 5
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   114
            if len(esc) != 4:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   115
                msg = "Invalid \\uXXXX escape"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   116
                raise ValueError(errmsg(msg, s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   117
            uni = int(esc, 16)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   118
            # Check for surrogate pair on UCS-4 systems
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   119
            if 0xd800 <= uni <= 0xdbff and sys.maxunicode > 65535:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   120
                msg = "Invalid \\uXXXX\\uXXXX surrogate pair"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   121
                if not s[end + 5:end + 7] == '\\u':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   122
                    raise ValueError(errmsg(msg, s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   123
                esc2 = s[end + 7:end + 11]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   124
                if len(esc2) != 4:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   125
                    raise ValueError(errmsg(msg, s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   126
                uni2 = int(esc2, 16)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   127
                uni = 0x10000 + (((uni - 0xd800) << 10) | (uni2 - 0xdc00))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   128
                next_end += 6
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   129
            char = unichr(uni)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   130
            end = next_end
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   131
        # Append the unescaped character
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   132
        _append(char)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   133
    return u''.join(chunks), end
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   134
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   135
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   136
# Use speedup if available
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   137
scanstring = c_scanstring or py_scanstring
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   138
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   139
WHITESPACE = re.compile(r'[ \t\n\r]*', FLAGS)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   140
WHITESPACE_STR = ' \t\n\r'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   141
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   142
def JSONObject((s, end), encoding, strict, scan_once, object_hook, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   143
    pairs = {}
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   144
    # Use a slice to prevent IndexError from being raised, the following
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   145
    # check will raise a more specific ValueError if the string is empty
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   146
    nextchar = s[end:end + 1]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   147
    # Normally we expect nextchar == '"'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   148
    if nextchar != '"':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   149
        if nextchar in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   150
            end = _w(s, end).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   151
            nextchar = s[end:end + 1]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   152
        # Trivial empty object
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   153
        if nextchar == '}':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   154
            return pairs, end + 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   155
        elif nextchar != '"':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   156
            raise ValueError(errmsg("Expecting property name", s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   157
    end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   158
    while True:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   159
        key, end = scanstring(s, end, encoding, strict)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   160
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   161
        # To skip some function call overhead we optimize the fast paths where
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   162
        # the JSON key separator is ": " or just ":".
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   163
        if s[end:end + 1] != ':':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   164
            end = _w(s, end).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   165
            if s[end:end + 1] != ':':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   166
                raise ValueError(errmsg("Expecting : delimiter", s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   167
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   168
        end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   169
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   170
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   171
            if s[end] in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   172
                end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   173
                if s[end] in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   174
                    end = _w(s, end + 1).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   175
        except IndexError:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   176
            pass
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   177
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   178
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   179
            value, end = scan_once(s, end)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   180
        except StopIteration:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   181
            raise ValueError(errmsg("Expecting object", s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   182
        pairs[key] = value
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   183
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   184
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   185
            nextchar = s[end]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   186
            if nextchar in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   187
                end = _w(s, end + 1).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   188
                nextchar = s[end]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   189
        except IndexError:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   190
            nextchar = ''
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   191
        end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   192
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   193
        if nextchar == '}':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   194
            break
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   195
        elif nextchar != ',':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   196
            raise ValueError(errmsg("Expecting , delimiter", s, end - 1))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   197
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   198
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   199
            nextchar = s[end]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   200
            if nextchar in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   201
                end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   202
                nextchar = s[end]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   203
                if nextchar in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   204
                    end = _w(s, end + 1).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   205
                    nextchar = s[end]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   206
        except IndexError:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   207
            nextchar = ''
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   208
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   209
        end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   210
        if nextchar != '"':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   211
            raise ValueError(errmsg("Expecting property name", s, end - 1))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   212
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   213
    if object_hook is not None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   214
        pairs = object_hook(pairs)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   215
    return pairs, end
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   216
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   217
def JSONArray((s, end), scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   218
    values = []
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   219
    nextchar = s[end:end + 1]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   220
    if nextchar in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   221
        end = _w(s, end + 1).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   222
        nextchar = s[end:end + 1]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   223
    # Look-ahead for trivial empty array
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   224
    if nextchar == ']':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   225
        return values, end + 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   226
    _append = values.append
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   227
    while True:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   228
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   229
            value, end = scan_once(s, end)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   230
        except StopIteration:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   231
            raise ValueError(errmsg("Expecting object", s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   232
        _append(value)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   233
        nextchar = s[end:end + 1]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   234
        if nextchar in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   235
            end = _w(s, end + 1).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   236
            nextchar = s[end:end + 1]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   237
        end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   238
        if nextchar == ']':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   239
            break
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   240
        elif nextchar != ',':
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   241
            raise ValueError(errmsg("Expecting , delimiter", s, end))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   242
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   243
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   244
            if s[end] in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   245
                end += 1
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   246
                if s[end] in _ws:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   247
                    end = _w(s, end + 1).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   248
        except IndexError:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   249
            pass
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   250
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   251
    return values, end
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   252
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   253
class JSONDecoder(object):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   254
    """Simple JSON <http://json.org> decoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   255
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   256
    Performs the following translations in decoding by default:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   257
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   258
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   259
    | JSON          | Python            |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   260
    +===============+===================+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   261
    | object        | dict              |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   262
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   263
    | array         | list              |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   264
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   265
    | string        | unicode           |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   266
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   267
    | number (int)  | int, long         |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   268
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   269
    | number (real) | float             |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   270
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   271
    | true          | True              |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   272
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   273
    | false         | False             |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   274
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   275
    | null          | None              |
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   276
    +---------------+-------------------+
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   277
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   278
    It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   279
    their corresponding ``float`` values, which is outside the JSON spec.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   280
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   281
    """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   282
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   283
    def __init__(self, encoding=None, object_hook=None, parse_float=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   284
            parse_int=None, parse_constant=None, strict=True):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   285
        """``encoding`` determines the encoding used to interpret any ``str``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   286
        objects decoded by this instance (utf-8 by default).  It has no
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   287
        effect when decoding ``unicode`` objects.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   288
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   289
        Note that currently only encodings that are a superset of ASCII work,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   290
        strings of other encodings should be passed in as ``unicode``.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   291
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   292
        ``object_hook``, if specified, will be called with the result
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   293
        of every JSON object decoded and its return value will be used in
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   294
        place of the given ``dict``.  This can be used to provide custom
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   295
        deserializations (e.g. to support JSON-RPC class hinting).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   296
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   297
        ``parse_float``, if specified, will be called with the string
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   298
        of every JSON float to be decoded. By default this is equivalent to
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   299
        float(num_str). This can be used to use another datatype or parser
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   300
        for JSON floats (e.g. decimal.Decimal).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   301
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   302
        ``parse_int``, if specified, will be called with the string
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   303
        of every JSON int to be decoded. By default this is equivalent to
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   304
        int(num_str). This can be used to use another datatype or parser
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   305
        for JSON integers (e.g. float).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   306
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   307
        ``parse_constant``, if specified, will be called with one of the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   308
        following strings: -Infinity, Infinity, NaN.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   309
        This can be used to raise an exception if invalid JSON numbers
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   310
        are encountered.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   311
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   312
        """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   313
        self.encoding = encoding
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   314
        self.object_hook = object_hook
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   315
        self.parse_float = parse_float or float
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   316
        self.parse_int = parse_int or int
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   317
        self.parse_constant = parse_constant or _CONSTANTS.__getitem__
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   318
        self.strict = strict
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   319
        self.parse_object = JSONObject
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   320
        self.parse_array = JSONArray
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   321
        self.parse_string = scanstring
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   322
        self.scan_once = make_scanner(self)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   323
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   324
    def decode(self, s, _w=WHITESPACE.match):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   325
        """Return the Python representation of ``s`` (a ``str`` or ``unicode``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   326
        instance containing a JSON document)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   327
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   328
        """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   329
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   330
        end = _w(s, end).end()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   331
        if end != len(s):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   332
            raise ValueError(errmsg("Extra data", s, end, len(s)))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   333
        return obj
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   334
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   335
    def raw_decode(self, s, idx=0):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   336
        """Decode a JSON document from ``s`` (a ``str`` or ``unicode`` beginning
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   337
        with a JSON document) and return a 2-tuple of the Python
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   338
        representation and the index in ``s`` where the document ended.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   339
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   340
        This can be used to decode a JSON document from a string that may
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   341
        have extraneous data at the end.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   342
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   343
        """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   344
        try:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   345
            obj, end = self.scan_once(s, idx)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   346
        except StopIteration:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   347
            raise ValueError("No JSON object could be decoded")
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   348
        return obj, end