-
-
Save loverde/062f732dba8d29165db667348fb76601 to your computer and use it in GitHub Desktop.
Revisions
-
Jan Marsch renamed this gist
Apr 16, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Jan Marsch created this gist
Apr 16, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ var json = '...'; // your truncated json data here var chunk = json; var m, q = false; var stack = []; while (m = chunk.match(/[^\{\[\]\}"]*([\{\[\]\}"])/)) { switch (m[1]) { case '{': stack.push('}'); break; case '[': stack.push(']'); break; case '}': case ']': stack.pop(); break; case '"': if (!q) { q = true; stack.push('"'); } else { q = false; stack.pop(); } break; } chunk = chunk.substring(m[0].length); } if (chunk[chunk.length-1] === ':') { json += '""'; } while (stack.length) { json += stack.pop(); } // JSON.parse(json);