hue.json.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. JSON.bigdataParse = (function () {
  17. "use strict";
  18. // This is a function that can parse a JSON text, producing a JavaScript
  19. // data structure. It is a simple, recursive descent parser. It does not use
  20. // eval or regular expressions, so it can be used as a model for implementing
  21. // a JSON parser in other languages.
  22. // We are defining the function inside of another function to avoid creating
  23. // global variables.
  24. var at, // The index of the current character
  25. ch, // The current character
  26. escapee = {
  27. '"': '"',
  28. '\\': '\\',
  29. '/': '/',
  30. b: '\b',
  31. f: '\f',
  32. n: '\n',
  33. r: '\r',
  34. t: '\t'
  35. },
  36. text,
  37. error = function (m) {
  38. // Call error when something is wrong.
  39. throw {
  40. name: 'SyntaxError',
  41. message: m,
  42. at: at,
  43. text: text
  44. };
  45. },
  46. next = function (c) {
  47. // If a c parameter is provided, verify that it matches the current character.
  48. if (c && c !== ch) {
  49. error("Expected '" + c + "' instead of '" + ch + "'");
  50. }
  51. // Get the next character. When there are no more characters,
  52. // return the empty string.
  53. ch = text.charAt(at);
  54. at += 1;
  55. return ch;
  56. },
  57. number = function () {
  58. // Parse a number value.
  59. var number,
  60. string = '';
  61. if (ch === '-') {
  62. string = '-';
  63. next('-');
  64. }
  65. while (ch >= '0' && ch <= '9') {
  66. string += ch;
  67. next();
  68. }
  69. if (ch === '.') {
  70. string += '.';
  71. while (next() && ch >= '0' && ch <= '9') {
  72. string += ch;
  73. }
  74. }
  75. if (ch === 'e' || ch === 'E') {
  76. string += ch;
  77. next();
  78. if (ch === '-' || ch === '+') {
  79. string += ch;
  80. next();
  81. }
  82. while (ch >= '0' && ch <= '9') {
  83. string += ch;
  84. next();
  85. }
  86. }
  87. number = +string;
  88. if (!isFinite(number)) {
  89. error("Bad number");
  90. } else {
  91. //if (number > 9007199254740992 || number < -9007199254740992)
  92. // Bignumber has stricter check: everything with length > 15 digits disallowed
  93. if (string.length > 15)
  94. return string;
  95. return number;
  96. }
  97. },
  98. string = function () {
  99. // Parse a string value.
  100. var hex,
  101. i,
  102. string = '',
  103. uffff;
  104. // When parsing for string values, we must look for " and \ characters.
  105. if (ch === '"') {
  106. while (next()) {
  107. if (ch === '"') {
  108. next();
  109. return string;
  110. }
  111. if (ch === '\\') {
  112. next();
  113. if (ch === 'u') {
  114. uffff = 0;
  115. for (i = 0; i < 4; i += 1) {
  116. hex = parseInt(next(), 16);
  117. if (!isFinite(hex)) {
  118. break;
  119. }
  120. uffff = uffff * 16 + hex;
  121. }
  122. string += String.fromCharCode(uffff);
  123. } else if (typeof escapee[ch] === 'string') {
  124. string += escapee[ch];
  125. } else {
  126. break;
  127. }
  128. } else {
  129. string += ch;
  130. }
  131. }
  132. }
  133. error("Bad string");
  134. },
  135. white = function () {
  136. // Skip whitespace.
  137. while (ch && ch <= ' ') {
  138. next();
  139. }
  140. },
  141. word = function () {
  142. // true, false, or null.
  143. switch (ch) {
  144. case 't':
  145. next('t');
  146. next('r');
  147. next('u');
  148. next('e');
  149. return true;
  150. case 'f':
  151. next('f');
  152. next('a');
  153. next('l');
  154. next('s');
  155. next('e');
  156. return false;
  157. case 'n':
  158. next('n');
  159. next('u');
  160. next('l');
  161. next('l');
  162. return null;
  163. }
  164. error("Unexpected '" + ch + "'");
  165. },
  166. value, // Place holder for the value function.
  167. array = function () {
  168. // Parse an array value.
  169. var array = [];
  170. if (ch === '[') {
  171. next('[');
  172. white();
  173. if (ch === ']') {
  174. next(']');
  175. return array; // empty array
  176. }
  177. while (ch) {
  178. array.push(value());
  179. white();
  180. if (ch === ']') {
  181. next(']');
  182. return array;
  183. }
  184. next(',');
  185. white();
  186. }
  187. }
  188. error("Bad array");
  189. },
  190. object = function () {
  191. // Parse an object value.
  192. var key,
  193. object = {};
  194. if (ch === '{') {
  195. next('{');
  196. white();
  197. if (ch === '}') {
  198. next('}');
  199. return object; // empty object
  200. }
  201. while (ch) {
  202. key = string();
  203. white();
  204. next(':');
  205. if (Object.hasOwnProperty.call(object, key)) {
  206. error('Duplicate key "' + key + '"');
  207. }
  208. object[key] = value();
  209. white();
  210. if (ch === '}') {
  211. next('}');
  212. return object;
  213. }
  214. next(',');
  215. white();
  216. }
  217. }
  218. error("Bad object");
  219. };
  220. value = function () {
  221. // Parse a JSON value. It could be an object, an array, a string, a number,
  222. // or a word.
  223. white();
  224. switch (ch) {
  225. case '{':
  226. return object();
  227. case '[':
  228. return array();
  229. case '"':
  230. return string();
  231. case '-':
  232. return number();
  233. default:
  234. return ch >= '0' && ch <= '9' ? number() : word();
  235. }
  236. };
  237. // Return the json_parse function. It will have access to all of the above
  238. // functions and variables.
  239. return function (source, reviver) {
  240. var result;
  241. text = source;
  242. at = 0;
  243. ch = ' ';
  244. result = value();
  245. white();
  246. if (ch) {
  247. error("Syntax error");
  248. }
  249. // If there is a reviver function, we recursively walk the new structure,
  250. // passing each name/value pair to the reviver function for possible
  251. // transformation, starting with a temporary root object that holds the result
  252. // in an empty key. If there is not a reviver function, we simply return the
  253. // result.
  254. return typeof reviver === 'function'
  255. ? (function walk(holder, key) {
  256. var k, v, value = holder[key];
  257. if (value && typeof value === 'object') {
  258. for (k in value) {
  259. if (Object.prototype.hasOwnProperty.call(value, k)) {
  260. v = walk(value, k);
  261. if (v !== undefined) {
  262. value[k] = v;
  263. } else {
  264. delete value[k];
  265. }
  266. }
  267. }
  268. }
  269. return reviver.call(holder, key, value);
  270. }({'': result}, ''))
  271. : result;
  272. };
  273. }());