jsol.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2010, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. (function () {
  32. /**
  33. JSOL stands for JavaScript Object Literal which is a string representing
  34. an object in JavaScript syntax.
  35. For example:
  36. {foo:"bar"} is equivalent to {"foo":"bar"} in JavaScript. Both are valid JSOL.
  37. Note that {"foo":"bar"} is proper JSON[1] therefore you can use one of the many
  38. JSON parsers out there like json2.js[2] or even the native browser's JSON parser,
  39. if available.
  40. However, {foo:"bar"} is NOT proper JSON but valid Javascript syntax for
  41. representing an object with one key, "foo" and its value, "bar".
  42. Using a JSON parser is not an option since this is NOT proper JSON.
  43. You can use JSOL.parse to safely parse any string that reprsents a JavaScript Object Literal.
  44. JSOL.parse will throw an Invalid JSOL exception on function calls, function declarations and variable references.
  45. Examples:
  46. JSOL.parse('{foo:"bar"}'); // valid
  47. JSOL.parse('{evil:(function(){alert("I\'m evil");})()}'); // invalid function calls
  48. JSOL.parse('{fn:function() { }}'); // invalid function declarations
  49. var bar = "bar";
  50. JSOL.parse('{foo:bar}'); // invalid variable references
  51. [1] http://www.json.org
  52. [2] http://www.json.org/json2.js
  53. */
  54. var trim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g; // Used for trimming whitespace
  55. var JSOL = {
  56. parse: function(text) {
  57. // make sure text is a "string"
  58. if (typeof text !== "string" || !text) {
  59. return null;
  60. }
  61. // Make sure leading/trailing whitespace is removed
  62. text = text.replace(trim, "");
  63. // Make sure the incoming text is actual JSOL (or Javascript Object Literal)
  64. // Logic borrowed from http://json.org/json2.js
  65. if ( /^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
  66. .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
  67. .replace(/(?:^|:|,)(?:\s*\[)+/g, ":")
  68. /** everything up to this point is json2.js **/
  69. /** this is the 5th stage where it accepts unquoted keys **/
  70. .replace(/\w*\s*\:/g, ":")) ) {
  71. return (new Function("return " + text))();
  72. }
  73. else {
  74. throw("Invalid JSOL: " + text);
  75. }
  76. }
  77. };
  78. if (typeof define === "function" && define.amd) {
  79. define(JSOL);
  80. } else if (typeof module === "object" && module.exports) {
  81. module.exports = JSOL;
  82. } else {
  83. this.JSOL = JSOL;
  84. }
  85. })();