Fixed #129: code is sanitized before throwing an error

This commit is contained in:
jos 2014-09-13 10:04:55 +02:00
parent cc1b12effc
commit 330e5bb6d1
6 changed files with 79 additions and 52 deletions

View File

@ -3,6 +3,13 @@
https://github.com/josdejong/jsoneditor
## not yet released, version 3.1.3
- Before an error is thrown because of invalid text, the editor first tries to
sanitize the text (replace JavaScript notation with JSON notation), and only
after that throws the error.
## 2014-09-03, version 3.1.2
- Some fixes/improvements in `parseJS` (to parse a JSON object from a JavaScript

View File

@ -23,8 +23,8 @@
* Copyright (c) 2011-2014 Jos de Jong, http://jsoneditoronline.org
*
* @author Jos de Jong, <wjosdejong@gmail.com>
* @version 3.1.2
* @date 2014-09-03
* @version 3.1.3-SNAPSHOT
* @date 2014-09-13
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
@ -1264,16 +1264,18 @@ return /******/ (function(modules) { // webpackBootstrap
* Compact the code in the formatter
*/
textmode.compact = function () {
var json = util.parse(this.getText());
this.setText(JSON.stringify(json));
var json = this.get();
var text = JSON.stringify(json);
this.setText(text);
};
/**
* Format the code in the formatter
*/
textmode.format = function () {
var json = util.parse(this.getText());
this.setText(JSON.stringify(json, null, this.indentation));
var json = this.get();
var text = JSON.stringify(json, null, this.indentation);
this.setText(text);
};
/**
@ -1311,7 +1313,22 @@ return /******/ (function(modules) { // webpackBootstrap
* @return {Object} json
*/
textmode.get = function() {
return util.parse(this.getText());
var text = this.getText();
var json;
try {
json = util.parse(text); // this can throw an error
}
catch (err) {
// try to sanitize json, replace JavaScript notation with JSON notation
text = util.sanitize(text);
this.setText(text);
// try to parse again
json = util.parse(text); // this can throw an error
}
return json;
};
/**
@ -1379,30 +1396,23 @@ return /******/ (function(modules) { // webpackBootstrap
return JSON.parse(jsonString);
}
catch (err) {
// try to load as JavaScript instead of JSON (like "{a: 2}" instead of "{"a": 2}"
try {
return util.parseJS(jsonString);
}
catch(err2) {
// ok no luck loading as JavaScript
// try to throw a more detailed error message using validate
util.validate(jsonString);
// rethrow the original error
throw err;
}
}
};
/**
* Parse a string containing an object in JavaScript notation into a JSON.
* Throws an error when not successful. This function can for example parse
* a string like "{a: 2, 'b': {c: 'd'}".
* Sanitize a JSON-like string containing. For example changes JavaScript
* notation into JSON notation.
* This function for example changes a string like "{a: 2, 'b': {c: 'd'}"
* into '{"a": 2, "b": {"c": "d"}'
* @param {string} jsString
* @returns {JSON} json
* @returns {string} json
*/
util.parseJS = function (jsString) {
util.sanitize = function (jsString) {
// escape all single and double quotes inside strings
var chars = [];
var inString = false;
@ -1443,7 +1453,7 @@ return /******/ (function(modules) { // webpackBootstrap
return $1 + '"' + $2 + '"' + $3;
});
return JSON.parse(jsonString);
return jsonString;
};
/**

File diff suppressed because one or more lines are too long

6
jsoneditor.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -195,16 +195,18 @@ define(['./modeswitcher', './util'], function (modeswitcher, util) {
* Compact the code in the formatter
*/
textmode.compact = function () {
var json = util.parse(this.getText());
this.setText(JSON.stringify(json));
var json = this.get();
var text = JSON.stringify(json);
this.setText(text);
};
/**
* Format the code in the formatter
*/
textmode.format = function () {
var json = util.parse(this.getText());
this.setText(JSON.stringify(json, null, this.indentation));
var json = this.get();
var text = JSON.stringify(json, null, this.indentation);
this.setText(text);
};
/**
@ -242,7 +244,22 @@ define(['./modeswitcher', './util'], function (modeswitcher, util) {
* @return {Object} json
*/
textmode.get = function() {
return util.parse(this.getText());
var text = this.getText();
var json;
try {
json = util.parse(text); // this can throw an error
}
catch (err) {
// try to sanitize json, replace JavaScript notation with JSON notation
text = util.sanitize(text);
this.setText(text);
// try to parse again
json = util.parse(text); // this can throw an error
}
return json;
};
/**

View File

@ -14,30 +14,23 @@ define(function () {
return JSON.parse(jsonString);
}
catch (err) {
// try to load as JavaScript instead of JSON (like "{a: 2}" instead of "{"a": 2}"
try {
return util.parseJS(jsonString);
}
catch(err2) {
// ok no luck loading as JavaScript
// try to throw a more detailed error message using validate
util.validate(jsonString);
// rethrow the original error
throw err;
}
}
};
/**
* Parse a string containing an object in JavaScript notation into a JSON.
* Throws an error when not successful. This function can for example parse
* a string like "{a: 2, 'b': {c: 'd'}".
* Sanitize a JSON-like string containing. For example changes JavaScript
* notation into JSON notation.
* This function for example changes a string like "{a: 2, 'b': {c: 'd'}"
* into '{"a": 2, "b": {"c": "d"}'
* @param {string} jsString
* @returns {JSON} json
* @returns {string} json
*/
util.parseJS = function (jsString) {
util.sanitize = function (jsString) {
// escape all single and double quotes inside strings
var chars = [];
var inString = false;
@ -78,7 +71,7 @@ define(function () {
return $1 + '"' + $2 + '"' + $3;
});
return JSON.parse(jsonString);
return jsonString;
};
/**