private XElement SerializeMemberType(PropertyInfo prop, SerializeAsAttribute globalOptions, object obj)
        {
            string name     = prop.Name;
            Type   propType = prop.PropertyType;
            SerializeAsAttribute settings = prop.GetAttribute <SerializeAsAttribute>();

            if (settings != null)
            {
                name = settings.Name.HasValue()
                ? settings.Name
                : name;
            }

            SerializeAsAttribute options = prop.GetAttribute <SerializeAsAttribute>();

            if (options != null)
            {
                name = options.TransformName(name);
            }
            else if (globalOptions != null)
            {
                name = globalOptions.TransformName(name);
            }

            return(new XElement("member", new XElement("name", name), SerializeValueType(obj)));
        }
        private XElement SerializeStructType(object obj)
        {
            Type objType = obj.GetType();
            IEnumerable <PropertyInfo> props = from p in objType.GetProperties()
                                               let indexAttribute = p.GetAttribute <SerializeAsAttribute>()
                                                                    where p.CanRead && p.CanWrite
                                                                    orderby indexAttribute == null
                                               ? int.MaxValue
                                               : indexAttribute.Index
                                                                    select p;
            SerializeAsAttribute globalOptions = objType.GetAttribute <SerializeAsAttribute>();

            var structElement = new XElement("struct");

            foreach (PropertyInfo prop in props)
            {
                object rawValue = prop.GetValue(obj, null);

                if (rawValue == null)
                {
                    continue;
                }
                structElement.Add(SerializeMemberType(prop, globalOptions, rawValue));
            }
            return(structElement);
        }
Exemple #3
0
        /// <summary>
        /// Serialize the object as XML
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>XML as string</returns>
        public string Serialize(object obj)
        {
            XDocument            doc     = new XDocument();
            Type                 t       = obj.GetType();
            string               name    = t.Name;
            SerializeAsAttribute options = t.GetAttribute <SerializeAsAttribute>();

            if (options != null)
            {
                name = options.TransformName(options.Name ?? name);
            }

            XElement root = new XElement(name.AsNamespaced(this.Namespace));

            if (obj is IList)
            {
                string itemTypeName = "";

                foreach (object item in (IList)obj)
                {
                    Type type = item.GetType();
                    SerializeAsAttribute opts = type.GetAttribute <SerializeAsAttribute>();

                    if (opts != null)
                    {
                        itemTypeName = opts.TransformName(opts.Name ?? name);
                    }

                    if (itemTypeName == "")
                    {
                        itemTypeName = type.Name;
                    }

                    XElement instance = new XElement(itemTypeName.AsNamespaced(this.Namespace));

                    this.Map(instance, item);
                    root.Add(instance);
                }
            }
            else
            {
                this.Map(root, obj);
            }

            if (this.RootElement.HasValue())
            {
                XElement wrapper = new XElement(this.RootElement.AsNamespaced(this.Namespace), root);
                doc.Add(wrapper);
            }
            else
            {
                doc.Add(root);
            }

            return(doc.ToString());
        }
        public string Serialize(object obj)
        {
            XDocument            xDocument = new XDocument();
            Type                 type      = obj.GetType();
            string               text      = type.Name;
            SerializeAsAttribute attribute = type.GetAttribute <SerializeAsAttribute>();

            if (attribute != null)
            {
                text = attribute.TransformName(attribute.Name ?? text);
            }
            XElement xElement = new XElement(text.AsNamespaced(Namespace));

            if (obj is IList)
            {
                string text2 = "";
                foreach (object item in (IList)obj)
                {
                    Type type2 = item.GetType();
                    SerializeAsAttribute attribute2 = type2.GetAttribute <SerializeAsAttribute>();
                    if (attribute2 != null)
                    {
                        text2 = attribute2.TransformName(attribute2.Name ?? text);
                    }
                    if (text2 == "")
                    {
                        text2 = type2.Name;
                    }
                    XElement xElement2 = new XElement(text2.AsNamespaced(Namespace));
                    Map(xElement2, item);
                    xElement.Add(xElement2);
                }
            }
            else
            {
                Map(xElement, obj);
            }
            if (RootElement.HasValue())
            {
                XElement content = new XElement(RootElement.AsNamespaced(Namespace), xElement);
                xDocument.Add(content);
            }
            else
            {
                xDocument.Add(xElement);
            }
            return(xDocument.ToString());
        }
Exemple #5
0
        private void Map(XContainer root, object obj)
        {
            Type objType = obj.GetType();
            var  props   = from p in objType.GetProperties()
                           let indexAttribute = p.GetAttribute <SerializeAsAttribute>()
                                                where p.CanRead && p.CanWrite
                                                orderby indexAttribute?.Index ?? int.MaxValue
                                                select p;
            SerializeAsAttribute globalOptions   = objType.GetAttribute <SerializeAsAttribute>();
            bool textContentAttributeAlreadyUsed = false;

            foreach (var prop in props)
            {
                var name     = prop.Name;
                var rawValue = prop.GetValue(obj, null);

                if (rawValue == null)
                {
                    continue;
                }

                string value                 = this.GetSerializedValue(rawValue);
                Type   propType              = prop.PropertyType;
                bool   useAttribute          = false;
                bool   setTextContent        = false;
                SerializeAsAttribute options = prop.GetAttribute <SerializeAsAttribute>();

                if (options != null)
                {
                    name = options.Name.HasValue()
                        ? options.Name
                        : name;

                    name = options.TransformName(name);

                    useAttribute = options.Attribute;

                    setTextContent = options.Content;

                    if (textContentAttributeAlreadyUsed && setTextContent)
                    {
                        throw new ArgumentException("Class cannot have two properties marked with " +
                                                    "SerializeAs(Content = true) attribute.");
                    }

                    textContentAttributeAlreadyUsed |= setTextContent;
                }
                else if (globalOptions != null)
                {
                    name = globalOptions.TransformName(name);
                }

                var nsName  = name.AsNamespaced(Namespace);
                var element = new XElement(nsName);
                if (propType.GetTypeInfo().IsPrimitive || propType.GetTypeInfo().IsValueType ||
                    propType == typeof(string))
                {
                    if (useAttribute)
                    {
                        root.Add(new XAttribute(name, value));
                        continue;
                    }
                    else if (setTextContent)
                    {
                        root.Add(new XText(value));
                        continue;
                    }

                    element.Value = value;
                }
                else if (rawValue is IList)
                {
                    var itemTypeName = "";

                    foreach (var item in (IList)rawValue)
                    {
                        if (itemTypeName == "")
                        {
                            var type    = item.GetType();
                            var setting = type.GetAttribute <SerializeAsAttribute>();

                            itemTypeName = setting != null && setting.Name.HasValue()
                                ? setting.Name
                                : type.Name;
                        }

                        var instance = new XElement(itemTypeName.AsNamespaced(Namespace));

                        Map(instance, item);
                        element.Add(instance);
                    }
                }
                else
                {
                    Map(element, rawValue);
                }

                root.Add(element);
            }
        }
        private void Map(XElement root, object obj)
        {
            Type type = obj.GetType();
            IEnumerable <PropertyInfo> enumerable = from p in type.GetProperties()
                                                    let indexAttribute = p.GetAttribute <SerializeAsAttribute>()
                                                                         where p.CanRead && p.CanWrite
                                                                         orderby(indexAttribute == null) ? 2147483647 : indexAttribute.Index
                                                                         select p;

            SerializeAsAttribute attribute = type.GetAttribute <SerializeAsAttribute>();

            foreach (PropertyInfo item in enumerable)
            {
                string text  = item.Name;
                object value = item.GetValue(obj, null);
                if (value != null)
                {
                    string serializedValue          = GetSerializedValue(value);
                    Type   propertyType             = item.PropertyType;
                    bool   flag                     = false;
                    SerializeAsAttribute attribute2 = item.GetAttribute <SerializeAsAttribute>();
                    if (attribute2 != null)
                    {
                        text = (attribute2.Name.HasValue() ? attribute2.Name : text);
                        flag = attribute2.Attribute;
                    }
                    SerializeAsAttribute attribute3 = item.GetAttribute <SerializeAsAttribute>();
                    if (attribute3 != null)
                    {
                        text = attribute3.TransformName(text);
                    }
                    else if (attribute != null)
                    {
                        text = attribute.TransformName(text);
                    }
                    XName    name     = text.AsNamespaced(Namespace);
                    XElement xElement = new XElement(name);
                    if (propertyType.IsPrimitive || propertyType.IsValueType || propertyType == typeof(string))
                    {
                        if (flag)
                        {
                            root.Add(new XAttribute(text, serializedValue));
                            continue;
                        }
                        xElement.Value = serializedValue;
                    }
                    else if (value is IList)
                    {
                        string text2 = "";
                        foreach (object item2 in (IList)value)
                        {
                            if (text2 == "")
                            {
                                Type type2 = item2.GetType();
                                SerializeAsAttribute attribute4 = type2.GetAttribute <SerializeAsAttribute>();
                                text2 = ((attribute4 != null && attribute4.Name.HasValue()) ? attribute4.Name : type2.Name);
                            }
                            XElement xElement2 = new XElement(text2.AsNamespaced(Namespace));
                            Map(xElement2, item2);
                            xElement.Add(xElement2);
                        }
                    }
                    else
                    {
                        Map(xElement, value);
                    }
                    root.Add(xElement);
                }
            }
        }
