Created
April 8, 2009 15:24
-
-
Save satyr/91823 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* appjet:version 0.1 */ | |
import(appjet.appName, 'lib-json'); | |
const Data = [{ | |
'null': null, | |
date : new Date, | |
number: [Math.random(), Math.random() / 9e99, Math.random() * 9e99], | |
regexp: /\\u[0-9a-f]{4}/ig, | |
string: (function(){ | |
for(var i = -1, a = []; ++i < 999;) a[i] = i; | |
return String.fromCharCode.apply(0, a); | |
}()), | |
'boolean': {'\t': true, '\f': false}, | |
}]; | |
dispatch(); | |
function get_main(){ | |
print( | |
UL(LI(link('http://appjet.com/app/'+ appjet.encodedAppKey +'/docs')), | |
LI(link('test'))), | |
CODE(JS0N.stringify(JS0N))); | |
} | |
function get_test(){ | |
http://www.jsonlint.com/ | |
JS0N.put(Data, request.params.c); | |
} | |
function get_bench(){ | |
bench(function js0n(){ JS0N.stringify(Data) }); | |
bench(function json(){ JSON.stringify(Data) }); | |
} | |
function bench(fn){ | |
var t = Date.now(), i = 99; | |
while(i--) fn(); | |
printp(fn.name, ' : ', Date.now() - t); | |
} | |
/* appjet:library */ | |
/** | |
* @overview | |
* Provides JS0N, a pseudo <a href="http://lib-json.appjet.net/">JSON</a>. | |
* <dl> | |
* <dt>pros</dt><dd>multi-byte friendly</dd> | |
* <dt>cons</dt><dd>slower</dd> | |
* </dl> | |
*/ | |
let JS0N = (function(){ | |
/** Parses JSON without security-checking, aka eval. | |
* @param {string} json | |
* @return {*} value | |
*/ | |
function parse(j){ return eval('0,'+ j) } | |
/** Turns any value into JSON. Non-JSON values are treated as string. | |
* @param {*} value | |
* @return {string} json | |
*/ | |
function stringify(v){ | |
switch(typeof v){ | |
case 'string' : return jstr(v); | |
case 'number' : return isFinite(v) ? String(v) : '"'+ v +'"'; | |
case 'boolean': return String(v); | |
case 'object' : | |
if(!v) return 'null'; | |
if(typeof v.toJSON === 'function') | |
return stringify(v.toJSON()); | |
if(v.toString === Object.prototype.toString){ | |
var a = [], i = -1, k; | |
for(k in v) a[++i] = jstr(k) +':'+ stringify(v[k]); | |
return '{'+ a.join(',') +'}'; | |
} | |
if(v instanceof Array) | |
return '['+ v.map(stringify).join(',') +']'; | |
if(v instanceof Date) | |
return sprintf('"%04d-%02d-%02dT%02d:%02d:%02dZ"', | |
v.getUTCFullYear(), v.getUTCMonth() + 1, | |
v.getUTCDate(), v.getUTCHours(), | |
v.getUTCMinutes(), v.getUTCSeconds()); | |
} | |
return jstr(uneval(v)); | |
} | |
/** Uneval a string into JSON string. | |
* @param {string} s | |
* @return {string} json | |
*/ | |
function jstr(s){ | |
return uneval(s).replace(/\\(?:[\\v]|x(?=[0-9a-f][0-9a-f]))/g, rper); | |
} | |
/** @ignore */ | |
function rper($){ return dic[$] } | |
var dic = {'\\\\': '\\\\', '\\x': '\\u00', '\\v': '\\u000b'}; | |
/** Utility to emit JSON (or JSONP if callback is provided). | |
* @param {*} value | |
* @param {string} callback | |
*/ | |
function put(v, cb){ | |
var p = typeof cb === 'string'; | |
page.setMode('plain'); | |
response.setContentType( | |
(p ? 'text/javascript' : 'application/json') +';charset=utf-8'); | |
var j = stringify(v); | |
if(p) j = cb +'('+ j +')'; | |
response.writeBytes(j); | |
} | |
return {parse: parse, stringify: stringify, jstr: jstr, put: put}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment