Exemplo n.º 1
0
        public static object Unstage(StageItem item, Type targetType)
        {
            if (item is StageNull)
            {
                return(null);
            }
            IStager stager = SerializationMaster.GetStager(targetType);

            if (stager != null)
            {
                return(stager.UnstageValue(item, targetType));
            }
            StageValue stageValue = item as StageValue;

            if (stageValue == null)
            {
                StageElement stageElement = item as StageElement;
                if (stageElement == null)
                {
                    throw new SerializationException(string.Concat("Unable to unstage, the element is not supported: ", targetType.Name));
                }
                return(SerializationMaster.ReflectIn(stageElement));
            }
            IValueConverter converter = SerializationMaster.GetConverter(targetType);

            if (converter == null)
            {
                throw new SerializationException(string.Concat("Unable to unstage, no converter or stager was found for type: ", targetType.Name));
            }
            return(converter.FromString(stageValue.@value, targetType));
        }
Exemplo n.º 2
0
        public static StageItem Stage(string name, object value)
        {
            if (value == null)
            {
                return(new StageNull(name));
            }
            Type type = value.GetType();
            IPrepareForSerialization prepareForSerialization = value as IPrepareForSerialization;

            if (prepareForSerialization != null)
            {
                prepareForSerialization.Prepare();
            }
            IStager stager = SerializationMaster.GetStager(type);

            if (stager != null)
            {
                return(stager.StageValue(name, value));
            }
            IValueConverter converter = SerializationMaster.GetConverter(type);

            if (converter == null)
            {
                return(SerializationMaster.ReflectOut(name, value));
            }
            return(new StageValue(name, converter.ToString(value), type == SerializationMaster._stringType));
        }
        /// <summary>
        /// Sets the value of an attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool remove = (value == null);

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (attrib.isText && !(value is string))
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
        /// <summary>
        /// Sets the value of a text attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="removeIfEmpty">if set to <c>true</c> the attribute is removed if the <paramref name="value"/> is empty. The attribute is always removed if <paramref name="value"/> is <c>null</c></param>
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool remove = (value == null) || (removeIfEmpty && string.IsNullOrEmpty(value));

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (!attrib.isText)
            {
                throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
Exemplo n.º 5
0
        public static T Unstage <T>(StageItem item, ICollection <IInitializeAfterDeserialization> requiresInit)
        {
            T t;

            if (SerializationMaster._requiresInit != null)
            {
                throw new InvalidOperationException("Generic overloads of Unstage cannot be called during a nested unstage operation.");
            }
            SerializationMaster._requiresInit = requiresInit;
            try
            {
                object obj = SerializationMaster.Unstage(item, typeof(T));
                if (obj != null)
                {
                    t = (T)obj;
                }
                else
                {
                    t = default(T);
                    t = t;
                }
            }
            finally
            {
                SerializationMaster._requiresInit = null;
            }
            return(t);
        }
Exemplo n.º 6
0
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool flag;

            if (value == null)
            {
                flag = true;
            }
            else
            {
                flag = (!removeIfEmpty ? false : string.IsNullOrEmpty(value));
            }
            bool           flag1 = flag;
            StageAttribute str   = parent.Attribute(name);

            if (str != null)
            {
                if (flag1)
                {
                    str.Remove();
                    return;
                }
                if (!str.isText)
                {
                    throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag1)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }
        /// <summary>
        /// Sets the value of a text element. If it does not exist it is created.
        /// If multiple identically named items exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the item.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="removeIfNullOrEmpty">If <c>true</c> and <para<paramref name="value"/> is <c>null</c> or empty, the item is removed rather than converted to a <see cref="StageNull"/></param>
        public static void SetTextValue(this StageElement parent, string name, string value, bool removeIfNullOrEmpty = true)
        {
            bool remove = (removeIfNullOrEmpty && string.IsNullOrEmpty(value));

            var item = parent.Item(name);

            if (item == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }

                return;
            }

            if (item is StageAttribute)
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }

            var nullItem = item as StageNull;

            if (item != null)
            {
                if (value != null)
                {
                    nullItem.Remove();
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }

                return;
            }

            var valueItem = item as StageValue;

            if (item == null)
            {
                throw new InvalidOperationException("Only value elements can be set using this method.");
            }

            if (remove)
            {
                item.Remove();
            }
            else if (value == null)
            {
                item.Remove();
                parent.Add(new StageNull(name));
            }
            else if (!valueItem.isText)
            {
                throw new InvalidOperationException("Cannot set a text value on a non-text value item.");
            }
            else
            {
                valueItem.value = SerializationMaster.ToString(value);
            }
        }