Exemple #7
0
        private void Map(XContainer root, object obj)
        {
            Type objType = obj.GetType();

#if NETCORE50 || NETSTANDARD1_5 || NETSTANDARD2_0
            IEnumerable <PropertyInfo> props = from p in objType.GetTypeInfo().GetProperties()
                                               let indexAttribute = p.GetAttribute <SerializeAsAttribute>()
                                                                    where p.CanRead && p.CanWrite
                                                                    orderby indexAttribute == null
                                                  ? int.MaxValue
                                                  : indexAttribute.Index
                                                                    select p;
#else
            IEnumerable <PropertyInfo> props = from p in objType.GetProperties()
                                               let indexAttribute = p.GetAttribute <SerializeAsAttribute>()
                                                                    where p.CanRead && p.CanWrite
                                                                    orderby indexAttribute == null
                                                  ? int.MaxValue
                                                  : indexAttribute.Index
                                                                    select p;
#endif
            SerializeAsAttribute globalOptions = objType.GetAttribute <SerializeAsAttribute>();

            foreach (PropertyInfo prop in props)
            {
                string name     = prop.Name;
                object rawValue = prop.GetValue(obj, null);

                if (rawValue == null)
                {
                    continue;
                }

                string value                  = this.GetSerializedValue(rawValue);
                Type   propType               = prop.PropertyType;
                bool   useAttribute           = false;
                SerializeAsAttribute settings = prop.GetAttribute <SerializeAsAttribute>();

                if (settings != null)
                {
                    name = settings.Name.HasValue()
                        ? settings.Name
                        : name;
                    useAttribute = settings.Attribute;
                }

                SerializeAsAttribute options = prop.GetAttribute <SerializeAsAttribute>();

                if (options != null)
                {
                    name = options.TransformName(name);
                }
                else if (globalOptions != null)
                {
                    name = globalOptions.TransformName(name);
                }

                XName    nsName  = name.AsNamespaced(this.Namespace);
                XElement element = new XElement(nsName);
#if !WINDOWS_UWP && !(NETCORE50 || NETSTANDARD1_5 || NETSTANDARD2_0)
                if (propType.IsPrimitive || propType.IsValueType || propType == typeof(string))
#else
                if (propType.GetTypeInfo().IsPrimitive || propType.GetTypeInfo().IsValueType || propType == typeof(string))
#endif
                {
                    if (useAttribute)
                    {
                        root.Add(new XAttribute(name, value));
                        continue;
                    }

                    element.Value = value;
                }
                else if (rawValue is IList)
                {
                    string itemTypeName = "";

                    foreach (object item in (IList)rawValue)
                    {
                        if (itemTypeName == "")
                        {
                            Type type = item.GetType();
                            SerializeAsAttribute setting = type.GetAttribute <SerializeAsAttribute>();

                            itemTypeName = setting != null && setting.Name.HasValue()
                                ? setting.Name
                                : type.Name;
                        }

                        XElement instance = new XElement(itemTypeName.AsNamespaced(this.Namespace));

                        this.Map(instance, item);
                        element.Add(instance);
                    }
                }
                else
                {
                    this.Map(element, rawValue);
                }

                root.Add(element);
            }
        }