mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
80 lines
1.6 KiB
C#
80 lines
1.6 KiB
C#
using System.Linq;
|
|
using System.Text;
|
|
using ICD.Common.Properties;
|
|
using ICD.Common.Utils.Extensions;
|
|
|
|
namespace ICD.Common.Utils.Json
|
|
{
|
|
/// <summary>
|
|
/// Utility methods for working with JSON.
|
|
/// </summary>
|
|
[PublicAPI]
|
|
public static class JsonUtils
|
|
{
|
|
/// <summary>
|
|
/// Pretty-prints the JSON document.
|
|
/// </summary>
|
|
/// <param name="json"></param>
|
|
[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());
|
|
}
|
|
}
|
|
}
|