static string Serialize(object obj, int depth, string key) { if (obj == null) { return(""); } Type type = obj.GetType(); StringBuilder sb; if (type.GetCustomAttribute(typeof(NonSerializedAttribute)) != null || type.GetCustomAttribute(typeof(XmlIgnore)) != null || type.GetCustomAttribute(typeof(ParseIgnore)) != null) { return(""); } if (type.IsPrimitive || type.IsEnum || type == typeof(string)) { key = (key == "") ? type.Name : key; return((new StringBuilder($"{new string('\t', depth)}<{key}>{obj}</{key}>\n")).ToString()); } if (ParserUtils.IsEnumerable(type)) { Type genericArgument = (type.GenericTypeArguments.Length == 0) ? type.GetElementType() : type.GenericTypeArguments[0]; if (key == "") { key = $"{type.Name}_{genericArgument.Name}"; } sb = new StringBuilder($"{new string('\t', depth)}<{key}>\n"); foreach (object el in (IEnumerable)obj) { sb.Append(Serialize(el, depth + 1, genericArgument.Name)); } sb.AppendLine($"{new string('\t', depth)}</{key}>"); } else { key = (key == "") ? type.Name : key; sb = new StringBuilder($"{new string('\t', depth)}<{key}>\n"); MemberInfo[] members = type.GetFields(); members = members.Concat(type.GetProperties()).ToArray(); int counter = 0; foreach (var member in members) { if (member.GetCustomAttribute(typeof(XmlIgnore)) != null || member.GetCustomAttribute(typeof(ParseIgnore)) != null || member.GetCustomAttribute(typeof(NonSerializedAttribute)) != null || ParserUtils.GetMemberValue(obj, member.Name) == null) { continue; } string value = Serialize(ParserUtils.GetMemberValue(obj, member.Name), depth + 1, member.Name); if (counter == members.Length - 1) { value = value.TrimEnd(new char[] { '\t', '\n', ' ' }); } sb.Append(value); counter++; } sb.AppendLine($"\n{new string('\t', depth)}</{key}>"); } return(sb.ToString()); }
static string Serialize(object obj, int depth) { if (obj == null) { return(""); } Type type = obj.GetType(); StringBuilder sb; if (type.GetCustomAttribute(typeof(JsonIgnore)) != null || type.GetCustomAttribute(typeof(ParseIgnore)) != null || type.GetCustomAttribute(typeof(NonSerializedAttribute)) != null) { return(""); } if (type.IsPrimitive || type.IsEnum) { return($"{obj}"); } if (type == typeof(string)) { return($"\"{obj}\""); } if (ParserUtils.IsEnumerable(type)) { bool isComplex = false; sb = new StringBuilder(""); int counter = 0; int collectionLength = ParserUtils.CollectionLength((IEnumerable)obj); foreach (var subObj in (IEnumerable)obj) { if (subObj == null) { continue; } string value = $"{Serialize(subObj, depth + 1)}"; if (value.Trim(new char[] { '\t', '\n', ' ' }).First() == '{') { isComplex = true; } if (counter < collectionLength - 1) { value = value.TrimEnd('\n'); value += ','; } sb.Append(value); counter++; } if (isComplex) { sb.Insert(0, $"\n{new string('\t', depth)}["); sb.AppendLine(); sb.Append($"{new string('\t', depth)}]"); } else { sb.Insert(0, "["); sb.Append("]"); } } else { sb = new StringBuilder($"\n{new string('\t', depth)}{{\n"); MemberInfo[] members = type.GetProperties(); members = members.Concat(type.GetFields()).ToArray(); int counter = 0; int length = members.Length; foreach (var member in members) { if (member.GetCustomAttribute(typeof(JsonIgnore)) != null || member.GetCustomAttribute(typeof(ParseIgnore)) != null || member.GetCustomAttribute(typeof(NonSerializedAttribute)) != null || ParserUtils.GetMemberValue(obj, member.Name) == null) { continue; } string value = $"{Serialize(ParserUtils.GetMemberValue(obj, member.Name), depth + 1)}"; sb.Append($"{new string('\t', depth + 1)}\"{member.Name}\" : {value}".TrimEnd('\n')); // "name" : { field } if (counter != length - 1) { sb.Append(','); } sb.AppendLine(); ++counter; } sb.Append($"{new string('\t', depth)}}}\n"); } return(sb.ToString()); }