Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Reformat JSON even on failed parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
umbrae committed Jun 14, 2011
1 parent b5dc657 commit c628477
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
83 changes: 83 additions & 0 deletions c/js/jsl.format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: false, bitwise: true, newcap: true, maxerr: 50, indent: 4 */
var jsl = typeof jsl === 'undefined' ? {} : jsl;

/**
* jsl.format - Provide json reformatting in a character-by-character approach, so that even invalid JSON may be reformatted (to the best of its ability).
*
**/
jsl.format = (function () {

function repeat(s, count) {
return new Array(count + 1).join(s);
}

function formatJson(json) {
var i = 0,
il = 0,
tab = " ",
newJson = "",
indentLevel = 0,
inString = false,
currentChar = null;

for (i = 0, il = json.length; i < il; i += 1) {
currentChar = json.charAt(i);

switch (currentChar) {
case '{':
case '[':
if (!inString) {
newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
indentLevel += 1;
} else {
newJson += currentChar;
}
break;
case '}':
case ']':
if (!inString) {
indentLevel -= 1;
newJson += "\n" + repeat(tab, indentLevel) + currentChar;
} else {
newJson += currentChar;
}
break;
case ',':
if (!inString) {
newJson += ",\n" + repeat(tab, indentLevel);
} else {
newJson += currentChar;
}
break;
case ':':
if (!inString) {
newJson += ": ";
} else {
newJson += currentChar;
}
break;
case ' ':
case "\n":
case "\t":
if (inString) {
newJson += currentChar;
}
break;
case '"':
if (i > 0 && json.charAt(i - 1) !== '\\') {
inString = !inString;
}
newJson += currentChar;
break;
default:
newJson += currentChar;
break;
}
}

return newJson;
}

return { "formatJson": formatJson };

}());
21 changes: 18 additions & 3 deletions c/js/jsl.interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,24 @@ jsl.interactions = (function () {
} else {
alert("An unknown error occurred. Please contact Arc90.");
}
} catch (e) {
} catch (parseException) {

/**
* If we failed to validate, run our manual formatter and then re-validate so that we
* can get a better line number. On a successful validate, we don't want to run our
* manual formatter because the automatic one is faster and probably more reliable.
**/
try {
if (reformat) {
jsonVal = jsl.format.formatJson($('#json_input').val());
$('#json_input').val(jsonVal);
result = jsl.parser.parse($('#json_input').val());
}
} catch(e) {
parseException = e;
}

lineMatches = e.message.match(/line ([0-9]*)/);
lineMatches = parseException.message.match(/line ([0-9]*)/);
if (lineMatches && typeof lineMatches === "object" && lineMatches.length > 1) {
lineNum = parseInt(lineMatches[1], 10);

Expand All @@ -148,7 +163,7 @@ jsl.interactions = (function () {
$('#json_input').focus().caret(lineStart, lineEnd);
}

$('#results').text(e.message);
$('#results').text(parseException.message);

$('#results').removeClass('success').addClass('error');
$('div.linedwrap').removeClass('greenBorder').addClass('redBorder');
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<script src="c/js/jquery-linedtextarea/jquery-linedtextarea.js" type="text/javascript"></script>
<link href="c/js/jquery-linedtextarea/jquery-linedtextarea.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="c/js/jsl.parser.js"></script>
<script type="text/javascript" src="c/js/jsl.format.js"></script>
<script type="text/javascript" src="c/js/jsl.interactions.js"></script>
<link rel="stylesheet" href="c/css/blueprint/compressed/screen.css" type="text/css" media="screen, projection">
<!--[if IE]><link rel="stylesheet" href="c/css/blueprint/lib/ie.css" type="text/css" media="screen, projection"><![endif]-->
Expand Down
3 changes: 2 additions & 1 deletion jsonlint.manifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CACHE MANIFEST
CACHE:

# version: 4
# version: 5
c/css/blueprint/compressed/screen.css
c/css/blueprint/plugins/css-classes/css-classes.css
c/css/screen.css
Expand All @@ -13,6 +13,7 @@ c/js/jquery-linedtextarea/jquery-linedtextarea.css
c/js/jquery-linedtextarea/jquery-linedtextarea.js
c/js/jsl.interactions.js
c/js/jsl.parser.js
c/js/jsl.format.js

FALLBACK:

Expand Down

0 comments on commit c628477

Please sign in to comment.