Ejemplo n.º 1
0
 public string WriteObject(object o)
 {
     Xml.XmlDoc        doc         = new Utils.Xml.XmlDoc();
     Xml.XmlDocElement dataElement = doc["Data"];
     _defaultTypeSerializer.WriteObject(o, dataElement, null);
     return(doc.ToString());
 }
Ejemplo n.º 2
0
            public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                Type keyType   = null;
                Type valueType = null;

                if ((expectedType != null) && (expectedType.IsGenericType))
                {
                    keyType   = GenericArgumentLookup(expectedType, 0);
                    valueType = GenericArgumentLookup(expectedType, 1);
                    xmlElement.SetAttribute("keyType", keyType.AssemblyQualifiedNameWithoutVersion());
                    xmlElement.SetAttribute("valueType", valueType.AssemblyQualifiedNameWithoutVersion());
                }

                // Make sure the element gets created when there are zero elements in the list
                xmlElement.MakeSureElementExists();
                IDictionary dict = o as System.Collections.IDictionary;

                foreach (DictionaryEntry entry in dict)
                {
                    Xml.XmlDocElement itemElement = xmlElement.AddElement("Item");
                    Xml.XmlDocElement keyElement  = itemElement.AddElement("Key");
                    DefaultSerializer.WriteObject(entry.Key, keyElement, keyType);
                    Xml.XmlDocElement valueElement = itemElement.AddElement("Value");
                    DefaultSerializer.WriteObject(entry.Value, valueElement, valueType);
                }
            }
Ejemplo n.º 3
0
            public override void ReadObject(ref object result, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                if (result == null)
                {
                    throw new Exception("Cannot load an unknown type of dictionary");
                }

                Type keyType   = null;
                Type valueType = null;

                if ((expectedType != null) && (expectedType.IsGenericType))
                {
                    keyType   = GenericArgumentLookup(expectedType, 0);
                    valueType = GenericArgumentLookup(expectedType, 1);
                }

                IDictionary dictionary = result as IDictionary;

                foreach (Xml.XmlDocElement itemElement in xmlElement.GetChildElements("Item"))
                {
                    Xml.XmlDocElement keyElement   = itemElement["Key"];
                    Xml.XmlDocElement valueElement = itemElement["Value"];
                    if (keyElement.Exists && valueElement.Exists)
                    {
                        object key = null;
                        DefaultSerializer.ReadObject(ref key, keyElement, keyType);
                        object value = null;
                        DefaultSerializer.ReadObject(ref value, valueElement, valueType);

                        dictionary.Add(key, value);
                    }
                }
            }
Ejemplo n.º 4
0
 public Xml.XmlDoc WriteObjectToXmlDoc(object o)
 {
     Xml.XmlDoc doc = new Utils.Xml.XmlDoc();
     doc.AddXmlDeclaration("1.0", "utf-8", null);
     Xml.XmlDocElement rootElement = doc["XMLSerializer"];
     Xml.XmlDocElement dataElement = rootElement["Data"];
     _defaultTypeSerializer.WriteObject(o, dataElement, null);
     return(doc);
 }
Ejemplo n.º 5
0
        public object ReadObject(string xml)
        {
            Xml.XmlDoc        doc         = Utils.Xml.XmlDoc.FromString(xml);
            Xml.XmlDocElement rootElement = doc["XMLSerializer"];
            Xml.XmlDocElement dataElement = rootElement["Data"];
            object            result      = null;

            _defaultTypeSerializer.ReadObject(ref result, dataElement, null);
            return(result);
        }
Ejemplo n.º 6
0
        public XmlDocElement GetElementFromPath(string path)
        {
            XmlDocElement           element = null;
            XmlDocElementCollection parent  = this;

            string[] elementNames = path.Trim('/').Split('/');
            foreach (string elementName in elementNames)
            {
                element = parent[elementName];
                parent  = element;
            }

            return(element);
        }
Ejemplo n.º 7
0
        public object ReadObjectFromFile(string filename)
        {
            if (!File.Exists(filename))
            {
                return(null);
            }
            Xml.XmlDoc        doc         = new Utils.Xml.XmlDoc(filename);
            Xml.XmlDocElement rootElement = doc["XMLSerializer"];
            Xml.XmlDocElement dataElement = rootElement["Data"];
            object            result      = null;

            _defaultTypeSerializer.ReadObject(ref result, dataElement, null);
            return(result);
        }
Ejemplo n.º 8
0
            public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                Type itemType = null;

                if ((expectedType != null) && (expectedType.IsGenericType))
                {
                    itemType = GenericArgumentLookup(expectedType, 0);
                }

                // Make sure the element gets created when there are zero elements in the list
                xmlElement.MakeSureElementExists();
                foreach (object childValue in (o as System.Collections.IEnumerable))
                {
                    Xml.XmlDocElement childElement = xmlElement.AddElement("Item");
                    DefaultSerializer.WriteObject(childValue, childElement, itemType);
                }
            }
