yson.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;(function(){
  2. // JSON: JavaScript Object Notation
  3. // YSON: Yielding javaScript Object Notation
  4. var yson = {}, u, sI = setTimeout.turn || (typeof setImmediate != ''+u && setImmediate) || setTimeout;
  5. yson.parseAsync = function(text, done, revive, M){
  6. var ctx = {i: 0, text: text, done: done, o: {}, l: text.length, up: []};
  7. ctx.at = ctx.o;
  8. //M = 1024 * 1024 * 100;
  9. //M = M || 1024 * 64;
  10. M = M || 1024 * 32;
  11. parse();
  12. function parse(){
  13. //var S = +new Date;
  14. var s = ctx.text, o = ctx.o;
  15. var i = ctx.i, l = ctx.l, j = 0;
  16. var w = ctx.w, b, tmp;
  17. while(j++ < M){
  18. var c = s[i++];
  19. if(i > l){
  20. ctx.end = true;
  21. break;
  22. }
  23. if(w){
  24. i = s.indexOf('"', i-1); c = s[i];
  25. tmp = '\\' == s[i-1];
  26. b = b || tmp;
  27. if('"' == c && !tmp){
  28. w = u;
  29. tmp = ctx.s;
  30. if(ctx.a){
  31. tmp = s.slice(ctx.sl, i);
  32. if(b){ tmp = JSON.parse('"'+tmp+'"') }
  33. if(ctx.at instanceof Array){
  34. ctx.at.push(ctx.s = tmp);
  35. } else {
  36. ctx.at[ctx.s] = ctx.s = tmp;
  37. }
  38. } else {
  39. ctx.s = s.slice(ctx.sl, i);
  40. if(b){ ctx.s = JSON.parse('"'+ctx.s+'"'); }
  41. }
  42. ctx.a = b = u;
  43. }
  44. ++i;
  45. } else {
  46. switch(c){
  47. case '"':
  48. ctx.sl = i;
  49. w = true;
  50. break;
  51. case ':':
  52. ctx.ai = i;
  53. ctx.a = true;
  54. break;
  55. case ',':
  56. if(ctx.a || ctx.at instanceof Array){
  57. if(tmp = s.slice(ctx.ai, i-1)){
  58. if(u !== (tmp = value(tmp))){
  59. if(ctx.at instanceof Array){
  60. ctx.at.push(tmp);
  61. } else {
  62. ctx.at[ctx.s] = tmp;
  63. }
  64. }
  65. }
  66. }
  67. ctx.a = u;
  68. if(ctx.at instanceof Array){
  69. ctx.a = true;
  70. ctx.ai = i;
  71. }
  72. break;
  73. case '{':
  74. ctx.up.push(ctx.at);
  75. if(ctx.at instanceof Array){
  76. ctx.at.push(ctx.at = {});
  77. } else
  78. if(tmp = ctx.s){
  79. ctx.at[tmp] = ctx.at = {};
  80. }
  81. ctx.a = u;
  82. break;
  83. case '}':
  84. if(ctx.a){
  85. if(tmp = s.slice(ctx.ai, i-1)){
  86. if(u !== (tmp = value(tmp))){
  87. if(ctx.at instanceof Array){
  88. ctx.at.push(tmp);
  89. } else {
  90. ctx.at[ctx.s] = tmp;
  91. }
  92. }
  93. }
  94. }
  95. ctx.a = u;
  96. ctx.at = ctx.up.pop();
  97. break;
  98. case '[':
  99. if(tmp = ctx.s){
  100. ctx.up.push(ctx.at);
  101. ctx.at[tmp] = ctx.at = [];
  102. }
  103. ctx.a = true;
  104. break;
  105. case ']':
  106. if(ctx.a){
  107. if(tmp = s.slice(ctx.ai, i-1)){
  108. if(u !== (tmp = value(tmp))){
  109. if(ctx.at instanceof Array){
  110. ctx.at.push(tmp);
  111. } else {
  112. ctx.at[ctx.s] = tmp;
  113. }
  114. }
  115. }
  116. }
  117. ctx.a = u;
  118. ctx.at = ctx.up.pop();
  119. break;
  120. }
  121. }
  122. }
  123. ctx.i = i;
  124. ctx.w = w;
  125. //console.log("!!!!!!!!", +new Date - S, ctx.i, ctx.l);
  126. if(ctx.end){
  127. ctx.done(u, ctx.o);
  128. } else {
  129. //setTimeout.turn(parse);
  130. sI(parse);
  131. }
  132. }
  133. }
  134. function value(s){
  135. var n = parseFloat(s);
  136. if(!isNaN(n)){
  137. return n;
  138. }
  139. s = s.trim();
  140. if('true' == s){
  141. return true;
  142. }
  143. if('false' == s){
  144. return false;
  145. }
  146. if('null' == s){
  147. return null;
  148. }
  149. }
  150. module.exports = yson;
  151. }());