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

            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))
            {
                var isComplex = false;
                stringBuilderInstance = new StringBuilder("");
                var counter          = 0;
                var collectionLength = ParserUtils.CollectionLength((IEnumerable)obj);
                foreach (var subObj in (IEnumerable)obj)
                {
                    if (subObj == null)
                    {
                        continue;
                    }
                    var value = $"{Serialize(subObj, depth + 1)}";
                    if (value.Trim('\t', '\n', ' ').First() == '{')
                    {
                        isComplex = true;
                    }
                    if (counter < collectionLength - 1)
                    {
                        value  = value.TrimEnd('\n');
                        value += ',';
                    }

                    stringBuilderInstance.Append(value);
                    counter++;
                }

                if (isComplex)
                {
                    stringBuilderInstance.Insert(0, $"\n{new string('\t', depth)}[");
                    stringBuilderInstance.AppendLine();
                    stringBuilderInstance.Append($"{new string('\t', depth)}]");
                }
                else
                {
                    stringBuilderInstance.Insert(0, "[");
                    stringBuilderInstance.Append("]");
                }
            }
            else
            {
                stringBuilderInstance = 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(JsonIgnore)) != null || member.GetCustomAttribute(
                            typeof(ParseIgnore)) != null ||
                        member.GetCustomAttribute(
                            typeof(NonSerializedAttribute)) !=
                        null || ParserUtils.GetMemberValue(obj,
                                                           member.Name) == null)
                    {
                        continue;
                    }
                    var value = $"{Serialize(ParserUtils.GetMemberValue(obj, member.Name), depth + 1)}";
                    stringBuilderInstance.Append($"{new string('\t', depth + 1)}\"{member.Name}\" : {value}"
                                                 .TrimEnd('\n')); //           "name" : { field }
                    if (counter != length - 1)
                    {
                        stringBuilderInstance.Append(',');
                    }
                    stringBuilderInstance.AppendLine();
                    ++counter;
                }

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

            return(stringBuilderInstance.ToString());
        }