<s> """<STR_LIT>""" <EOL> import re <EOL> import sys <EOL> import struct <EOL> from simplejson . scanner import make_scanner <EOL> try : <EOL> from simplejson . _speedups import scanstring as c_scanstring <EOL> except ImportError : <EOL> c_scanstring = None <EOL> __all__ = [ '<STR_LIT>' ] <EOL> FLAGS = re . VERBOSE | re . MULTILINE | re . DOTALL <EOL> def _floatconstants ( ) : <EOL> _BYTES = '<STR_LIT>' . decode ( '<STR_LIT>' ) <EOL> if sys . byteorder != '<STR_LIT>' : <EOL> _BYTES = _BYTES [ : <NUM_LIT:8> ] [ : : - <NUM_LIT:1> ] + _BYTES [ <NUM_LIT:8> : ] [ : : - <NUM_LIT:1> ] <EOL> nan , inf = struct . unpack ( '<STR_LIT>' , _BYTES ) <EOL> return nan , inf , - inf <EOL> NaN , PosInf , NegInf = _floatconstants ( ) <EOL> def linecol ( doc , pos ) : <EOL> lineno = doc . count ( '<STR_LIT:\n>' , <NUM_LIT:0> , pos ) + <NUM_LIT:1> <EOL> if lineno == <NUM_LIT:1> : <EOL> colno = pos <EOL> else : <EOL> colno = pos - doc . rindex ( '<STR_LIT:\n>' , <NUM_LIT:0> , pos ) <EOL> return lineno , colno <EOL> def errmsg ( msg , doc , pos , end = None ) : <EOL> lineno , colno = linecol ( doc , pos ) <EOL> if end is None : <EOL> fmt = '<STR_LIT>' <EOL> return fmt % ( msg , lineno , colno , pos ) <EOL> endlineno , endcolno = linecol ( doc , end ) <EOL> fmt = '<STR_LIT>' <EOL> return fmt % ( msg , lineno , colno , endlineno , endcolno , pos , end ) <EOL> _CONSTANTS = { <EOL> '<STR_LIT>' : NegInf , <EOL> '<STR_LIT>' : PosInf , <EOL> '<STR_LIT>' : NaN , <EOL> } <EOL> STRINGCHUNK = re . compile ( r'<STR_LIT>' , FLAGS ) <EOL> BACKSLASH = { <EOL> '<STR_LIT:">' : u'<STR_LIT:">' , '<STR_LIT:\\>' : u'<STR_LIT:\\>' , '<STR_LIT:/>' : u'<STR_LIT:/>' , <EOL> '<STR_LIT:b>' : u'<STR_LIT>' , '<STR_LIT:f>' : u'<STR_LIT>' , '<STR_LIT:n>' : u'<STR_LIT:\n>' , '<STR_LIT:r>' : u'<STR_LIT:\r>' , '<STR_LIT:t>' : u'<STR_LIT:\t>' , <EOL> } <EOL> DEFAULT_ENCODING = "<STR_LIT:utf-8>" <EOL> def py_scanstring ( s , end , encoding = None , strict = True , _b = BACKSLASH , _m = STRINGCHUNK . match ) : <EOL> """<STR_LIT>""" <EOL> if encoding is None : <EOL> encoding = DEFAULT_ENCODING <EOL> chunks = [ ] <EOL> _append = chunks . append <EOL> begin = end - <NUM_LIT:1> <EOL> while <NUM_LIT:1> : <EOL> chunk = _m ( s , end ) <EOL> if chunk is None : <EOL> raise ValueError ( <EOL> errmsg ( "<STR_LIT>" , s , begin ) ) <EOL> end = chunk . end ( ) <EOL> content , terminator = chunk . groups ( ) <EOL> if content : <EOL> if not isinstance ( content , unicode ) : <EOL> content = unicode ( content , encoding ) <EOL> _append ( content ) <EOL> if terminator == '<STR_LIT:">' : <EOL> break <EOL> elif terminator != '<STR_LIT:\\>' : <EOL> if strict : <EOL> msg = "<STR_LIT>" % ( terminator , ) <EOL> raise ValueError ( errmsg ( msg , s , end ) ) <EOL> else : <EOL> _append ( terminator ) <EOL> continue <EOL> try : <EOL> esc = s [ end ] <EOL> except IndexError : <EOL> raise ValueError ( <EOL> errmsg ( "<STR_LIT>" , s , begin ) ) <EOL> if esc != '<STR_LIT:u>' : <EOL> try : <EOL> char = _b [ esc ] <EOL> except KeyError : <EOL> msg = "<STR_LIT>" + repr ( esc ) <EOL> raise ValueError ( errmsg ( msg , s , end ) ) <EOL> end += <NUM_LIT:1> <EOL> else : <EOL> esc = s [ end + <NUM_LIT:1> : end + <NUM_LIT:5> ] <EOL> next_end = end + <NUM_LIT:5> <EOL> if len ( esc ) != <NUM_LIT:4> : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( errmsg ( msg , s , end ) ) <EOL> uni = int ( esc , <NUM_LIT:16> ) <EOL> if <NUM_LIT> <= uni <= <NUM_LIT> and sys . maxunicode > <NUM_LIT> : <EOL> msg = "<STR_LIT>" <EOL> if not s [ end + <NUM_LIT:5> : end + <NUM_LIT:7> ] == '<STR_LIT>' : <EOL> raise ValueError ( errmsg ( msg , s , end ) ) <EOL> esc2 = s [ end + <NUM_LIT:7> : end + <NUM_LIT:11> ] <EOL> if len ( esc2 ) != <NUM_LIT:4> : <EOL> raise ValueError ( errmsg ( msg , s , end ) ) <EOL> uni2 = int ( esc2 , <NUM_LIT:16> ) <EOL> uni = <NUM_LIT> + ( ( ( uni - <NUM_LIT> ) << <NUM_LIT:10> ) | ( uni2 - <NUM_LIT> ) ) <EOL> next_end += <NUM_LIT:6> <EOL> char = unichr ( uni ) <EOL> end = next_end <EOL> _append ( char ) <EOL> return u'<STR_LIT>' . join ( chunks ) , end <EOL> scanstring = c_scanstring or py_scanstring <EOL> WHITESPACE = re . compile ( r'<STR_LIT>' , FLAGS ) <EOL> WHITESPACE_STR = '<STR_LIT>' <EOL> def JSONObject ( ( s , end ) , encoding , strict , scan_once , object_hook , _w = WHITESPACE . match , _ws = WHITESPACE_STR ) : <EOL> pairs = { } <EOL> nextchar = s [ end : end + <NUM_LIT:1> ] <EOL> if nextchar != '<STR_LIT:">' : <EOL> if nextchar in _ws : <EOL> end = _w ( s , end ) . end ( ) <EOL> nextchar = s [ end : end + <NUM_LIT:1> ] <EOL> if nextchar == '<STR_LIT:}>' : <EOL> return pairs , end + <NUM_LIT:1> <EOL> elif nextchar != '<STR_LIT:">' : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end ) ) <EOL> end += <NUM_LIT:1> <EOL> while True : <EOL> key , end = scanstring ( s , end , encoding , strict ) <EOL> if s [ end : end + <NUM_LIT:1> ] != '<STR_LIT::>' : <EOL> end = _w ( s , end ) . end ( ) <EOL> if s [ end : end + <NUM_LIT:1> ] != '<STR_LIT::>' : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end ) ) <EOL> end += <NUM_LIT:1> <EOL> try : <EOL> if s [ end ] in _ws : <EOL> end += <NUM_LIT:1> <EOL> if s [ end ] in _ws : <EOL> end = _w ( s , end + <NUM_LIT:1> ) . end ( ) <EOL> except IndexError : <EOL> pass <EOL> try : <EOL> value , end = scan_once ( s , end ) <EOL> except StopIteration : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end ) ) <EOL> pairs [ key ] = value <EOL> try : <EOL> nextchar = s [ end ] <EOL> if nextchar in _ws : <EOL> end = _w ( s , end + <NUM_LIT:1> ) . end ( ) <EOL> nextchar = s [ end ] <EOL> except IndexError : <EOL> nextchar = '<STR_LIT>' <EOL> end += <NUM_LIT:1> <EOL> if nextchar == '<STR_LIT:}>' : <EOL> break <EOL> elif nextchar != '<STR_LIT:U+002C>' : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end - <NUM_LIT:1> ) ) <EOL> try : <EOL> nextchar = s [ end ] <EOL> if nextchar in _ws : <EOL> end += <NUM_LIT:1> <EOL> nextchar = s [ end ] <EOL> if nextchar in _ws : <EOL> end = _w ( s , end + <NUM_LIT:1> ) . end ( ) <EOL> nextchar = s [ end ] <EOL> except IndexError : <EOL> nextchar = '<STR_LIT>' <EOL> end += <NUM_LIT:1> <EOL> if nextchar != '<STR_LIT:">' : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end - <NUM_LIT:1> ) ) <EOL> if object_hook is not None : <EOL> pairs = object_hook ( pairs ) <EOL> return pairs , end <EOL> def JSONArray ( ( s , end ) , scan_once , _w = WHITESPACE . match , _ws = WHITESPACE_STR ) : <EOL> values = [ ] <EOL> nextchar = s [ end : end + <NUM_LIT:1> ] <EOL> if nextchar in _ws : <EOL> end = _w ( s , end + <NUM_LIT:1> ) . end ( ) <EOL> nextchar = s [ end : end + <NUM_LIT:1> ] <EOL> if nextchar == '<STR_LIT:]>' : <EOL> return values , end + <NUM_LIT:1> <EOL> _append = values . append <EOL> while True : <EOL> try : <EOL> value , end = scan_once ( s , end ) <EOL> except StopIteration : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end ) ) <EOL> _append ( value ) <EOL> nextchar = s [ end : end + <NUM_LIT:1> ] <EOL> if nextchar in _ws : <EOL> end = _w ( s , end + <NUM_LIT:1> ) . end ( ) <EOL> nextchar = s [ end : end + <NUM_LIT:1> ] <EOL> end += <NUM_LIT:1> <EOL> if nextchar == '<STR_LIT:]>' : <EOL> break <EOL> elif nextchar != '<STR_LIT:U+002C>' : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end ) ) <EOL> try : <EOL> if s [ end ] in _ws : <EOL> end += <NUM_LIT:1> <EOL> if s [ end ] in _ws : <EOL> end = _w ( s , end + <NUM_LIT:1> ) . end ( ) <EOL> except IndexError : <EOL> pass <EOL> return values , end <EOL> class JSONDecoder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , encoding = None , object_hook = None , parse_float = None , <EOL> parse_int = None , parse_constant = None , strict = True ) : <EOL> """<STR_LIT>""" <EOL> self . encoding = encoding <EOL> self . object_hook = object_hook <EOL> self . parse_float = parse_float or float <EOL> self . parse_int = parse_int or int <EOL> self . parse_constant = parse_constant or _CONSTANTS . __getitem__ <EOL> self . strict = strict <EOL> self . parse_object = JSONObject <EOL> self . parse_array = JSONArray <EOL> self . parse_string = scanstring <EOL> self . scan_once = make_scanner ( self ) <EOL> def decode ( self , s , _w = WHITESPACE . match ) : <EOL> """<STR_LIT>""" <EOL> obj , end = self . raw_decode ( s , idx = _w ( s , <NUM_LIT:0> ) . end ( ) ) <EOL> end = _w ( s , end ) . end ( ) <EOL> if end != len ( s ) : <EOL> raise ValueError ( errmsg ( "<STR_LIT>" , s , end , len ( s ) ) ) <EOL> return obj <EOL> def raw_decode ( self , s , idx = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> obj , end = self . scan_once ( s , idx ) <EOL> except StopIteration : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return obj , end </s>
