aboutsummaryrefslogtreecommitdiff
path: root/src/js/lib/require/json.js
blob: 40e4da1df002eb2dd1864fea20867731e69330fd (plain)
  1. /** @license
  2. * RequireJS plugin for loading JSON files
  3. * - depends on Text plugin and it was HEAVILY "inspired" by it as well.
  4. * Author: Miller Medeiros
  5. * Version: 0.4.0 (2014/04/10)
  6. * Released under the MIT license
  7. *
  8. * Patched (2013/10/10):
  9. * - supports JS-like comments which are beginning from /* or //
  10. */
  11. define(['text'], function (text) {
  12. var CACHE_BUST_QUERY_PARAM = 'bust',
  13. CACHE_BUST_FLAG = '!bust',
  14. jsonParse = (typeof JSON !== 'undefined' && typeof JSON.parse === 'function') ? JSON.parse : function (val) {
  15. return eval('(' + val + ')'); //quick and dirty
  16. },
  17. PROTECTION_PREFIX = /^\)\]\}',?\n/,
  18. buildMap = {};
  19. function cacheBust(url) {
  20. url = url.replace(CACHE_BUST_FLAG, '');
  21. url += (url.indexOf('?') < 0) ? '?' : '&';
  22. return url + CACHE_BUST_QUERY_PARAM + '=' + Math.round(2147483647 * Math.random());
  23. }
  24. //API
  25. return {
  26. load: function(name, req, onLoad, config) {
  27. // Make sure file part of url ends with .json, add it if not
  28. name = name.replace(new RegExp("^[^?]*"), function(base) {
  29. return base.substr(-5) === ".json" ? base : base + ".json";
  30. });
  31. var url = req.toUrl(name);
  32. if (config.isBuild && (config.inlineJSON === false || name.indexOf(CACHE_BUST_QUERY_PARAM + '=') !== -1)) {
  33. //avoid inlining cache busted JSON or if inlineJSON:false
  34. onLoad(null);
  35. } else if (url.indexOf('empty:') === 0) {
  36. //and don't inline files marked as empty: urls
  37. onLoad(null);
  38. } else {
  39. text.get(url,
  40. function (data) {
  41. // Need to check if the JSON data has been formatted for the JSON array security vulnerability
  42. var cleaned_data = ('' + data).replace(PROTECTION_PREFIX, '');
  43. cleaned_data = cleaned_data.replace(/\/\*.+?\*\/|\/\/[^\n\r]*/g, '');
  44. var parsed = null;
  45. try {
  46. parsed = jsonParse(cleaned_data);
  47. if (config.isBuild) {
  48. buildMap[name] = parsed;
  49. }
  50. onLoad(parsed);
  51. } catch (e) {
  52. onLoad.error(e);
  53. //onLoad(null); -- should we really call onLoad???
  54. }
  55. },
  56. onLoad.error, {
  57. accept: 'application/json'
  58. }
  59. );
  60. }
  61. },
  62. normalize: function (name, normalize) {
  63. // used normalize to avoid caching references to a "cache busted" request
  64. if (name.indexOf(CACHE_BUST_FLAG) !== -1) {
  65. name = cacheBust(name);
  66. }
  67. // resolve any relative paths
  68. return normalize(name);
  69. },
  70. // write method based on RequireJS official text plugin by James Burke
  71. // https://github.com/jrburke/requirejs/blob/master/text.js
  72. write: function (pluginName, moduleName, write) {
  73. if (moduleName in buildMap) {
  74. var content = buildMap[moduleName];
  75. write('define("' + pluginName + '!' + moduleName + '", function () { return ' + (content ? JSON.stringify(content) : content) + '; });\n');
  76. }
  77. }
  78. };
  79. });