Karma-Read-JSON icon indicating copy to clipboard operation
Karma-Read-JSON copied to clipboard

Use of JSON5

Open rochejul opened this issue 8 years ago • 1 comments

Could we imagine the library use JSON5 (https://www.npmjs.com/package/json5) to allow more flexibility into the loaded JSON files ?

Many thanks

rochejul avatar Jul 10 '17 13:07 rochejul

Something like that:

var readJSON = function (url) {
  url = readJSON.base + url;

  var xhr = new XMLHttpRequest();
  var json = null;

  xhr.open('GET', url, false);

  xhr.onload = function (e) {
    if (xhr.status === 200) {
      json = typeof  JSON5 !== 'undefined' ? JSON5.parse(xhr.responseText) : JSON.parse(xhr.responseText);
    }

    else {
      console.error('readJSON', url, xhr.statusText);
    }
  };

  xhr.onerror = function (e) {
    console.error('readJSON', url, xhr.statusText);
  };

  xhr.send(null);
  return json;
};

readJSON.base = '/base/';

try {
  if (exports) {
    exports.readJSON = readJSON;
  }
}

catch (error) {
  //exports not available so not loaded by require
}

rochejul avatar Jul 10 '17 14:07 rochejul