Skip to content

Commit 39c483c

Browse files
authored
Merge pull request #122 from kyleladd/ArraySize
Array size
2 parents f85f770 + 25b51b1 commit 39c483c

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

extension/pages/options.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ <h2>Structure</h2>
7272
<li>
7373
<strong>"indentCStyle"</strong> - <span class="hint">Default false</span>
7474
</li>
75+
<li>
76+
<strong>"showArraySize"</strong> - <span class="hint">Default true</span>
77+
</li>
7578
</ul>
7679
</div>
7780
</header>

extension/src/json-viewer/jsl-format.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,41 @@ jsl.format = (function () {
1010
function repeat(s, count) {
1111
return new Array(count + 1).join(s);
1212
}
13-
13+
function getSizeOfArray(jsonString,startingPosition){
14+
var currentPosition = startingPosition + 1;
15+
var inString = false;
16+
var numOpened = 1;
17+
try{
18+
while (numOpened > 0 && currentPosition < jsonString.length) {
19+
var currentChar = jsonString.charAt(currentPosition)
20+
switch (currentChar) {
21+
case '[':
22+
if(!inString){
23+
numOpened++;
24+
}
25+
break;
26+
case ']':
27+
if(!inString){
28+
numOpened--;
29+
}
30+
break;
31+
case '"':
32+
inString = !inString;
33+
break;
34+
}
35+
currentPosition++;
36+
}
37+
return JSON.parse(jsonString.substring(startingPosition,currentPosition)).length;
38+
}
39+
catch(err){
40+
return null;
41+
}
42+
}
1443
function formatJson(json, options) {
1544
options = options || {};
1645
var tabSize = options.tabSize || 2;
1746
var indentCStyle = options.indentCStyle || false;
18-
47+
var showArraySize = (typeof options.showArraySize !== "undefined" ? Boolean(options.showArraySize) : true);
1948
var tab = "";
2049
for (var ts = 0; ts < tabSize; ts++) {
2150
tab += " ";
@@ -27,7 +56,6 @@ jsl.format = (function () {
2756
indentLevel = 0,
2857
inString = false,
2958
currentChar = null;
30-
3159
for (i = 0, il = json.length; i < il; i += 1) {
3260
currentChar = json.charAt(i);
3361

@@ -36,7 +64,17 @@ jsl.format = (function () {
3664
case '[':
3765
if (!inString) {
3866
if (indentCStyle) newJson += "\n" + repeat(tab, indentLevel);
39-
newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
67+
if(currentChar === "["){
68+
if(showArraySize){
69+
var arraySize = getSizeOfArray(json,i);
70+
if(arraySize !== null){
71+
newJson += "Array[" + arraySize + "]";
72+
}
73+
}
74+
}
75+
newJson += currentChar;
76+
77+
newJson += "\n" + repeat(tab, indentLevel + 1);
4078
indentLevel += 1;
4179
} else {
4280
newJson += currentChar;

extension/src/json-viewer/options/defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module.exports = {
1616
lineWrapping: true,
1717
foldGutter: true,
1818
tabSize: 2,
19-
indentCStyle: false
19+
indentCStyle: false,
20+
showArraySize:true
2021
},
2122
style: [
2223
".CodeMirror {",

0 commit comments

Comments
 (0)