mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
Created S# .sln and moved project to src folder
This commit is contained in:
79
ICD.Common/Utils/Json/JsonUtils.cs
Normal file
79
ICD.Common/Utils/Json/JsonUtils.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user