Exemplo n.º 8
0
 public static StageItem ToStageValue(string name, object value)
 {
     if (value == null)
     {
         return(new StageNull(name));
     }
     return(new StageValue(name, SerializationMaster.ToString(value), value is string));
 }
Exemplo n.º 9
0
 public static void AddAttribute(this StageElement parent, string name, object value, bool onlyIfNotNull = true)
 {
     if (onlyIfNotNull && value == null)
     {
         return;
     }
     parent.Add(SerializationMaster.ToStageAttribute(name, value));
 }
Exemplo n.º 10
0
 public static void AddValue(this StageContainer parent, string name, object value, bool onlyIfNotNull = true)
 {
     if (onlyIfNotNull && value == null)
     {
         return;
     }
     parent.Add(SerializationMaster.Stage(name, value));
 }
Exemplo n.º 11
0
 public static void AddTextValue(this StageContainer parent, string name, string value, bool onlyIfNotNullOrEmpty = true)
 {
     if (onlyIfNotNullOrEmpty && string.IsNullOrEmpty(value))
     {
         return;
     }
     parent.Add(SerializationMaster.ToStageValue(name, value));
 }
Exemplo n.º 12
0
        public static string Serialize <T>(T item, bool pretty = false)
        {
            SerializationMaster.EnsureInit();
            StageItem stageItem = SerializationMaster.Stage(typeof(T).Name, item);

            if (stageItem == null)
            {
                return(string.Empty);
            }
            return(SerializationMaster._serializer.Serialize(stageItem, pretty));
        }
Exemplo n.º 13
0
        public static T Deserialize <T>(string data, ICollection <IInitializeAfterDeserialization> requiresInit)
        {
            SerializationMaster.EnsureInit();
            StageItem stageItem = SerializationMaster._serializer.Deserialize(data);

            if (stageItem == null)
            {
                return(default(T));
            }
            return(SerializationMaster.Unstage <T>(stageItem, requiresInit));
        }
Exemplo n.º 14
0
        public static T Deserialize <T>(string data)
        {
            SerializationMaster.EnsureInit();
            StageItem stageItem = SerializationMaster._serializer.Deserialize(data);

            if (stageItem != null)
            {
                return(SerializationMaster.UnstageAndInitialize <T>(stageItem));
            }
            return(default(T));
        }
Exemplo n.º 15
0
 public static T ValueOrDefault <T>(this StageItem item, T defaultValue = null)
 {
     if (item == null || item is StageNull)
     {
         return(defaultValue);
     }
     if (item is StageContainer)
     {
         return(SerializationMaster.UnstageAndInitialize <T>(item));
     }
     return(SerializationMaster.FromString <T>(((StageValue)item).@value));
 }
Exemplo n.º 16
0
 public static T AttributeValue <T>(this StageElement element, string attributeName)
 {
     if (element != null)
     {
         StageAttribute stageAttribute = element.Attribute(attributeName);
         if (stageAttribute != null)
         {
             return(SerializationMaster.FromString <T>(stageAttribute.@value));
         }
     }
     throw new ArgumentException(string.Concat("No attribute by that name was found: ", attributeName));
 }
