<s> """<STR_LIT>""" <EOL> import re <EOL> from decimal import Decimal <EOL> def _import_speedups ( ) : <EOL> try : <EOL> raise ImportError <EOL> from simplejson import _speedups <EOL> return _speedups . encode_basestring_ascii , _speedups . make_encoder <EOL> except ImportError : <EOL> return None , None <EOL> c_encode_basestring_ascii , c_make_encoder = _import_speedups ( ) <EOL> from decoder import PosInf <EOL> ESCAPE = re . compile ( r'<STR_LIT>' ) <EOL> ESCAPE_ASCII = re . compile ( r'<STR_LIT>' ) <EOL> HAS_UTF8 = re . compile ( r'<STR_LIT>' ) <EOL> ESCAPE_DCT = { <EOL> '<STR_LIT:\\>' : '<STR_LIT>' , <EOL> '<STR_LIT:">' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:\n>' : '<STR_LIT>' , <EOL> '<STR_LIT:\r>' : '<STR_LIT>' , <EOL> '<STR_LIT:\t>' : '<STR_LIT>' , <EOL> } <EOL> for i in range ( <NUM_LIT> ) : <EOL> ESCAPE_DCT . setdefault ( chr ( i ) , '<STR_LIT>' % ( i , ) ) <EOL> FLOAT_REPR = repr <EOL> def encode_basestring ( s ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , str ) and HAS_UTF8 . search ( s ) is not None : <EOL> s = s . decode ( '<STR_LIT:utf-8>' ) <EOL> def replace ( match ) : <EOL> return ESCAPE_DCT [ match . group ( <NUM_LIT:0> ) ] <EOL> return u'<STR_LIT:">' + ESCAPE . sub ( replace , s ) + u'<STR_LIT:">' <EOL> def py_encode_basestring_ascii ( s ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , str ) and HAS_UTF8 . search ( s ) is not None : <EOL> s = s . decode ( '<STR_LIT:utf-8>' ) <EOL> def replace ( match ) : <EOL> s = match . group ( <NUM_LIT:0> ) <EOL> try : <EOL> return ESCAPE_DCT [ s ] <EOL> except KeyError : <EOL> n = ord ( s ) <EOL> if n < <NUM_LIT> : <EOL> return '<STR_LIT>' % ( n , ) <EOL> else : <EOL> n -= <NUM_LIT> <EOL> s1 = <NUM_LIT> | ( ( n >> <NUM_LIT:10> ) & <NUM_LIT> ) <EOL> s2 = <NUM_LIT> | ( n & <NUM_LIT> ) <EOL> return '<STR_LIT>' % ( s1 , s2 ) <EOL> return '<STR_LIT:">' + str ( ESCAPE_ASCII . sub ( replace , s ) ) + '<STR_LIT:">' <EOL> encode_basestring_ascii = ( <EOL> c_encode_basestring_ascii or py_encode_basestring_ascii ) <EOL> class JSONEncoder ( object ) : <EOL> """<STR_LIT>""" <EOL> item_separator = '<STR_LIT:U+002CU+0020>' <EOL> key_separator = '<STR_LIT>' <EOL> def __init__ ( self , skipkeys = False , ensure_ascii = True , <EOL> check_circular = True , allow_nan = True , sort_keys = False , <EOL> indent = None , separators = None , encoding = '<STR_LIT:utf-8>' , default = None , <EOL> use_decimal = False ) : <EOL> """<STR_LIT>""" <EOL> self . skipkeys = skipkeys <EOL> self . ensure_ascii = ensure_ascii <EOL> self . check_circular = check_circular <EOL> self . allow_nan = allow_nan <EOL> self . sort_keys = sort_keys <EOL> self . use_decimal = use_decimal <EOL> if isinstance ( indent , ( int , long ) ) : <EOL> indent = '<STR_LIT:U+0020>' * indent <EOL> self . indent = indent <EOL> if separators is not None : <EOL> self . item_separator , self . key_separator = separators <EOL> if default is not None : <EOL> self . default = default <EOL> self . encoding = encoding <EOL> def default ( self , o ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( repr ( o ) + "<STR_LIT>" ) <EOL> def encode ( self , o ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( o , basestring ) : <EOL> if isinstance ( o , str ) : <EOL> _encoding = self . encoding <EOL> if ( _encoding is not None <EOL> and not ( _encoding == '<STR_LIT:utf-8>' ) ) : <EOL> o = o . decode ( _encoding ) <EOL> if self . ensure_ascii : <EOL> return encode_basestring_ascii ( o ) <EOL> else : <EOL> return encode_basestring ( o ) <EOL> chunks = self . iterencode ( o , _one_shot = True ) <EOL> if not isinstance ( chunks , ( list , tuple ) ) : <EOL> chunks = list ( chunks ) <EOL> if self . ensure_ascii : <EOL> return '<STR_LIT>' . join ( chunks ) <EOL> else : <EOL> return u'<STR_LIT>' . join ( chunks ) <EOL> def iterencode ( self , o , _one_shot = False ) : <EOL> """<STR_LIT>""" <EOL> if self . check_circular : <EOL> markers = { } <EOL> else : <EOL> markers = None <EOL> if self . ensure_ascii : <EOL> _encoder = encode_basestring_ascii <EOL> else : <EOL> _encoder = encode_basestring <EOL> if self . encoding != '<STR_LIT:utf-8>' : <EOL> def _encoder ( o , _orig_encoder = _encoder , _encoding = self . encoding ) : <EOL> if isinstance ( o , str ) : <EOL> o = o . decode ( _encoding ) <EOL> return _orig_encoder ( o ) <EOL> def floatstr ( o , allow_nan = self . allow_nan , <EOL> _repr = FLOAT_REPR , _inf = PosInf , _neginf = - PosInf ) : <EOL> if o != o : <EOL> text = '<STR_LIT>' <EOL> elif o == _inf : <EOL> text = '<STR_LIT>' <EOL> elif o == _neginf : <EOL> text = '<STR_LIT>' <EOL> else : <EOL> return _repr ( o ) <EOL> if not allow_nan : <EOL> raise ValueError ( <EOL> "<STR_LIT>" + <EOL> repr ( o ) ) <EOL> return text <EOL> key_memo = { } <EOL> if ( _one_shot and c_make_encoder is not None <EOL> and self . indent is None ) : <EOL> _iterencode = c_make_encoder ( <EOL> markers , self . default , _encoder , self . indent , <EOL> self . key_separator , self . item_separator , self . sort_keys , <EOL> self . skipkeys , self . allow_nan , key_memo , self . use_decimal ) <EOL> else : <EOL> _iterencode = _make_iterencode ( <EOL> markers , self . default , _encoder , self . indent , floatstr , <EOL> self . key_separator , self . item_separator , self . sort_keys , <EOL> self . skipkeys , _one_shot , self . use_decimal ) <EOL> try : <EOL> return _iterencode ( o , <NUM_LIT:0> ) <EOL> finally : <EOL> key_memo . clear ( ) <EOL> class JSONEncoderForHTML ( JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def encode ( self , o ) : <EOL> chunks = self . iterencode ( o , True ) <EOL> if self . ensure_ascii : <EOL> return '<STR_LIT>' . join ( chunks ) <EOL> else : <EOL> return u'<STR_LIT>' . join ( chunks ) <EOL> def iterencode ( self , o , _one_shot = False ) : <EOL> chunks = super ( JSONEncoderForHTML , self ) . iterencode ( o , _one_shot ) <EOL> for chunk in chunks : <EOL> chunk = chunk . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) <EOL> chunk = chunk . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) <EOL> chunk = chunk . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) <EOL> yield chunk <EOL> def _make_iterencode ( markers , _default , _encoder , _indent , _floatstr , <EOL> _key_separator , _item_separator , _sort_keys , _skipkeys , _one_shot , <EOL> _use_decimal , <EOL> False = False , <EOL> True = True , <EOL> ValueError = ValueError , <EOL> basestring = basestring , <EOL> Decimal = Decimal , <EOL> dict = dict , <EOL> float = float , <EOL> id = id , <EOL> int = int , <EOL> isinstance = isinstance , <EOL> list = list , <EOL> long = long , <EOL> str = str , <EOL> tuple = tuple , <EOL> ) : <EOL> def _iterencode_list ( lst , _current_indent_level ) : <EOL> if not lst : <EOL> yield '<STR_LIT>' <EOL> return <EOL> if markers is not None : <EOL> markerid = id ( lst ) <EOL> if markerid in markers : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> markers [ markerid ] = lst <EOL> buf = '<STR_LIT:[>' <EOL> if _indent is not None : <EOL> _current_indent_level += <NUM_LIT:1> <EOL> newline_indent = '<STR_LIT:\n>' + ( _indent * _current_indent_level ) <EOL> separator = _item_separator + newline_indent <EOL> buf += newline_indent <EOL> else : <EOL> newline_indent = None <EOL> separator = _item_separator <EOL> first = True <EOL> for value in lst : <EOL> if first : <EOL> first = False <EOL> else : <EOL> buf = separator <EOL> if isinstance ( value , basestring ) : <EOL> yield buf + _encoder ( value ) <EOL> elif value is None : <EOL> yield buf + '<STR_LIT:null>' <EOL> elif value is True : <EOL> yield buf + '<STR_LIT:true>' <EOL> elif value is False : <EOL> yield buf + '<STR_LIT:false>' <EOL> elif isinstance ( value , ( int , long ) ) : <EOL> yield buf + str ( value ) <EOL> elif isinstance ( value , float ) : <EOL> yield buf + _floatstr ( value ) <EOL> elif _use_decimal and isinstance ( value , Decimal ) : <EOL> yield buf + str ( value ) <EOL> else : <EOL> yield buf <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> chunks = _iterencode_list ( value , _current_indent_level ) <EOL> elif isinstance ( value , dict ) : <EOL> chunks = _iterencode_dict ( value , _current_indent_level ) <EOL> else : <EOL> chunks = _iterencode ( value , _current_indent_level ) <EOL> for chunk in chunks : <EOL> yield chunk <EOL> if newline_indent is not None : <EOL> _current_indent_level -= <NUM_LIT:1> <EOL> yield '<STR_LIT:\n>' + ( _indent * _current_indent_level ) <EOL> yield '<STR_LIT:]>' <EOL> if markers is not None : <EOL> del markers [ markerid ] <EOL> def _iterencode_dict ( dct , _current_indent_level ) : <EOL> if not dct : <EOL> yield '<STR_LIT:{}>' <EOL> return <EOL> if markers is not None : <EOL> markerid = id ( dct ) <EOL> if markerid in markers : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> markers [ markerid ] = dct <EOL> yield '<STR_LIT:{>' <EOL> if _indent is not None : <EOL> _current_indent_level += <NUM_LIT:1> <EOL> newline_indent = '<STR_LIT:\n>' + ( _indent * _current_indent_level ) <EOL> item_separator = _item_separator + newline_indent <EOL> yield newline_indent <EOL> else : <EOL> newline_indent = None <EOL> item_separator = _item_separator <EOL> first = True <EOL> if _sort_keys : <EOL> items = dct . items ( ) <EOL> items . sort ( key = lambda kv : kv [ <NUM_LIT:0> ] ) <EOL> else : <EOL> items = dct . iteritems ( ) <EOL> for key , value in items : <EOL> if isinstance ( key , basestring ) : <EOL> pass <EOL> elif isinstance ( key , float ) : <EOL> key = _floatstr ( key ) <EOL> elif key is True : <EOL> key = '<STR_LIT:true>' <EOL> elif key is False : <EOL> key = '<STR_LIT:false>' <EOL> elif key is None : <EOL> key = '<STR_LIT:null>' <EOL> elif isinstance ( key , ( int , long ) ) : <EOL> key = str ( key ) <EOL> elif _skipkeys : <EOL> continue <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" + repr ( key ) + "<STR_LIT>" ) <EOL> if first : <EOL> first = False <EOL> else : <EOL> yield item_separator <EOL> yield _encoder ( key ) <EOL> yield _key_separator <EOL> if isinstance ( value , basestring ) : <EOL> yield _encoder ( value ) <EOL> elif value is None : <EOL> yield '<STR_LIT:null>' <EOL> elif value is True : <EOL> yield '<STR_LIT:true>' <EOL> elif value is False : <EOL> yield '<STR_LIT:false>' <EOL> elif isinstance ( value , ( int , long ) ) : <EOL> yield str ( value ) <EOL> elif isinstance ( value , float ) : <EOL> yield _floatstr ( value ) <EOL> elif _use_decimal and isinstance ( value , Decimal ) : <EOL> yield str ( value ) <EOL> else : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> chunks = _iterencode_list ( value , _current_indent_level ) <EOL> elif isinstance ( value , dict ) : <EOL> chunks = _iterencode_dict ( value , _current_indent_level ) <EOL> else : <EOL> chunks = _iterencode ( value , _current_indent_level ) <EOL> for chunk in chunks : <EOL> yield chunk <EOL> if newline_indent is not None : <EOL> _current_indent_level -= <NUM_LIT:1> <EOL> yield '<STR_LIT:\n>' + ( _indent * _current_indent_level ) <EOL> yield '<STR_LIT:}>' <EOL> if markers is not None : <EOL> del markers [ markerid ] <EOL> def _iterencode ( o , _current_indent_level ) : <EOL> if isinstance ( o , basestring ) : <EOL> yield _encoder ( o ) <EOL> elif o is None : <EOL> yield '<STR_LIT:null>' <EOL> elif o is True : <EOL> yield '<STR_LIT:true>' <EOL> elif o is False : <EOL> yield '<STR_LIT:false>' <EOL> elif isinstance ( o , ( int , long ) ) : <EOL> yield str ( o ) <EOL> elif isinstance ( o , float ) : <EOL> yield _floatstr ( o ) <EOL> elif isinstance ( o , ( list , tuple ) ) : <EOL> for chunk in _iterencode_list ( o , _current_indent_level ) : <EOL> yield chunk <EOL> elif isinstance ( o , dict ) : <EOL> for chunk in _iterencode_dict ( o , _current_indent_level ) : <EOL> yield chunk <EOL> elif _use_decimal and isinstance ( o , Decimal ) : <EOL> yield str ( o ) <EOL> else : <EOL> if markers is not None : <EOL> markerid = id ( o ) <EOL> if markerid in markers : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> markers [ markerid ] = o <EOL> o = _default ( o ) <EOL> for chunk in _iterencode ( o , _current_indent_level ) : <EOL> yield chunk <EOL> if markers is not None : <EOL> del markers [ markerid ] <EOL> return _iterencode </s>