Ejemplo n.º 9
0
            public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType)
            {
                if (o == null)
                {
                    xmlElement.SetAttribute("null", "true");
                    return;
                }

                Type   type     = o.GetType();
                string typeName = null;

                string canRecreateTypeName;

                if (!_canRecreateLookup.TryGetValue(type, out canRecreateTypeName))
                {
                    canRecreateTypeName = typeName;
                    if (canRecreateTypeName == null)
                    {
                        canRecreateTypeName = type.AssemblyQualifiedNameWithoutVersion();
                    }
                    bool canRecreate = (Type.GetType(canRecreateTypeName) != null);
                    if (!canRecreate)
                    {
                        canRecreateTypeName = type.AssemblyQualifiedName;
                        canRecreate         = (Type.GetType(canRecreateTypeName) != null);
                        if (!canRecreate)
                        {
                            canRecreateTypeName = "";
                        }
                    }
                    _canRecreateLookup.Add(type, canRecreateTypeName);
                }
                if (canRecreateTypeName == "")
                {
                    throw new Exception("Could not recreate type from Assembly Qualified Name");
                }

                if (expectedType == null)
                {
                    xmlElement.SetAttribute("type", canRecreateTypeName);
                }

                if (o is DateTime)
                {
                    xmlElement.Value = ((DateTime)o).ToString("yyyy-MM-dd HH:mm:ss");
                    return;
                }

                if (o is IConvertible)
                {
                    xmlElement.Value = Convert.ChangeType(o, typeof(string)) as string;
                    return;
                }

                if (type.IsPrimitive)
                {
                    throw new Exception("Primitive that's not convertible");
                }

                if (type.IsArray)
                {
                    Type itemType = type.GetElementType();

                    Array array = o as Array;
                    foreach (object childValue in array)
                    {
                        Xml.XmlDocElement childElement = xmlElement.AddElement("Item");
                        WriteObject(childValue, childElement, itemType);
                    }
                    return;
                }

                for (; type != typeof(object); type = type.BaseType)
                {
                    foreach (BaseTypeSerializer typeSerializer in _typeSerializers)
                    {
                        if (typeSerializer.CanSerializeType(type))
                        {
                            typeSerializer.DefaultSerializer = this;
                            typeSerializer.WriteObject(o, xmlElement, type);
                            return;
                        }
                    }

                    FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                    foreach (FieldInfo field in fields)
                    {
                        object[] xmlIgnoreAttributes;
                        string   attributesKey = "ig:Field " + field.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = field.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        if (xmlIgnoreAttributes.Length > 0)
                        {
                            continue;
                        }

                        attributesKey = "igEmpty:Field " + field.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = field.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreIfNullOrEmptyAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        bool ignoreIfNullOrEmpty = (xmlIgnoreAttributes.Length > 0);

                        object childValue = field.GetValue(o);
                        if (ignoreIfNullOrEmpty)
                        {
                            if (childValue == null)
                            {
                                continue;
                            }
                            if ((childValue is string) && string.IsNullOrEmpty(childValue as string))
                            {
                                continue;
                            }
                        }

                        Xml.XmlDocElement childElement = xmlElement[field.Name];

                        if (childValue == o)
                        {
                            childElement.SetAttribute("selfReference", "true");
                        }
                        else
                        {
                            WriteObject(childValue, childElement, field.FieldType);
                        }
                    }

                    PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                    foreach (PropertyInfo property in properties)
                    {
                        if (!property.CanRead || !property.CanWrite)
                        {
                            continue;
                        }

                        object[] xmlIgnoreAttributes;
                        string   attributesKey = "ig:Property " + property.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = property.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        if (xmlIgnoreAttributes.Length > 0)
                        {
                            continue;
                        }

                        attributesKey = "igEmpty:Property " + property.ToString();
                        if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes))
                        {
                            xmlIgnoreAttributes = property.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreIfNullOrEmptyAttribute), true);
                            _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes);
                        }
                        bool ignoreIfNullOrEmpty = (xmlIgnoreAttributes.Length > 0);

                        object childValue = property.GetValue(o, null);
                        if (ignoreIfNullOrEmpty)
                        {
                            if (childValue == null)
                            {
                                continue;
                            }
                            if ((childValue is string) && string.IsNullOrEmpty(childValue as string))
                            {
                                continue;
                            }
                        }

                        Xml.XmlDocElement childElement = xmlElement[property.Name];

                        if (childValue == o)
                        {
                            childElement.SetAttribute("selfReference", "true");
                        }
                        else
                        {
                            WriteObject(childValue, childElement, property.PropertyType);
                        }
                    }
                }
            }
Ejemplo n.º 10
0
 public abstract void ReadObject(ref object o, Xml.XmlDocElement xmlElement, Type expectedType);
Ejemplo n.º 11
0
 public abstract void WriteObject(object o, Xml.XmlDocElement xmlElement, Type expectedType);