using System.Collections; using XFEExtension.NetCore.XFETransform.ObjectInfoAnalyzer; namespace XFEExtension.NetCore.XFETransform.StringConverter; /// /// 格式化Json转换器 /// public class FormattedJsonTransformer : StringConverter { /// /// 输出Json对象信息 /// /// /// public override string OutPutObject(IObjectInfo objectInfo) { var outPutString = string.Empty; var tabString = string.Empty; for (var k = 0; k < objectInfo.Layer; k++) { tabString += " "; } if (objectInfo.IsBasicType || objectInfo.ObjectPlace == ObjectPlace.Enum) { if (IsEnumerableMember(objectInfo)) { switch (objectInfo.Value) { case null: outPutString += $"{tabString}null"; break; case string: outPutString += $"{tabString}\"{objectInfo.Value}\""; break; default: { if (objectInfo.ObjectPlace == ObjectPlace.Enum) outPutString += $"{tabString}{(int)objectInfo.Value}"; else if (objectInfo.Value is bool) outPutString += $"{tabString}{objectInfo.Value.ToString()?.ToLower()}"; else outPutString += $"{tabString}\"{objectInfo.Value}\""; break; } } } else { switch (objectInfo.Value) { case null: outPutString += $"{tabString}\"{objectInfo.Name}\": null"; break; case string: outPutString += $"{tabString}\"{objectInfo.Name}\": \"{objectInfo.Value}\""; break; default: { if (objectInfo.ObjectPlace == ObjectPlace.Enum) outPutString += $"{tabString}\"{objectInfo.Name}\": {(int)objectInfo.Value}"; else if (objectInfo.Value is bool) outPutString += $"{tabString}\"{objectInfo.Name}\": {objectInfo.Value.ToString()?.ToLower()}"; else outPutString += $"{tabString}\"{objectInfo.Name}\": \"{objectInfo.Value}\""; break; } } } } else { if (objectInfo.Layer == 0) { if (objectInfo.IsArray) { outPutString += objectInfo.SubObjects is not null ? $""" [{OutPutSubObjects(objectInfo.SubObjects)} ] """ : "null"; } else { outPutString += objectInfo.SubObjects is not null ? $$""" {{{OutPutSubObjects(objectInfo.SubObjects)}} } """ : "null"; } } else { if (objectInfo.SubObjects is not null) outPutString += OutPutSubObjects(objectInfo.SubObjects); } } return outPutString; } /// /// 输出子对象的Json信息 /// /// /// public override string OutPutSubObjects(ISubObjects subObjects) { var outString = string.Empty; for (var i = 0; i < subObjects.Count; i++) { var obj = subObjects[i]; var tabString = string.Empty; for (var k = 0; k < obj.Layer; k++) { tabString += " "; } outString += "\n" + tabString; var currentConnectString = i == subObjects.Count - 1 ? "" : ","; if (obj.IsBasicType || obj.ObjectPlace == ObjectPlace.Enum) { outString += obj.OutPutObject(); outString += currentConnectString; } else { if (IsEnumerableClassMember(obj)) outString += obj.Value is not null ? $""" {tabString}"{obj.Name}": [{OutPutObject(obj)} {tabString}]{currentConnectString} """ : "null"; else if (IsEnumerableMember(obj)) if (obj.Value is not null && (obj.Type!.IsAssignableTo(typeof(IEnumerable)) || obj.Type.IsAssignableTo(typeof(Array)))) outString += obj.Value is not null ? $""" {tabString}[{OutPutObject(obj)} {tabString}]{currentConnectString} """ : "null"; else outString += obj.Value is not null ? $$""" {{tabString}}{{{OutPutObject(obj)}} {{tabString}}}{{currentConnectString}} """ : "null"; else outString += obj.Value is not null ? $$""" {{tabString}}"{{obj.Name}}": {{{tabString}}{{OutPutObject(obj)}} {{tabString}}}{{currentConnectString}} """ : "null"; } } if (outString.Length > 0 && outString[^1] == '\n') { outString = outString[..^1]; } return outString; } internal static bool IsEnumerable(IObjectInfo objectInfo) => objectInfo.ObjectPlace is ObjectPlace.Array or ObjectPlace.List; internal static bool IsEnumerableMember(IObjectInfo objectInfo) => objectInfo.ObjectPlace is ObjectPlace.ArrayMember or ObjectPlace.ListMember; internal static bool IsEnumerableClassMember(IObjectInfo objectInfo) => objectInfo.Value is not null && objectInfo.ObjectPlace == ObjectPlace.Property && (objectInfo.Type!.IsAssignableTo(typeof(IEnumerable)) || objectInfo.Type.IsAssignableTo(typeof(Array))); }