webmcp
annotate framework/env/encode/json.lua @ 506:83b3882dc31b
New functions moonhash.shake128(data, len, alphabet), moonhash.shake256(data, len, alphabet)
Removed moonhash.shake128_128(...)
Removed moonhash.shake128_128(...)
| author | jbe | 
|---|---|
| date | Wed Aug 16 00:31:11 2017 +0200 (2017-08-16) | 
| parents | 81d94b362043 | 
| children | 
| rev | line source | 
|---|---|
| jbe/bsw@0 | 1 --[[-- | 
| jbe@285 | 2 json_string = -- JavaScript code | 
| jbe/bsw@0 | 3 encode.json( | 
| jbe@285 | 4 value -- nil, false, true, a number, a string, or json.array{...} or json.object{...} | 
| jbe/bsw@0 | 5 ) | 
| jbe/bsw@0 | 6 | 
| jbe@285 | 7 This function encodes any native datatype or table structure to JavaScript object notation (JSON). In order to distinguish between the empty array and the empty object, use json.array{} and json.object{} respectively. | 
| jbe@10 | 8 | 
| jbe@285 | 9 The return value of this function is additionally escaped in such way that it can be safely included in inline scripts both in HTML and XHTML (within CDATA section). | 
| jbe@223 | 10 | 
| jbe/bsw@0 | 11 --]]-- | 
| jbe/bsw@0 | 12 | 
| jbe/bsw@0 | 13 function encode.json(obj) | 
| jbe@293 | 14 local str = json.export(obj) | 
| jbe@285 | 15 str = string.gsub(str, "</", "<\\/") | 
| jbe@285 | 16 str = string.gsub(str, "<!%[CDATA%[", "\\u003c![CDATA[") | 
| jbe@285 | 17 str = string.gsub(str, "]]>", "]]\\u003e") | 
| jbe@285 | 18 return str | 
| jbe/bsw@0 | 19 end |