1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| #!/usr/bin/env node
|
| /**
| * Manual formatter taken straight from https://github.com/umbrae/jsonlintdotcom
| **/
|
| /*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 */
|
| /**
| * 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).
| *
| **/
| var formatter = (function () {
|
| function repeat(s, count) {
| return new Array(count + 1).join(s);
| }
|
| function formatJson(json, indentChars) {
| var i = 0,
| il = 0,
| tab = (typeof indentChars !== "undefined") ? indentChars : " ",
| 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 };
|
| }());
|
| if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
| exports.formatter = formatter;
| }
|
|