Exemplo n.º 17
0
        private static object ReflectIn(StageElement element)
        {
            object obj;
            object obj1;
            object obj2;
            string str = element.AttributeValueOrDefault <string>("type", null);

            if (str == null)
            {
                throw new SerializationException("Invalid structure detected, missing type info.");
            }
            Type type = Type.GetType(str, true);

            try
            {
                obj = Activator.CreateInstance(type, true);
            }
            catch (MissingMethodException missingMethodException1)
            {
                MissingMethodException missingMethodException = missingMethodException1;
                throw new SerializationException(string.Format("Unable to create type {0}, ensure it has a parameterless constructor", type.Name), missingMethodException);
            }
            IInitializeAfterDeserialization initializeAfterDeserialization = obj as IInitializeAfterDeserialization;

            if (initializeAfterDeserialization != null)
            {
                if (SerializationMaster._requiresInit == null)
                {
                    throw new InvalidOperationException("An entity requires initialization but was unable to register, call UnstageAndInitialize instead.");
                }
                SerializationMaster._requiresInit.Add(initializeAfterDeserialization);
            }
            foreach (PropertyInfo serializedProperty in SerializationMaster.GetSerializedProperties(type))
            {
                StageItem stageItem = element.Item(serializedProperty.Name);
                if (stageItem == null || !SerializationMaster.TryUnstage(stageItem, serializedProperty.PropertyType, out obj1))
                {
                    continue;
                }
                serializedProperty.SetValue(obj, obj1, null);
            }
            foreach (FieldInfo serializedField in SerializationMaster.GetSerializedFields(type))
            {
                StageItem stageItem1 = element.Item(serializedField.Name);
                if (stageItem1 == null || !SerializationMaster.TryUnstage(stageItem1, serializedField.FieldType, out obj2))
                {
                    continue;
                }
                serializedField.SetValue(obj, obj2);
            }
            return(obj);
        }
        /// <summary>
        /// Gets the converted value of the specified attribute.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <returns>The value of the attribute.</returns>
        /// <exception cref="System.ArgumentException">If no attribute by that name was found.</exception>
        public static T AttributeValue <T>(this StageElement element, string attributeName)
        {
            if (element != null)
            {
                var attrib = element.Attribute(attributeName);
                if (attrib != null)
                {
                    return(SerializationMaster.FromString <T>(attrib.value));
                }
            }

            throw new ArgumentException("No attribute by that name was found: " + attributeName);
        }
Exemplo n.º 19
0
        public static void SetValue(this StageElement parent, string name, object value, bool removeIfNull = true)
        {
            bool      flag      = (!removeIfNull ? false : value == null);
            StageItem stageItem = parent.Item(name);

            if (stageItem == null)
            {
                if (!flag)
                {
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }
                return;
            }
            if (stageItem is StageAttribute)
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }
            StageNull stageNull = stageItem as StageNull;

            if (stageItem != null)
            {
                if (value != null)
                {
                    stageNull.Remove();
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }
                return;
            }
            StageValue str = stageItem as StageValue;

            if (stageItem == null)
            {
                throw new InvalidOperationException("Only value elements can be set using this method.");
            }
            if (flag)
            {
                stageItem.Remove();
                return;
            }
            if (value == null)
            {
                stageItem.Remove();
                parent.Add(new StageNull(name));
                return;
            }
            if (str.isText && !(value is string))
            {
                throw new InvalidOperationException("Use SetTextValue to set text values.");
            }
            str.@value = SerializationMaster.ToString(value);
        }
Exemplo n.º 20
0
 private static void EnsureInit()
 {
     if (!SerializationMaster._isInitialized)
     {
         lock (SerializationMaster._typeConverters)
         {
             if (!SerializationMaster._isInitialized)
             {
                 SerializationMaster.PopulateKnownSerializers();
                 SerializationMaster._isInitialized = true;
             }
         }
     }
 }
Exemplo n.º 21
0
        public static T AttributeValueOrDefault <T>(this StageElement element, string attributeName, T defaultValue = null)
        {
            if (element == null)
            {
                return(defaultValue);
            }
            StageAttribute stageAttribute = element.Attribute(attributeName);

            if (stageAttribute == null)
            {
                return(defaultValue);
            }
            return(SerializationMaster.FromString <T>(stageAttribute.@value));
        }
Exemplo n.º 22
0
        public static string ToString(object value)
        {
            if (value == null)
            {
                return(null);
            }
            IValueConverter converter = SerializationMaster.GetConverter(value.GetType());

            if (converter == null)
            {
                throw new ArgumentException(string.Concat("No converter was found for type ", value.GetType()));
            }
            return(converter.ToString(value));
        }
