Package lepl :: Package contrib :: Module json
[hide private]
[frames] | no frames]

Source Code for Module lepl.contrib.json

 1   
 2  # The contents of this file are subject to the Mozilla Public License 
 3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
 4  # in compliance with the License. You may obtain a copy of the License 
 5  # at http://www.mozilla.org/MPL/ 
 6  # 
 7  # Software distributed under the License is distributed on an "AS IS" 
 8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 9  # the License for the specific language governing rights and 
10  # limitations under the License. 
11  # 
12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
13  # The Initial Developer of the Original Code is Andrew Cooke. 
14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
16  # 
17  # Contributor(s): 
18  # - "magcius" / jstpierre at mecheye dot net 
19  # Portions created by the Contributors are Copyright (C) 2010 
20  # The Contributors.  All Rights Reserved. 
21  # 
22  # Alternatively, the contents of this file may be used under the terms 
23  # of the LGPL license (the GNU Lesser General Public License, 
24  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
25  # of the LGPL License are applicable instead of those above. 
26  # 
27  # If you wish to allow use of your version of this file only under the 
28  # terms of the LGPL License and not to allow others to use your version 
29  # of this file under the MPL, indicate your decision by deleting the 
30  # provisions above and replace them with the notice and other provisions 
31  # required by the LGPL License.  If you do not delete the provisions 
32  # above, a recipient may use your version of this file under either the 
33  # MPL or the LGPL License. 
34   
35  ''' 
36  http://www.ietf.org/rfc/rfc4627.txt 
37  ''' 
38   
39   
40  from lepl import * 
41  from lepl.support.lib import str 
42   
43   
44 -def Simple():
45 ''' 46 A simple JSON parser. 47 ''' 48 49 escapes = {'\\b': '\b', '\\f': '\f', '\\n': '\n', '\\t': '\t', '\\r': '\r'} 50 51 def unescape_string(text): 52 return escapes[text]
53 54 def unescape_unicode(text): 55 # Python 3 only 56 return bytes(str(text), 'utf8').decode('unicode_escape') 57 58 value = Delayed() 59 60 unicode_escape = ("\\u" + Digit()[4, ...]) >> unescape_unicode 61 regular_escape = ("\\" + Any("bfntr")) >> unescape_string 62 escape = (unicode_escape | regular_escape) 63 string = (Drop('"') & (AnyBut('"\\') | escape)[...] & Drop('"')) 64 65 number = Real() >> float 66 67 comma = Drop(',') 68 69 with DroppedSpace(): 70 array = Drop("[") & value[:, comma] & Drop("]") > list 71 72 pair = string & Drop(":") & value > tuple 73 object_ = Drop("{") & pair[:, comma] & Drop("}") > dict 74 75 76 value += ((Literal('true') >= (lambda x: True)) | 77 (Literal('false') >= (lambda x: False)) | 78 (Literal('null') >= (lambda x: None)) | 79 array | object_ | number | string) 80 81 return value 82