标签:
1 public class StringWalker 2 { 3 private readonly string _s; 4 5 public int Index { get; private set; } 6 public bool IsEscaped { get; private set; } 7 public char CurrentChar { get; private set; } 8 9 public StringWalker(string s) 10 { 11 _s = s; 12 this.Index = -1; 13 } 14 15 public bool MoveNext() 16 { 17 if (this.Index == _s.Length - 1) 18 return false; 19 20 if (IsEscaped == false) 21 IsEscaped = CurrentChar == ‘\\‘; 22 else 23 IsEscaped = false; 24 this.Index++; 25 CurrentChar = _s[Index]; 26 return true; 27 } 28 }; 29 30 public class IndentWriter 31 { 32 private readonly StringBuilder _result = new StringBuilder(); 33 private int _indentLevel; 34 35 public void Indent() 36 { 37 _indentLevel++; 38 } 39 40 public void UnIndent() 41 { 42 if (_indentLevel > 0) 43 _indentLevel--; 44 } 45 46 public void WriteLine(string line) 47 { 48 _result.AppendLine(CreateIndent() + line); 49 } 50 51 private string CreateIndent() 52 { 53 StringBuilder indent = new StringBuilder(); 54 for (int i = 0; i < _indentLevel; i++) 55 indent.Append(" "); 56 return indent.ToString(); 57 } 58 59 public override string ToString() 60 { 61 return _result.ToString(); 62 } 63 }; 64 65 66 67 public class JsonFormatter 68 { 69 private readonly StringWalker _walker; 70 private readonly IndentWriter _writer = new IndentWriter(); 71 private readonly StringBuilder _currentLine = new StringBuilder(); 72 private bool _quoted; 73 74 public JsonFormatter(string json) 75 { 76 _walker = new StringWalker(json); 77 ResetLine(); 78 } 79 80 public void ResetLine() 81 { 82 _currentLine.Length = 0; 83 } 84 85 public string Format() 86 { 87 while (MoveNextChar()) 88 { 89 if (this._quoted == false && this.IsOpenBracket()) 90 { 91 this.WriteCurrentLine(); 92 this.AddCharToLine(); 93 this.WriteCurrentLine(); 94 _writer.Indent(); 95 } 96 else if (this._quoted == false && this.IsCloseBracket()) 97 { 98 this.WriteCurrentLine(); 99 _writer.UnIndent(); 100 this.AddCharToLine(); 101 } 102 else if (this._quoted == false && this.IsColon()) 103 { 104 this.AddCharToLine(); 105 this.WriteCurrentLine(); 106 } 107 else 108 { 109 AddCharToLine(); 110 } 111 } 112 this.WriteCurrentLine(); 113 return _writer.ToString(); 114 } 115 116 private bool MoveNextChar() 117 { 118 bool success = _walker.MoveNext(); 119 if (this.IsApostrophe()) 120 { 121 this._quoted = !_quoted; 122 } 123 return success; 124 } 125 126 public bool IsApostrophe() 127 { 128 return this._walker.CurrentChar == ‘"‘ && this._walker.IsEscaped == false; 129 } 130 131 public bool IsOpenBracket() 132 { 133 return this._walker.CurrentChar == ‘{‘ 134 || this._walker.CurrentChar == ‘[‘; 135 } 136 137 public bool IsCloseBracket() 138 { 139 return this._walker.CurrentChar == ‘}‘ 140 || this._walker.CurrentChar == ‘]‘; 141 } 142 143 public bool IsColon() 144 { 145 return this._walker.CurrentChar == ‘,‘; 146 } 147 148 private void AddCharToLine() 149 { 150 this._currentLine.Append(_walker.CurrentChar); 151 } 152 153 private void WriteCurrentLine() 154 { 155 string line = this._currentLine.ToString().Trim(); 156 if (line.Length > 0) 157 { 158 _writer.WriteLine(line); 159 } 160 this.ResetLine(); 161 } 162 };
测试代码:
1 Console.WriteLine(new JsonFormatter( 2 @"{""parameter"" : ""value"" , { ""parameter2"" : ""value2"" },{ ""parameter3"" : ""value3"" } }").Format()); 3 Console.WriteLine(new JsonFormatter( 4 @"{""parameter"":[""value1"",""value2"",""value3""] }").Format()); 5 Console.WriteLine(new JsonFormatter( 6 @"{""parameter"": ""value with {brackets}"" }").Format()); 7 Console.WriteLine(new JsonFormatter( 8 @"{ ""hello"" : ""value with quotes \""{brackets} and back slash: \\"" }").Format());
运行结果:
1 { 2 "parameter" : "value" , 3 { 4 "parameter2" : "value2" 5 }, 6 { 7 "parameter3" : "value3" 8 } 9 } 10 11 { 12 "parameter": 13 [ 14 "value1", 15 "value2", 16 "value3" 17 ] 18 } 19 20 { 21 "parameter": "value with {brackets}" 22 } 23 24 { 25 "hello" : "value with quotes \"{brackets} and back slash: \\" 26 }
标签:
原文地址:http://www.cnblogs.com/kuillldan/p/5000423.html