Exemplo n.º 23
0
        private static StageElement ReflectOut(string elementName, object item)
        {
            StageItem stageItem;
            StageItem stageItem1;
            Type      type = item.GetType();

            string[]     strArrays    = type.AssemblyQualifiedName.Split(new char[] { ',' });
            StageElement stageElement = new StageElement(elementName, new StageItem[] { new StageAttribute("type", string.Concat(strArrays[0], ",", strArrays[1]), true) });

            foreach (SerializationMaster.AIPropInfo aIPropInfo in (
                         from p in (IEnumerable <PropertyInfo>)type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                         select new { p = p, attrib = p.GetAttribute <ApexSerializationAttribute>(true) }).Where((argument0) => {
                if (argument0.attrib == null || !argument0.p.CanRead)
                {
                    return(false);
                }
                return(argument0.p.CanWrite);
            }).Select((argument1) => new SerializationMaster.AIPropInfo()
            {
                prop = argument1.p,
                defaultValue = argument1.attrib.defaultValue
            }))
            {
                object value = aIPropInfo.prop.GetValue(item, null);
                if (value == null || value.Equals(aIPropInfo.defaultValue) || !SerializationMaster.TryStage(aIPropInfo.prop.Name, value, out stageItem))
                {
                    continue;
                }
                stageElement.Add(stageItem);
            }
            foreach (SerializationMaster.AIFieldInfo aIFieldInfo in
                     from f in (IEnumerable <FieldInfo>)type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                     let attrib = f.GetAttribute <ApexSerializationAttribute>(false)
                                  where attrib != null
                                  select new SerializationMaster.AIFieldInfo()
            {
                field = f,
                defaultValue = attrib.defaultValue
            })
            {
                object obj = aIFieldInfo.field.GetValue(item);
                if (obj == null || obj.Equals(aIFieldInfo.defaultValue) || !SerializationMaster.TryStage(aIFieldInfo.field.Name, obj, out stageItem1))
                {
                    continue;
                }
                stageElement.Add(stageItem1);
            }
            return(stageElement);
        }
Exemplo n.º 24
0
        public static T FromString <T>(string value)
        {
            if (value == null)
            {
                return(default(T));
            }
            Type            type      = typeof(T);
            IValueConverter converter = SerializationMaster.GetConverter(type);

            if (converter == null)
            {
                throw new ArgumentException(string.Concat("No converter was found for type ", type.Name));
            }
            return((T)converter.FromString(value, type));
        }
Exemplo n.º 25
0
        private static IValueConverter GetConverter(Type forType)
        {
            SerializationMaster.EnsureInit();
            if (forType.IsGenericType)
            {
                forType = forType.GetGenericTypeDefinition();
            }
            else if (forType.IsEnum)
            {
                forType = SerializationMaster._enumType;
            }
            IValueConverter valueConverter = null;

            SerializationMaster._typeConverters.TryGetValue(forType, out valueConverter);
            return(valueConverter);
        }
Exemplo n.º 26
0
        private static IStager GetStager(Type forType)
        {
            SerializationMaster.EnsureInit();
            if (forType.IsGenericType)
            {
                forType = forType.GetGenericTypeDefinition();
            }
            else if (forType.IsArray)
            {
                forType = typeof(Array);
            }
            IStager stager = null;

            SerializationMaster._typeStagers.TryGetValue(forType, out stager);
            return(stager);
        }
        /// <summary>
        /// Gets the converted value of the specified attribute, or a default value if the element or the attribute is null or if it has no value.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The value of the attribute, or <paramref name="defaultValue"/> if <paramref name="element"/> or the attribute is <c>null</c> or has no value.</returns>
        public static T AttributeValueOrDefault <T>(this StageElement element, string attributeName, T defaultValue = default(T))
        {
            if (element == null)
            {
                return(defaultValue);
            }

            var attrib = element.Attribute(attributeName);

            if (attrib == null)
            {
                return(defaultValue);
            }

            return(SerializationMaster.FromString <T>(attrib.value));
        }
Exemplo n.º 28
0
        public static T UnstageAndInitialize <T>(StageItem item)
        {
            if (SerializationMaster._initBuffer != null)
            {
                SerializationMaster._initBuffer.Clear();
            }
            else
            {
                SerializationMaster._initBuffer = new List <IInitializeAfterDeserialization>();
            }
            T t = SerializationMaster.Unstage <T>(item, SerializationMaster._initBuffer);

            if (SerializationMaster._initBuffer.Count > 0)
            {
                foreach (IInitializeAfterDeserialization initializeAfterDeserialization in SerializationMaster._initBuffer)
                {
                    initializeAfterDeserialization.Initialize(t);
                }
                SerializationMaster._initBuffer.Clear();
            }
            return(t);
        }
Exemplo n.º 29
0
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool           flag = value == null;
            StageAttribute str  = parent.Attribute(name);

            if (str != null)
            {
                if (flag)
                {
                    str.Remove();
                    return;
                }
                if (str.isText && !(value is string))
                {
                    throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }
Exemplo n.º 30
0
 public static StageElement Deserialize(string data)
 {
     SerializationMaster.EnsureInit();
     return(SerializationMaster._serializer.Deserialize(data) as StageElement);
 }