Пример #1
0
        private static string Serialize(object obj, int depth)
        {
            if (obj == null)
            {
                return("");
            }
            var           type = obj.GetType();
            StringBuilder sb;

            if (type.GetCustomAttribute(typeof(NonSerializedAttribute)) != null)
            {
                return("");
            }

            if (type.IsPrimitive || type.IsEnum)
            {
                return($"{obj}");
            }
            if (type == typeof(string))
            {
                return($"\"{obj}\"");
            }
            if (ParserUtils.IsEnumerable(type))
            {
                var isComplex = false;
                sb = new StringBuilder("");
                var counter          = 0;
                var collectionLength = ParserUtils.CollectionLength((IEnumerable)obj);
                foreach (var subObj in (IEnumerable)obj)
                {
                    if (subObj == null)
                    {
                        continue;
                    }
                    var contetntInstance = $"{Serialize(subObj, depth + 1)}";
                    if (contetntInstance.Trim('\t', '\n', ' ').First() == '{')
                    {
                        isComplex = true;
                    }
                    if (counter < collectionLength - 1)
                    {
                        contetntInstance  = contetntInstance.TrimEnd('\n');
                        contetntInstance += ',';
                    }

                    sb.Append(contetntInstance);
                    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();
                var counter = 0;
                var length  = members.Length;
                foreach (var member in members)
                {
                    if (member.GetCustomAttribute(typeof(NonSerializedAttribute)) != null ||
                        ParserUtils.GetMemberContent(obj, member.Name) == null)
                    {
                        continue;
                    }
                    var content = $"{Serialize(ParserUtils.GetMemberContent(obj, member.Name), depth + 1)}";
                    sb.Append($"{new string('\t', depth + 1)}\"{member.Name}\" : {content}"
                              .TrimEnd('\n')); //           "Name" : { field }
                    if (counter != length - 1)
                    {
                        sb.Append(',');
                    }
                    sb.AppendLine();
                    ++counter;
                }

                sb.Append($"{new string('\t', depth)}}}\n");
            }

            return(sb.ToString());
        }
Пример #2
0
        private static string Serialize(object obj, int depth, string key)
        {
            if (obj == null)
            {
                return("");
            }
            var           type = obj.GetType();
            StringBuilder sb;

            if (type.GetCustomAttribute(typeof(NonSerializedAttribute)) != 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))
            {
                var 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 (var 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();
                var counter = 0;
                foreach (var member in members)
                {
                    if (member.GetCustomAttribute(typeof(NonSerializedAttribute)) != null ||
                        ParserUtils.GetMemberContent(obj, member.Name) == null)
                    {
                        continue;
                    }
                    var value = Serialize(ParserUtils.GetMemberContent(obj, member.Name), depth + 1, member.Name);
                    if (counter == members.Length - 1)
                    {
                        value = value.TrimEnd('\t', '\n', ' ');
                    }
                    sb.Append(value);
                    counter++;
                }

                sb.AppendLine($"\n{new string('\t', depth)}</{key}>");
            }

            return(sb.ToString());
        }