using System.Linq; using System.Text; using ICD.Common.Properties; using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.Json { /// /// Utility methods for working with JSON. /// [PublicAPI] public static class JsonUtils { /// /// Pretty-prints the JSON document. /// /// [PublicAPI] public static void Print(string json) { int indent = 0; bool quoted = false; StringBuilder sb = new StringBuilder(); for (int i = 0; i < json.Length; i++) { char ch = json[i]; switch (ch) { case '{': case '[': sb.Append(ch); if (!quoted) { sb.Append(IcdEnvironment.NewLine); Enumerable.Range(0, ++indent).ForEach(item => sb.Append('\t')); } break; case '}': case ']': if (!quoted) { sb.Append(IcdEnvironment.NewLine); Enumerable.Range(0, --indent).ForEach(item => sb.Append('\t')); } sb.Append(ch); break; case '"': sb.Append(ch); bool escaped = false; int index = i; while (index > 0 && json[--index] == '\\') escaped = !escaped; if (!escaped) quoted = !quoted; break; case ',': sb.Append(ch); if (!quoted) { sb.Append(IcdEnvironment.NewLine); Enumerable.Range(0, indent).ForEach(item => sb.Append('\t')); } break; case ':': sb.Append(ch); if (!quoted) sb.Append(" "); break; default: sb.Append(ch); break; } } IcdConsole.PrintLine(sb.ToString()); } } }