Advertisement
konyakov

json.js | AJAX+PHP+MySQL Autosuggest (Cyr & Utf-8)

Dec 26th, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright (c) 2005 JSON.org
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The Software shall be used for Good, not Evil.
  12.  
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21.  
  22. var JSON = {
  23.     org: 'http://www.JSON.org',
  24.     copyright: '(c)2005 JSON.org',
  25.     license: 'http://www.crockford.com/JSON/license.html',
  26.     stringify: function (arg) {
  27.         var c, i, l, s = '', v;
  28.  
  29.         switch (typeof arg) {
  30.         case 'object':
  31.             if (arg) {
  32.                 if (arg.constructor == Array) {
  33.                     for (i = 0; i < arg.length; ++i) {
  34.                         v = this.stringify(arg[i]);
  35.                         if (s) {
  36.                             s += ',';
  37.                         }
  38.                         s += v;
  39.                     }
  40.                     return '[' + s + ']';
  41.                 } else if (typeof arg.toString != 'undefined') {
  42.                     for (i in arg) {
  43.                         v = arg[i];
  44.                         if (typeof v != 'undefined' && typeof v != 'function') {
  45.                             v = this.stringify(v);
  46.                             if (s) {
  47.                                 s += ',';
  48.                             }
  49.                             s += this.stringify(i) + ':' + v;
  50.                         }
  51.                     }
  52.                     return '{' + s + '}';
  53.                 }
  54.             }
  55.             return 'null';
  56.         case 'number':
  57.             return isFinite(arg) ? String(arg) : 'null';
  58.         case 'string':
  59.             l = arg.length;
  60.             s = '"';
  61.             for (i = 0; i < l; i += 1) {
  62.                 c = arg.charAt(i);
  63.                 if (c >= ' ') {
  64.                     if (c == '\\' || c == '"') {
  65.                         s += '\\';
  66.                     }
  67.                     s += c;
  68.                 } else {
  69.                     switch (c) {
  70.                         case '\b':
  71.                             s += '\\b';
  72.                             break;
  73.                         case '\f':
  74.                             s += '\\f';
  75.                             break;
  76.                         case '\n':
  77.                             s += '\\n';
  78.                             break;
  79.                         case '\r':
  80.                             s += '\\r';
  81.                             break;
  82.                         case '\t':
  83.                             s += '\\t';
  84.                             break;
  85.                         default:
  86.                             c = c.charCodeAt();
  87.                             s += '\\u00' + Math.floor(c / 16).toString(16) +
  88.                                 (c % 16).toString(16);
  89.                     }
  90.                 }
  91.             }
  92.             return s + '"';
  93.         case 'boolean':
  94.             return String(arg);
  95.         default:
  96.             return 'null';
  97.         }
  98.     },
  99.     parse: function (text) {
  100.         var at = 0;
  101.         var ch = ' ';
  102.  
  103.         function error(m) {
  104.             throw {
  105.                 name: 'JSONError',
  106.                 message: m,
  107.                 at: at - 1,
  108.                 text: text
  109.             };
  110.         }
  111.  
  112.         function next() {
  113.             ch = text.charAt(at);
  114.             at += 1;
  115.             return ch;
  116.         }
  117.  
  118.         function white() {
  119.             while (ch) {
  120.                 if (ch <= ' ') {
  121.                     next();
  122.                 } else if (ch == '/') {
  123.                     switch (next()) {
  124.                         case '/':
  125.                             while (next() && ch != '\n' && ch != '\r') {}
  126.                             break;
  127.                         case '*':
  128.                             next();
  129.                             for (;;) {
  130.                                 if (ch) {
  131.                                     if (ch == '*') {
  132.                                         if (next() == '/') {
  133.                                             next();
  134.                                             break;
  135.                                         }
  136.                                     } else {
  137.                                         next();
  138.                                     }
  139.                                 } else {
  140.                                     error("Unterminated comment");
  141.                                 }
  142.                             }
  143.                             break;
  144.                         default:
  145.                             error("Syntax error");
  146.                     }
  147.                 } else {
  148.                     break;
  149.                 }
  150.             }
  151.         }
  152.  
  153.         function string() {
  154.             var i, s = '', t, u;
  155.  
  156.             if (ch == '"') {
  157. outer:          while (next()) {
  158.                     if (ch == '"') {
  159.                         next();
  160.                         return s;
  161.                     } else if (ch == '\\') {
  162.                         switch (next()) {
  163.                         case 'b':
  164.                             s += '\b';
  165.                             break;
  166.                         case 'f':
  167.                             s += '\f';
  168.                             break;
  169.                         case 'n':
  170.                             s += '\n';
  171.                             break;
  172.                         case 'r':
  173.                             s += '\r';
  174.                             break;
  175.                         case 't':
  176.                             s += '\t';
  177.                             break;
  178.                         case 'u':
  179.                             u = 0;
  180.                             for (i = 0; i < 4; i += 1) {
  181.                                 t = parseInt(next(), 16);
  182.                                 if (!isFinite(t)) {
  183.                                     break outer;
  184.                                 }
  185.                                 u = u * 16 + t;
  186.                             }
  187.                             s += String.fromCharCode(u);
  188.                             break;
  189.                         default:
  190.                             s += ch;
  191.                         }
  192.                     } else {
  193.                         s += ch;
  194.                     }
  195.                 }
  196.             }
  197.             error("Bad string");
  198.         }
  199.  
  200.         function array() {
  201.             var a = [];
  202.  
  203.             if (ch == '[') {
  204.                 next();
  205.                 white();
  206.                 if (ch == ']') {
  207.                     next();
  208.                     return a;
  209.                 }
  210.                 while (ch) {
  211.                     a.push(value());
  212.                     white();
  213.                     if (ch == ']') {
  214.                         next();
  215.                         return a;
  216.                     } else if (ch != ',') {
  217.                         break;
  218.                     }
  219.                     next();
  220.                     white();
  221.                 }
  222.             }
  223.             error("Bad array");
  224.         }
  225.  
  226.         function object() {
  227.             var k, o = {};
  228.  
  229.             if (ch == '{') {
  230.                 next();
  231.                 white();
  232.                 if (ch == '}') {
  233.                     next();
  234.                     return o;
  235.                 }
  236.                 while (ch) {
  237.                     k = string();
  238.                     white();
  239.                     if (ch != ':') {
  240.                         break;
  241.                     }
  242.                     next();
  243.                     o[k] = value();
  244.                     white();
  245.                     if (ch == '}') {
  246.                         next();
  247.                         return o;
  248.                     } else if (ch != ',') {
  249.                         break;
  250.                     }
  251.                     next();
  252.                     white();
  253.                 }
  254.             }
  255.             error("Bad object");
  256.         }
  257.  
  258.         function number() {
  259.             var n = '', v;
  260.             if (ch == '-') {
  261.                 n = '-';
  262.                 next();
  263.             }
  264.             while (ch >= '0' && ch <= '9') {
  265.                 n += ch;
  266.                 next();
  267.             }
  268.             if (ch == '.') {
  269.                 n += '.';
  270.                 while (next() && ch >= '0' && ch <= '9') {
  271.                     n += ch;
  272.                 }
  273.             }
  274.             if (ch == 'e' || ch == 'E') {
  275.                 n += 'e';
  276.                 next();
  277.                 if (ch == '-' || ch == '+') {
  278.                     n += ch;
  279.                     next();
  280.                 }
  281.                 while (ch >= '0' && ch <= '9') {
  282.                     n += ch;
  283.                     next();
  284.                 }
  285.             }
  286.             v = +n;
  287.             if (!isFinite(v)) {
  288.                 ////error("Bad number");
  289.             } else {
  290.                 return v;
  291.             }
  292.         }
  293.  
  294.         function word() {
  295.             switch (ch) {
  296.                 case 't':
  297.                     if (next() == 'r' && next() == 'u' && next() == 'e') {
  298.                         next();
  299.                         return true;
  300.                     }
  301.                     break;
  302.                 case 'f':
  303.                     if (next() == 'a' && next() == 'l' && next() == 's' &&
  304.                             next() == 'e') {
  305.                         next();
  306.                         return false;
  307.                     }
  308.                     break;
  309.                 case 'n':
  310.                     if (next() == 'u' && next() == 'l' && next() == 'l') {
  311.                         next();
  312.                         return null;
  313.                     }
  314.                     break;
  315.             }
  316.             error("Syntax error");
  317.         }
  318.  
  319.         function value() {
  320.             white();
  321.             switch (ch) {
  322.                 case '{':
  323.                     return object();
  324.                 case '[':
  325.                     return array();
  326.                 case '"':
  327.                     return string();
  328.                 case '-':
  329.                     return number();
  330.                 default:
  331.                     return ch >= '0' && ch <= '9' ? number() : word();
  332.             }
  333.         }
  334.  
  335.         return value();
  336.     }
  337. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement