Beispiel #1
0
        /// <summary>
        /// Deserializes the XElement to the object of a specified type using options.
        /// </summary>
        /// <param name="type">The type of the object to deserialize.</param>
        /// <param name="parentElement">The parent XElement used to deserialize the object.</param>
        /// <param name="options">Indicates how the output is deserialized.</param>
        /// <returns>The deserialized object from the XElement.</returns>
        public static object Deserialize(Type type, XElement parentElement, XmlConvertOptions options)
        {
            var value = parentElement?.Value;

            if (value == null)
            {
                return(null);
            }                                   //We might not have an element for a property

            var objectType = ObjectType.From(type);

            if (objectType == ObjectType.Primitive)
            {
                return(PrimitiveSerializer.Deserialize(type, parentElement, options));
            }
            else if (objectType == ObjectType.Dictionary)
            {
                return(DictionarySerializer.Deserialize(type, parentElement, options));
            }
            else if (objectType == ObjectType.List)
            {
                return(ListSerializer.Deserialize(type, parentElement, options));
            }
            else
            {
                return(DeserializeObject(type, parentElement, options));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Serializes the specified property into a XElement using options.
        /// </summary>
        /// <param name="value">The object to serialize.</param>
        /// <param name="name">The name of the object to serialize.</param>
        /// <param name="parentElement">The element in which to serialize the object.</param>
        /// <param name="elementNames">The optional custom name of collection elements.</param>
        /// <param name="keyNames">The optional custom name of dictionary key elements.</param>
        /// <param name="valueNames">The optional custom name of dictionary value elements.</param>
        /// <param name="options">Indicates how the output is formatted or serialized.</param>
        /// <returns>The XElement representation of the object.</returns>
        public static XElement Serialize(object value, string name, string elementNames, string keyNames, string valueNames, XmlConvertOptions options)
        {
            XElement element = null;

            var objectType = ObjectType.From(value);

            if (objectType == ObjectType.Primitive)
            {
                Debug.Assert(objectType != ObjectType.Other); //For 100% code coverage :/
                element = PrimitiveSerializer.Serialize(value, name, options);
            }
            else if (objectType == ObjectType.Dictionary)
            {
                if (elementNames == null)
                {
                    elementNames = "Element";
                }
                if (keyNames == null)
                {
                    keyNames = "Key";
                }
                if (valueNames == null)
                {
                    valueNames = "Value";
                }

                element = DictionarySerializer.Serialize(value, name, elementNames, keyNames, valueNames, options);
            }
            else if (objectType == ObjectType.List)
            {
                if (elementNames == null)
                {
                    elementNames = "Element";
                }

                element = ListSerializer.Serialize(value, name, elementNames, options);
            }
            else
            {
                element = Serialize(value, name, options); //Recurse
            }

            return(Utilities.SetupSerializedElement(value, element, options));
        }