You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.5 KiB

1 month ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace Sog
{
public class JsonUtils
{
private static string TrimSpace(string json)
{
int i = 0, j = 0;
while (json[i] != '\0')
{
if (json[i] == ' ')
{
json.Replace(json[j++], json[i]);
}
i++;
}
return json;
}
private static string indent(int num)
{
string result = "";
for (int i = 0; i < num; i++)
{
result += " ";
}
return result;
}
public static string FormatJson(string json)
{
json.Trim(' ');
string result = "";
int length = json.Length;
int number = 0;
char key;
for (int i = 0; i < length; i++)
{
key = json[i];
if (key == '[' || key == '{')
{
if ((i - 1) > 0 && json[i - 1] == ':')
{
result += '\n';
result += indent(number);
}
result += key;
result += '\n';
number++;
result += indent(number);
continue;
}
if (key == ']' || key == '}')
{
if ((i - 1 >= 0) && (json[i - 1] == ']' || json[i - 1] == '}'))
{
number--;
result += indent(number);
result += key;
}
else
{
result += '\n';
number--;
result += indent(number);
result += key;
}
if (i + 1 < length && json[i + 1] != ',')
{
result += '\n';
}
continue;
}
if (key == ',')
{
result += key;
result += '\n';
result += indent(number);
continue;
}
result += key;
}
return result;
}
}
}