Last active
August 29, 2015 14:00
-
-
Save puncha/11205221 to your computer and use it in GitHub Desktop.
JSON Utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Runtime.Serialization.Json; | |
using System.Text; | |
namespace AcadCIAutoDeploy.Utils | |
{ | |
public static class JsonUtil | |
{ | |
public static TResult Parse<TResult>(Stream jsonStream, IEnumerable<Type> knownTypes = null) where TResult : class | |
{ | |
var serializer = new DataContractJsonSerializer(typeof(TResult), knownTypes ?? new Type[]{}); | |
return (serializer.ReadObject(jsonStream) as TResult); | |
} | |
public static TResult Parse<TResult>(String jsonString, IEnumerable<Type> knownTypes = null) where TResult : class | |
{ | |
MemoryStream mStream = new MemoryStream(); | |
using (StreamWriter wr = new StreamWriter(mStream)) | |
{ | |
wr.Write(jsonString); | |
wr.Flush(); | |
mStream.Seek(0, SeekOrigin.Begin); | |
var serializer = new DataContractJsonSerializer(typeof(TResult), knownTypes); | |
return (serializer.ReadObject(mStream) as TResult); | |
} | |
} | |
public static void Stringify(Stream stream, Object obj, IEnumerable<Type> knownTypes = null) | |
{ | |
var serializer = new DataContractJsonSerializer(obj.GetType(), knownTypes ?? new Type[] { }); | |
serializer.WriteObject(stream, obj); | |
} | |
public static String Stringify(Object obj, bool bFormat = false, IEnumerable<Type> knownTypes = null) | |
{ | |
MemoryStream memStream = new MemoryStream(); | |
StreamReader sr = null; | |
try | |
{ | |
Stringify(memStream, obj, knownTypes); | |
memStream.Seek(0, SeekOrigin.Begin); | |
sr = new StreamReader(memStream); | |
if (bFormat) | |
return new JsonFormatter(sr.ReadToEnd()).Format(); | |
else | |
return sr.ReadToEnd(); | |
} | |
finally | |
{ | |
if (sr != null) | |
sr.Dispose(); | |
else | |
memStream.Dispose(); | |
} | |
} | |
} | |
/// <summary> | |
/// From: http://www.limilabs.com/blog/json-net-formatter | |
/// </summary> | |
class JsonFormatter | |
{ | |
class StringWalker | |
{ | |
private readonly string _s; | |
public int Index { get; private set; } | |
public bool IsEscaped { get; private set; } | |
public char CurrentChar { get; private set; } | |
public StringWalker(string s) | |
{ | |
_s = s; | |
this.Index = -1; | |
} | |
public bool MoveNext() | |
{ | |
if (this.Index == _s.Length - 1) | |
return false; | |
if (IsEscaped == false) | |
IsEscaped = CurrentChar == '\\'; | |
else | |
IsEscaped = false; | |
this.Index++; | |
CurrentChar = _s[Index]; | |
return true; | |
} | |
}; | |
class IndentWriter | |
{ | |
private readonly StringBuilder _result = new StringBuilder(); | |
private int _indentLevel; | |
public void Indent() | |
{ | |
_indentLevel++; | |
} | |
public void UnIndent() | |
{ | |
if (_indentLevel > 0) | |
_indentLevel--; | |
} | |
public void WriteLine(string line) | |
{ | |
_result.AppendLine(CreateIndent() + line); | |
} | |
private string CreateIndent() | |
{ | |
StringBuilder indent = new StringBuilder(); | |
for (int i = 0; i < _indentLevel; i++) | |
indent.Append(" "); | |
return indent.ToString(); | |
} | |
public override string ToString() | |
{ | |
return _result.ToString(); | |
} | |
}; | |
private readonly StringWalker _walker; | |
private readonly IndentWriter _writer = new IndentWriter(); | |
private readonly StringBuilder _currentLine = new StringBuilder(); | |
private bool _quoted; | |
public JsonFormatter(string json) | |
{ | |
_walker = new StringWalker(json); | |
ResetLine(); | |
} | |
public void ResetLine() | |
{ | |
_currentLine.Length = 0; | |
} | |
public string Format() | |
{ | |
while (MoveNextChar()) | |
{ | |
if (this._quoted == false && this.IsOpenBracket()) | |
{ | |
this.WriteCurrentLine(); | |
this.AddCharToLine(); | |
this.WriteCurrentLine(); | |
_writer.Indent(); | |
} | |
else if (this._quoted == false && this.IsCloseBracket()) | |
{ | |
this.WriteCurrentLine(); | |
_writer.UnIndent(); | |
this.AddCharToLine(); | |
} | |
else if (this._quoted == false && this.IsColon()) | |
{ | |
this.AddCharToLine(); | |
this.WriteCurrentLine(); | |
} | |
else | |
{ | |
AddCharToLine(); | |
} | |
} | |
this.WriteCurrentLine(); | |
return _writer.ToString(); | |
} | |
private bool MoveNextChar() | |
{ | |
bool success = _walker.MoveNext(); | |
if (this.IsApostrophe()) | |
{ | |
this._quoted = !_quoted; | |
} | |
return success; | |
} | |
private bool IsApostrophe() | |
{ | |
return this._walker.CurrentChar == '"' && this._walker.IsEscaped == false; | |
} | |
private bool IsOpenBracket() | |
{ | |
return this._walker.CurrentChar == '{' | |
|| this._walker.CurrentChar == '['; | |
} | |
private bool IsCloseBracket() | |
{ | |
return this._walker.CurrentChar == '}' | |
|| this._walker.CurrentChar == ']'; | |
} | |
private bool IsColon() | |
{ | |
return this._walker.CurrentChar == ','; | |
} | |
private void AddCharToLine() | |
{ | |
this._currentLine.Append(_walker.CurrentChar); | |
} | |
private void WriteCurrentLine() | |
{ | |
string line = this._currentLine.ToString().Trim(); | |
if (line.Length > 0) | |
{ | |
_writer.WriteLine(line); | |
} | |
this.ResetLine(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment