Exemplo n.º 1
0
        public static object DeserializeField(Type type, AldNode data)
        {
            object obj = null;

            if (data.Type != string.Empty)
            {
                type = NameToType(data.Type);
            }
            bool expand = type.IsDefined(expandAttributeType, true);

            if (IsGenericList(type))
            {
                Type  genericArg = (type.GetGenericArguments().Length == 0) ? type.GetElementType() : type.GetGenericArguments()[0];
                IList list       = (IList)Activator.CreateInstance(type, new object[] { data.ArrayLength });
                for (int i = 0; i < data.ArrayLength; i++)
                {
                    if (list.IsFixedSize)
                    {
                        list[i] = DeserializeField(genericArg, data[i]);
                    }
                    else
                    {
                        list.Insert(i, DeserializeField(genericArg, data[i]));
                    }
                }
                obj = list;
            }
            else if (IsGenericDictionary(type))
            {
                Type        genericArg0 = type.GetGenericArguments()[0];
                Type        genericArg1 = type.GetGenericArguments()[1];
                IDictionary dict        = (IDictionary)Activator.CreateInstance(type);
                foreach (AldNode node in data)
                {
                    dict[((IConvertible)node.Key).ToType(genericArg0, null)] = DeserializeField(genericArg1, node);
                }
                obj = dict;
            }
            else if (!type.IsValueType && data.Value == string.Empty && data.ChildCount == 0)
            {
                obj = null;
            }
            else if (((AldSettings.AutoSerializeStructs || expand) && type.IsValueType && !type.IsPrimitive && !type.IsEnum) || (type.IsClass && expand))
            {
                obj = Deserialize(type, data);
            }
            else if (type.IsEnum)
            {
                obj = Enum.Parse(type, data.Value);
            }
            else
            {
                obj = Convert.ChangeType(data.Value, type);
            }
            return(obj);
        }
Exemplo n.º 2
0
        public static AldNode ParseString(string input)
        {
            AldNode root = new AldNode();

            input = input.Replace(AldSettings.LineSeperator, "\n");
            input = input.Replace("\r\n", "\n");
            string[] lines = input.Split('\n');
            root.ParseLines(lines, 0);
            return(root);
        }
Exemplo n.º 3
0
        public static AldNode SerializeField(string key, object obj)
        {
            AldNode data = new AldNode(key, string.Empty);

            if (obj == null)
            {
                return(data);
            }
            Type type = obj.GetType();

            if (!type.IsValueType && !type.IsSealed)
            {
                data.Type = TypeToName(type);
            }
            bool expand = type.IsDefined(expandAttributeType, true);

            if (obj is IDictionary)
            {
                IDictionary dict      = obj as IDictionary;
                Hashtable   hashtable = new Hashtable(dict);
                foreach (DictionaryEntry pair in hashtable)
                {
                    data.Add(SerializeField(pair.Key.ToString(), pair.Value));
                }
            }
            else if (obj is IList)
            {
                IList list = obj as IList;
                for (int i = 0; i < list.Count; i++)
                {
                    data.Add(SerializeField(i.ToString(), list[i]));
                }
            }
            else if (obj is DateTime)
            {
                data.Value = obj.ToString();
            }
            else if (((AldSettings.AutoSerializeStructs || expand) && type.IsValueType && !type.IsPrimitive && !type.IsEnum) || (type.IsClass && expand))
            {
                data = Serialize(key, obj);
            }
            else
            {
                data.Value = obj.ToString();
            }
            return(data);
        }
Exemplo n.º 4
0
        public static object Deserialize(Type type, AldNode data)
        {
            object obj = Activator.CreateInstance(type);

            PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (PropertyInfo prop in props)
            {
                if (!prop.IsDefined(serializeAttributeType, true))
                {
                    continue;
                }
                if (prop.IsDefined(ignoreAttributeType, true))
                {
                    continue;
                }
                if (data.ContainsKey(prop.Name))
                {
                    AldNode propData = data[prop.Name];
                    Type    pType    = prop.PropertyType;
                    prop.SetValue(obj, DeserializeField(pType, propData), null);
                }
            }
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo field in fields)
            {
                if (!field.IsPublic && !field.IsDefined(serializeAttributeType, true))
                {
                    continue;
                }
                if (field.IsDefined(ignoreAttributeType, true))
                {
                    continue;
                }
                if (data.ContainsKey(field.Name))
                {
                    AldNode fieldData = data[field.Name];
                    Type    fType     = field.FieldType;
                    field.SetValue(obj, DeserializeField(fType, fieldData));
                }
            }
            return(obj);
        }
Exemplo n.º 5
0
        public static AldNode Serialize(string key, object obj)
        {
            AldNode data = new AldNode(key, string.Empty);
            Type    type = obj.GetType();

            if (!type.IsValueType && !type.IsSealed)
            {
                data.Type = TypeToName(type);
            }
            PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (PropertyInfo prop in props)
            {
                if (!prop.IsDefined(serializeAttributeType, true))
                {
                    continue;
                }
                if (prop.IsDefined(ignoreAttributeType, true))
                {
                    continue;
                }
                object value = prop.GetValue(obj, null);
                data.Add(SerializeField(prop.Name, value));
            }
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo field in fields)
            {
                if (!field.IsPublic && !field.IsDefined(serializeAttributeType, true))
                {
                    continue;
                }
                if (field.IsDefined(ignoreAttributeType, true))
                {
                    continue;
                }
                object value = field.GetValue(obj);
                data.Add(SerializeField(field.Name, value));
            }
            return(data);
        }
Exemplo n.º 6
0
        public int ParseLines(string[] lines, int i)
        {
            AldNode n         = null;
            int     autoIndex = 0;

            while (i < lines.Length)
            {
                if (lines[i] == string.Empty)
                {
                    i++;
                    continue;
                }
                int lineDepth = StringDepth(lines[i]);
                if (lineDepth == Depth + 1)
                {
                    n = Import(lines[i]);
                    if (n.Key == AldSettings.AutoArrayKey)
                    {
                        n.Key = autoIndex.ToString();
                        autoIndex++;
                    }
                    n.Parent = this;
                }
                else if (lineDepth > Depth + 1)
                {
                    if (n != null)
                    {
                        i = n.ParseLines(lines, i) - 1;
                    }
                }
                else
                {
                    break;
                }
                i++;
            }
            _ArrayLength = Math.Max(_ArrayLength, autoIndex);
            return(i);
        }
Exemplo n.º 7
0
        public static AldNode Import(string input)
        {
            AldNode node = new AldNode();

            input = input.Replace(new string(AldSettings.IndentCharacter, AldSettings.IndentCount), string.Empty);
            if (input.Contains(AldSettings.KeyValueSeperator))
            {
                string[] bits = input.Split(new string[] { AldSettings.KeyValueSeperator }, StringSplitOptions.None);
                node.Key   = bits[0];
                node.Value = bits[1];
            }
            else
            {
                node.Key   = input;
                node.Value = string.Empty;
            }
            if (node.Key.Contains(AldSettings.KeyTypeSeperator))
            {
                string[] bits = node.Key.Split(new string[] { AldSettings.KeyTypeSeperator }, StringSplitOptions.None);
                node.Key  = bits[0];
                node.Type = bits[1];
            }
            return(node);
        }
Exemplo n.º 8
0
 public static T Deserialize <T>(AldNode data)
 {
     return((T)Deserialize(typeof(T), data));
 }
Exemplo n.º 9
0
 public bool Contains(AldNode node)
 {
     return(_NodeDict.ContainsValue(node));
 }
Exemplo n.º 10
0
 public AldNode Add(AldNode node)
 {
     node.Parent = this;
     return(node);
 }
Exemplo n.º 11
0
 public AldNode Remove(AldNode node)
 {
     node.Parent = null;
     return(node);
 }
Exemplo n.º 12
0
		public static AldNode ParseString(string input) {
			AldNode root = new AldNode();
			input = input.Replace(AldSettings.LineSeperator, "\n");
			input = input.Replace("\r\n", "\n");
			string[] lines = input.Split('\n');
			root.ParseLines(lines, 0);
			return root;
		}
Exemplo n.º 13
0
		public static AldNode Import(string input) {
			AldNode node = new AldNode();
			input = input.Replace(new string(AldSettings.IndentCharacter, AldSettings.IndentCount), string.Empty);
			if (input.Contains(AldSettings.KeyValueSeperator)) {
				string[] bits = input.Split(new string[] { AldSettings.KeyValueSeperator }, StringSplitOptions.None);
				node.Key = bits[0];
				node.Value = bits[1];
			} else {
				node.Key = input;
				node.Value = string.Empty;
			}
			if (node.Key.Contains(AldSettings.KeyTypeSeperator)) {
				string[] bits = node.Key.Split(new string[] { AldSettings.KeyTypeSeperator }, StringSplitOptions.None);
				node.Key = bits[0];
				node.Type = bits[1];
			}
			return node;
		}
Exemplo n.º 14
0
		public bool Contains(AldNode node) {
			return _NodeDict.ContainsValue(node);
		}
Exemplo n.º 15
0
		public AldNode Remove(AldNode node) {
			node.Parent = null;
			return node;
		}
Exemplo n.º 16
0
		public AldNode Add(AldNode node) {
			node.Parent = this;
			return node;
		}