Package pyjamas :: Module JSONParser
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.JSONParser

  1  from __pyjamas__ import JS 
  2  import math 
  3   
  4  """ 
  5  /* 
  6  JSONEncode: 
  7      +---------------+-------------------+---------------+ 
  8      | PYGWT         | Python            | JSON          | 
  9      +===============+===================+===============+ 
 10      | pyjslib.dict  | dict              | object        | 
 11      +---------------+-------------------+---------------+ 
 12      | pyjslib.list  | list              | array         | 
 13      +---------------+-------------------+---------------+ 
 14      | pyjslib.tuple | tuple             | array         | 
 15      +---------------+-------------------+---------------+ 
 16      | string        | str, unicode      | string        | 
 17      +---------------+-------------------+---------------+ 
 18      | number        | int, long, float  | number        | 
 19      +---------------+-------------------+---------------+ 
 20      | true          | True              | true          | 
 21      +---------------+-------------------+---------------+ 
 22      | false         | False             | false         | 
 23      +---------------+-------------------+---------------+ 
 24      | null          | None              | null          | 
 25      +---------------+-------------------+---------------+ 
 26   
 27   
 28  JSONDecode: 
 29      +---------------+-------------------+--------------+ 
 30      | JSON          | Python            | PYGWT        | 
 31      +===============+===================+==============+ 
 32      | object        | dict              | pyjslib.dict | 
 33      +---------------+-------------------+--------------+ 
 34      | array         | list              | pyjslib.list | 
 35      +---------------+-------------------+--------------+ 
 36      | string        | unicode           | string       | 
 37      +---------------+-------------------+--------------+ 
 38      | number (int)  | int, long         | number       | 
 39      +---------------+-------------------+--------------+ 
 40      | number (real) | float             | number       | 
 41      +---------------+-------------------+--------------+ 
 42      | true          | True              | true         | 
 43      +---------------+-------------------+--------------+ 
 44      | false         | False             | false        | 
 45      +---------------+-------------------+--------------+ 
 46      | null          | None              | null         | 
 47      +---------------+-------------------+--------------+ 
 48  */ 
 49  """ 
 50   
 51   
 52  # toJSONString & parseJSON from http://www.json.org/json.js 
 53   
54 -class JSONParser:
55 - def decode(self, str):
56 return self.jsObjectToPy(self.parseJSON(str))
57
58 - def decodeAsObject(self, str):
59 return self.jsObjectToPyObject(self.parseJSON(str))
60
61 - def encode(self, obj):
62 return self.toJSONString(obj)
63
64 - def jsObjectToPy(self, obj):
65 JS(""" 66 if (pyjslib.isArray(obj)) { 67 for (var i in obj) obj[i] = this.jsObjectToPy(obj[i]); 68 obj=new pyjslib.list(obj); 69 } 70 else if (pyjslib.isObject(obj)) { 71 for (var i in obj) obj[i]=this.jsObjectToPy(obj[i]); 72 obj=new pyjslib.dict(obj); 73 } 74 75 return obj; 76 """)
77
78 - def jsObjectToPyObject(self, obj):
79 JS(""" 80 if (pyjslib.isArray(obj)) { 81 for (var i in obj) obj[i] = this.jsObjectToPyObject(obj[i]); 82 obj=new pyjslib.list(obj); 83 } 84 else if (pyjslib.isObject(obj)) { 85 if (obj["__jsonclass__"]) { 86 var class_name = obj["__jsonclass__"][0]; 87 delete obj["__jsonclass__"]; 88 obj = this.jsObjectToPyObject(obj); 89 90 obj = $pyjs_kwargs_call(null, eval(class_name), null, obj, [{}]); 91 } 92 else { 93 for (var i in obj) obj[i]=this.jsObjectToPyObject(obj[i]); 94 obj=new pyjslib.dict(obj); 95 } 96 } 97 98 return obj; 99 """)
100 101 # modified to detect pyjslib.list & pyjslib.dict
102 - def toJSONString(self, obj):
103 JS(r""" 104 var m = { 105 '\b': '\\b', 106 '\t': '\\t', 107 '\n': '\\n', 108 '\f': '\\f', 109 '\r': '\\r', 110 '"' : '\\"', 111 '\\': '\\\\' 112 }, 113 s = { 114 array: function (x) { 115 var a = ['['], b, f, i, l = x.length, v; 116 for (i = 0; i < l; i += 1) { 117 v = x[i]; 118 f = s[typeof v]; 119 if (f) { 120 v = f(v); 121 if (typeof v == 'string') { 122 if (b) { 123 a[a.length] = ','; 124 } 125 a[a.length] = v; 126 b = true; 127 } 128 } 129 } 130 a[a.length] = ']'; 131 return a.join(''); 132 }, 133 'boolean': function (x) { 134 return String(x); 135 }, 136 'undefined':function (x) { 137 return "null"; 138 }, 139 'null': function (x) { 140 return "null"; 141 }, 142 number: function (x) { 143 return isFinite(x) ? String(x) : 'null'; 144 }, 145 object: function (x) { 146 if (x) { 147 if (x.__number__) { 148 return String(x); 149 } 150 if (x instanceof Array) { 151 return s.array(x); 152 } 153 if (x instanceof pyjslib.list) { 154 return s.array(x.__array); 155 } 156 if (x instanceof pyjslib.tuple) { 157 return s.array(x.__array); 158 } 159 if (x instanceof pyjslib.dict) { 160 return s.object(pyjslib.toJSObjects(x)); 161 } 162 var a = ['{'], b, f, i, v; 163 for (i in x) { 164 v = x[i]; 165 f = s[typeof v]; 166 if (f) { 167 v = f(v); 168 if (typeof v == 'string') { 169 if (b) { 170 a[a.length] = ','; 171 } 172 a.push(s.string(i), ':', v); 173 b = true; 174 } 175 } 176 } 177 a[a.length] = '}'; 178 return a.join(''); 179 } 180 return 'null'; 181 }, 182 string: function (x) { 183 if (/["\\\x00-\x1f]/.test(x)) { 184 x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) { 185 var c = m[b]; 186 if (c) { 187 return c; 188 } 189 c = b.charCodeAt(); 190 return '\\u00' + 191 math.floor(c / 16).toString(16) + 192 (c % 16).toString(16); 193 }); 194 } 195 return '"' + x + '"'; 196 } 197 }; 198 199 typ = typeof obj; 200 f=s[typ]; 201 return f(obj); 202 """)
203
204 - def parseJSON(self, str):
205 JS(r""" 206 try { 207 return (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(str)) && 208 eval('(' + str + ')'); 209 } catch (e) { 210 return false; 211 } 212 """)
213