|
| 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 | +} |
0 commit comments