Update JsonDebugger.cs

This commit is contained in:
Mikhail 2026-04-07 23:38:10 +08:00
parent f8e35f58c6
commit 1f263726f9

View File

@ -8,20 +8,43 @@ namespace DCFApixels.DragonECS.Core.Internal
{ {
internal static class JsonDebugger internal static class JsonDebugger
{ {
private readonly static List<string> _indentsChache = new List<string>();
internal static string ToJsonLog(object obj) internal static string ToJsonLog(object obj)
{ {
if (obj == null) return "null"; if (obj == null) return "null";
var sb = new StringBuilder(); var sb = new StringBuilder();
ToJsonLog(obj, sb, new HashSet<object>(), " ", ""); int linesCounter = 0;
return sb.ToString(); var visited = new Dictionary<object, int>();
ToJsonLog(ref linesCounter, obj, sb, visited, 0, 2);
string json = sb.ToString();
return json;
}
private static string GetIndentString(int count)
{
int newSize = count + 1;
while (newSize > _indentsChache.Count)
{
_indentsChache.Add(new string(' ', _indentsChache.Count));
}
return _indentsChache[count];
}
private static void NewLine(
ref int linesCounter,
StringBuilder sb,
int indent,
int indentStep)
{
sb.AppendLine();
sb.Append(GetIndentString(indent * indentStep));
linesCounter++;
} }
private static void ToJsonLog( private static void ToJsonLog(
ref int linesCounter,
object value, object value,
StringBuilder sb, StringBuilder sb,
HashSet<object> visited, Dictionary<object, int> visited,
string indent, int indent,
string currentIndent) int indentStep)
{ {
if (value == null) if (value == null)
{ {
@ -107,7 +130,7 @@ namespace DCFApixels.DragonECS.Core.Internal
sb.Append('"'); sb.Append('"');
return; return;
} }
if (typeof(Type).IsAssignableFrom(type) || if (value is Type ||
type.Namespace == typeof(FieldInfo).Namespace || type.Namespace == typeof(FieldInfo).Namespace ||
type.IsPointer || type.IsPointer ||
type.IsFunctionPointer || type.IsFunctionPointer ||
@ -142,36 +165,51 @@ namespace DCFApixels.DragonECS.Core.Internal
// как дописать приваильно тут вызов ToJsonLog ? // как дописать приваильно тут вызов ToJsonLog ?
} }
if (visited.Contains(value)) if(type.IsValueType == false)
{
if (visited.TryGetValue(value, out var line))
{ {
sb.Append('#'); sb.Append('#');
sb.Append(type.Name); sb.Append(type.Name);
sb.Append('#'); sb.Append('#');
sb.Append(line);
sb.Append('#');
return; return;
} }
visited.Add(value); visited.Add(value, linesCounter);
}
if (value is IEnumerable enumerable)
IEnumerable enumerable = value as IEnumerable;
if(enumerable != null)
{
try
{
enumerable.GetEnumerator();
}
catch (Exception)
{
enumerable = null;
}
}
if (enumerable != null)
{ {
sb.Append('['); sb.Append('[');
bool first = true; bool first = true;
string nextIndent = currentIndent + indent;
foreach (object item in enumerable) foreach (object item in enumerable)
{ {
if (!first) { sb.Append(','); } else { first = false; } if (!first) { sb.Append(','); } else { first = false; }
// Перенос строки и отступ перед элементом // Перенос строки и отступ перед элементом
sb.AppendLine(); NewLine(ref linesCounter, sb, indent + 1, indentStep);
sb.Append(nextIndent); ToJsonLog(ref linesCounter, item, sb, visited, indent + 1, indentStep);
ToJsonLog(item, sb, visited, indent, nextIndent);
} }
// Если были элементы, переносим строку перед закрывающей скобкой // Если были элементы, переносим строку перед закрывающей скобкой
if (!first) if (!first)
{ {
sb.AppendLine(); NewLine(ref linesCounter, sb, indent, indentStep);
sb.Append(currentIndent);
} }
sb.Append(']'); sb.Append(']');
} }
@ -179,25 +217,28 @@ namespace DCFApixels.DragonECS.Core.Internal
{ {
sb.Append('{'); sb.Append('{');
bool first = true; bool first = true;
string nextIndent = currentIndent + indent;
// Fields // Fields
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (var field in fields) foreach (var field in fields)
{ {
if (field.IsStatic) continue; if (field.IsStatic)
{
continue;
}
if (!first) { sb.Append(','); } else { first = false; } if (!first) { sb.Append(','); } else { first = false; }
sb.AppendLine(); NewLine(ref linesCounter, sb, indent + 1, indentStep);
sb.Append(nextIndent); //sb.AppendLine();
//sb.Append(nextIndent);
sb.Append('"'); sb.Append('"');
sb.Append(field.Name); sb.Append(field.Name);
sb.Append('"'); sb.Append('"');
sb.Append(':').Append(' '); sb.Append(':').Append(' ');
object fieldValue = field.GetValue(value); object fieldValue = field.GetValue(value);
ToJsonLog(fieldValue, sb, visited, indent, nextIndent); ToJsonLog(ref linesCounter, fieldValue, sb, visited, indent + 1, 2);
} }
// Properties // Properties
@ -207,12 +248,13 @@ namespace DCFApixels.DragonECS.Core.Internal
if (prop.GetIndexParameters().Length > 0 || if (prop.GetIndexParameters().Length > 0 ||
prop.GetMethod == null || prop.GetMethod == null ||
prop.GetMethod.IsStatic) prop.GetMethod.IsStatic)
{
continue; continue;
}
if (!first) { sb.Append(','); } else { first = false; } if (!first) { sb.Append(','); } else { first = false; }
sb.AppendLine(); NewLine(ref linesCounter, sb, indent + 1, indentStep);
sb.Append(nextIndent);
sb.Append('"'); sb.Append('"');
sb.Append(prop.Name); sb.Append(prop.Name);
sb.Append('"'); sb.Append('"');
@ -227,19 +269,18 @@ namespace DCFApixels.DragonECS.Core.Internal
{ {
propValue = cathcedE; propValue = cathcedE;
} }
ToJsonLog(propValue, sb, visited, indent, nextIndent); ToJsonLog(ref linesCounter, propValue, sb, visited, indent + 1, indentStep);
} }
// Если были поля/свойства, добавляем перевод строки перед закрывающей скобкой // Если были поля/свойства, добавляем перевод строки перед закрывающей скобкой
if (!first) if (!first)
{ {
sb.AppendLine(); NewLine(ref linesCounter, sb, indent, indentStep);
sb.Append(currentIndent);
} }
sb.Append('}'); sb.Append('}');
} }
visited.Remove(value); //visited.Remove(value);
} }
private static void EscapeString(string s, StringBuilder sb) private static void EscapeString(string s, StringBuilder sb)