Skip to content

Commit 8b7a086

Browse files
committed
Added a PrettyPrint json formatter for more readable output
1 parent 05f2baa commit 8b7a086

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

TestStack.BDDfy/Processors/Diagnostics/DiagnosticsReporter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public string CreateJson(FileReportModel viewModel)
9090
}
9191

9292
var serializer = new JavaScriptSerializer();
93-
return serializer.Serialize(graph);
93+
string json = serializer.Serialize(graph);
94+
95+
return new JsonFormatter(json).Format();
9496
}
9597
}
9698
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace TestStack.BDDfy.Processors.Diagnostics
7+
{
8+
// http://www.limilabs.com/blog/json-net-formatter
9+
public class JsonFormatter
10+
{
11+
StringWalker _walker;
12+
IndentWriter _writer = new IndentWriter();
13+
StringBuilder _currentLine = new StringBuilder();
14+
bool _quoted;
15+
16+
public JsonFormatter(string json)
17+
{
18+
_walker = new StringWalker(json);
19+
ResetLine();
20+
}
21+
22+
public void ResetLine()
23+
{
24+
_currentLine.Length = 0;
25+
}
26+
27+
public string Format()
28+
{
29+
while (MoveNextChar())
30+
{
31+
if (this._quoted == false && this.IsOpenBracket())
32+
{
33+
this.WriteCurrentLine();
34+
this.AddCharToLine();
35+
this.WriteCurrentLine();
36+
_writer.Indent();
37+
}
38+
else if (this._quoted == false && this.IsCloseBracket())
39+
{
40+
this.WriteCurrentLine();
41+
_writer.UnIndent();
42+
this.AddCharToLine();
43+
}
44+
else if (this._quoted == false && this.IsColon())
45+
{
46+
this.AddCharToLine();
47+
this.WriteCurrentLine();
48+
}
49+
else
50+
{
51+
AddCharToLine();
52+
}
53+
}
54+
this.WriteCurrentLine();
55+
return _writer.ToString();
56+
}
57+
58+
private bool MoveNextChar()
59+
{
60+
bool success = _walker.MoveNext();
61+
if (this.IsApostrophe())
62+
{
63+
this._quoted = !_quoted;
64+
}
65+
return success;
66+
}
67+
68+
public bool IsApostrophe()
69+
{
70+
return this._walker.CharAtIndex() == '"';
71+
}
72+
73+
public bool IsOpenBracket()
74+
{
75+
return this._walker.CharAtIndex() == '{'
76+
|| this._walker.CharAtIndex() == '[';
77+
}
78+
79+
public bool IsCloseBracket()
80+
{
81+
return this._walker.CharAtIndex() == '}'
82+
|| this._walker.CharAtIndex() == ']';
83+
}
84+
85+
public bool IsColon()
86+
{
87+
return this._walker.CharAtIndex() == ',';
88+
}
89+
90+
private void AddCharToLine()
91+
{
92+
this._currentLine.Append(_walker.CharAtIndex());
93+
}
94+
95+
private void WriteCurrentLine()
96+
{
97+
string line = this._currentLine.ToString().Trim();
98+
if (line.Length > 0)
99+
{
100+
_writer.WriteLine(line);
101+
}
102+
this.ResetLine();
103+
}
104+
}
105+
106+
public class IndentWriter
107+
{
108+
StringBuilder _sb = new StringBuilder();
109+
int _indent;
110+
111+
public void Indent()
112+
{
113+
_indent++;
114+
}
115+
116+
public void UnIndent()
117+
{
118+
if (_indent > 0)
119+
_indent--;
120+
}
121+
122+
public void WriteLine(string line)
123+
{
124+
_sb.AppendLine(CreateIndent() + line);
125+
}
126+
127+
private string CreateIndent()
128+
{
129+
StringBuilder indentString = new StringBuilder();
130+
for (int i = 0; i < _indent; i++)
131+
indentString.Append(" ");
132+
return indentString.ToString();
133+
}
134+
135+
public override string ToString()
136+
{
137+
return _sb.ToString();
138+
}
139+
}
140+
141+
public class StringWalker
142+
{
143+
string _s;
144+
public int Index { get; set; }
145+
146+
public StringWalker(string s)
147+
{
148+
_s = s;
149+
Index = -1;
150+
}
151+
152+
public bool MoveNext()
153+
{
154+
if (Index == _s.Length - 1)
155+
return false;
156+
Index++;
157+
return true;
158+
}
159+
160+
public char CharAtIndex()
161+
{
162+
return _s[Index];
163+
}
164+
};
165+
}

TestStack.BDDfy/TestStack.BDDfy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="Core\IBatchProcessor.cs" />
7474
<Compile Include="Processors\ConsoleReporter.cs" />
7575
<Compile Include="Processors\Diagnostics\DiagnosticsReporter.cs" />
76+
<Compile Include="Processors\Diagnostics\JsonFormatter.cs" />
7677
<Compile Include="Processors\FileReportModel.cs" />
7778
<Compile Include="Processors\FileReportSummaryModel.cs" />
7879
<Compile Include="Processors\HtmlReporter\DefaultHtmlReportConfiguration.cs" />

0 commit comments

Comments
 (0)