Beispiel #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());
        }
Beispiel #2
0
        private static T Deserialize <T>(List <ParsingObject> objects)
        {
            T   result;
            var type = typeof(T);

            if (objects.Count == 1 && objects.First().Name == "")
            {
                if (objects.First().ObjType == ParsingObject.ObjectType.Primitive)
                {
                    return((T)Convert.ChangeType(objects.First().ContentInstance.Trim('\"'), type));
                }
                if (objects.First().ObjType == ParsingObject.ObjectType.Collection)
                {
                    if (ParserUtils.IsEnumerable(type))
                    {
                        return((T)GetEnumerableInstance(objects.First(), type));
                    }
                }
            }

            result = (T)Activator.CreateInstance(type);
            foreach (var obj in objects)
            {
                var name            = obj.Name.Trim('\"');
                var contentInstance = obj.ContentInstance.Trim('\"');
                var memberType      = ParserUtils.GetMemberType(type, name);
                if (obj.ObjType == ParsingObject.ObjectType.Primitive)
                {
                    object converted;
                    if (memberType.IsEnum)
                    {
                        converted = Enum.Parse(memberType, contentInstance);
                    }
                    else
                    {
                        converted = Convert.ChangeType(contentInstance, memberType);
                    }
                    ParserUtils.SetMemberContent(result, name, converted);
                }
                else if (obj.ObjType == ParsingObject.ObjectType.ComplexObject)
                {
                    var parsed = typeof(JsonParser)
                                 .GetMethod("Deserialize")
                                 .MakeGenericMethod(memberType)
                                 .Invoke(null, new object[] { Parse(contentInstance) });
                    ParserUtils.SetMemberContent(result, name, parsed);
                }
                else
                {
                    if (ParserUtils.IsEnumerable(memberType))
                    {
                        ParserUtils.SetMemberContent(result, name, GetEnumerableInstance(obj, memberType));
                    }
                    else
                    {
                        throw new Exception("Invalid deserialization");
                    }
                }
            }

            return(result);
        }
Beispiel #3
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());
        }