forked from josdejong/jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
62 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,8 @@ | |
* Copyright (c) 2011-2014 Jos de Jong, http://jsoneditoronline.org | ||
* | ||
* @author Jos de Jong, <[email protected]> | ||
* @version 3.2.0-SNAPSHOT | ||
* @date 2014-07-28 | ||
* @version 3.1.1 | ||
* @date 2014-08-01 | ||
*/ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
|
@@ -1372,6 +1372,7 @@ return /******/ (function(modules) { // webpackBootstrap | |
* Parse JSON using the parser built-in in the browser. | ||
* On exception, the jsonString is validated and a detailed error is thrown. | ||
* @param {String} jsonString | ||
* @return {JSON} json | ||
*/ | ||
util.parse = function parse(jsonString) { | ||
try { | ||
|
@@ -1380,7 +1381,7 @@ return /******/ (function(modules) { // webpackBootstrap | |
catch (err) { | ||
// try to load as JavaScript instead of JSON (like "{a: 2}" instead of "{"a": 2}" | ||
try { | ||
return eval('(' + jsonString + ')'); | ||
return util.parseJS(jsonString); | ||
} | ||
catch(err2) { | ||
// ok no luck loading as JavaScript | ||
|
@@ -1394,6 +1395,57 @@ return /******/ (function(modules) { // webpackBootstrap | |
} | ||
}; | ||
|
||
/** | ||
* 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'}". | ||
* @param {string} jsString | ||
* @returns {JSON} json | ||
*/ | ||
util.parseJS = function (jsString) { | ||
// escape quotes inside strings | ||
var chars = []; | ||
var inString = false; | ||
var i = 0; | ||
while(i < jsString.length) { | ||
var c = jsString.charAt(i); | ||
|
||
if (c === '"' || c === '\'') { | ||
if (c === inString) { | ||
// end of string | ||
inString = false; | ||
} | ||
else if (!inString) { | ||
// start of string | ||
inString = c; | ||
} | ||
else { | ||
// add escape character if not escaped | ||
if (jsString.charAt(i - 1) !== '\\') { | ||
chars.push('\\'); | ||
} | ||
} | ||
} | ||
|
||
chars.push(c); | ||
i++; | ||
} | ||
var jsonString = chars.join(''); | ||
|
||
// replace keys/values enclosed by single quotes with double quotes | ||
jsonString = jsonString.replace(/(.)'/g, function ($0, $1) { | ||
var str = $1.replace(/"/g, '\\"'); // escape double quotes | ||
return ($1 == '\\') ? '\'' : str + '"'; | ||
}); | ||
|
||
// enclose unquoted object keys with double quotes | ||
jsonString = jsonString.replace(/([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)(\s*:)/g, function ($0, $1, $2, $3) { | ||
return $1 + '"' + $2 + '"' + $3; | ||
}); | ||
|
||
return JSON.parse(jsonString); | ||
}; | ||
|
||
/** | ||
* Validate a string containing a JSON object | ||
* This method uses JSONLint to validate the String. If JSONLint is not | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters