Exemple #1
0
        /// <summary>
        /// Applies the data stored in this serialized data to the specified object using reflection.
        /// </summary>
        /// <param name="filename">Only used for error reporting</param>
        public void ApplyTo(string filename, object obj, List <SerializerError> errors, XMLSerializerParams parameters)
        {
            foreach (var field in fields)
            {
                var _fieldInfo = this.targetType.GetFieldData(field.Key);
                var fieldInfo  = _fieldInfo.Value;
                var value      = field.Value;

                var sub = value as SerializedData;
                if (!ReferenceEquals(sub, null))
                {
                    // Field already set?
                    value = fieldInfo.fieldInfo.GetValue(obj);

                    // If not, create new obj
                    if (ReferenceEquals(value, null))
                    {
                        value = sub.targetType.Create();
                    }

                    sub.ApplyTo(filename, value, errors, parameters);
                }

                var col = value as SerializedCollectionData;
                if (!ReferenceEquals(col, null))
                {
                    // Field already set?
                    value = fieldInfo.fieldInfo.GetValue(obj);

                    CollectionOverrideAction action;
                    if (!ReferenceEquals(value, null))
                    {
                        if (!collectionOverrideActions.TryGetValue(field.Key, out action))
                        {
                            action = CollectionOverrideAction.Combine;                             // Always default to comibining
                        }
                        switch (action)
                        {
                        case CollectionOverrideAction.Combine: value = col.CombineWithInNew(value); break;

                        case CollectionOverrideAction.Replace: value = col.CreateCollection(); break;
                        }
                    }
                    else                     // Write new collection
                    {
                        value = col.CreateCollection();
                    }
                }

                if (!SerializerValidation.TypeCheck(parameters, debug.TryGet(field.Key), field.Key, value, fieldInfo.fieldInfo.FieldType, filename, errors))
                {
                    continue;
                }

                fieldInfo.fieldInfo.SetValue(obj, value);
            }
        }
Exemple #2
0
        /// <summary>
        /// <see cref="SerializedData.LoadFields(List{SerializerError}, PrototypeParserState)"/>
        /// </summary>
        /// <param name="errors"></param>
        /// <param name="filename">The filename from where data will be parsed. Only used for error reporting.</param>
        public void ParseAndLoadData(string filename, List <SerializerError> errors, XMLSerializerParams parameters)
        {
            var elementNodes = xElement.Nodes().ToList();
            var collection   = GetCollectionInstance(this.collectionType, elementNodes.Count);

            Type   elementType      = GetElementType(this.collectionType);
            var    elementTypeCache = SerializerCache.GetSerializableTypeCacheFor(elementType);
            string elementTypeName  = elementType.Name;

            foreach (var node in elementNodes)
            {
                var xElementNode = node as XElement;
                if (ReferenceEquals(xElementNode, null))                 // Malformed XML
                {
                    errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, filename, (node as IXmlLineInfo).LineNumber, "Unable to cast node to element for " + node + "! Skipping element!"));
                    continue;
                }

                IXMLDataSerializer serializer = SerializerCache.GetBestSerializerFor(elementType);
                if (!ReferenceEquals(serializer, null))
                {
                    try
                    {
                        if (!SerializerValidation.SerializerWasFound(parameters, null, serializer, "Collection", null, elementType, "SERIALIZE", errors))
                        {
                            continue;
                        }

                        this.elements.Add(serializer.Deserialize(elementType, xElementNode, parameters));
                    }
                    catch (Exception ex)
                    {
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, "SERIALIZE", -1, "Serializer threw exception on collection of type " + elementType + ":\n\n" + ex.ToString() + "\n\nSkipping element!"));
                    }
                }
                else if (typeof(ISerializableRoot).IsAssignableFrom(elementType))
                {
                    // object ref
                    this.elements.Add(new SerializedRootObjectReference()
                    {
                        identifier = xElementNode.Value
                    });
                }
                else
                {
                    // Determine type
                    var    serializableTypeCache = elementTypeCache;
                    string typeName = elementTypeName;

                    // Try to read class attrib
                    var classAttrib = xElementNode.Attribute(XMLSerializer.AttributeType);
                    if (!ReferenceEquals(classAttrib, null))
                    {
                        serializableTypeCache = SerializerCache.GetSerializableTypeCacheFor(classAttrib.Value, parameters.standardNamespace);
                        typeName = classAttrib.Value;
                    }

                    // Validity checks
                    // Field not serializable?
                    if (ReferenceEquals(serializableTypeCache, null))
                    {
                        // TODO: Line number, better reporting as to why this type is unserializable!
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, filename, -1, "Collection element with unknown type " + typeName + " unserializable! Skipping field!"));
                        continue;
                    }

                    // Add element
                    this.elements.Add(new SerializedData(serializableTypeCache, xElementNode));
                }
            }

            foreach (var element in this.elements)
            {
                var sElement = element as SerializedData;
                if (!ReferenceEquals(sElement, null))
                {
                    sElement.LoadFieldsFromXML(filename, errors, parameters);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Serializes the collection obj to <see cref="xElement"/>.
        /// </summary>
        /// <param name="targetElement">The element to write the loaded data to.</param>
        /// <param name="obj">The collection to load and write to <see cref="xElement"/></param>
        /// <param name="referenceables">Serializable object roots which will possibly be referenced by elements in obj.</param>
        public void WriteFromObject(object collection, XElement targetElement, List <SerializerError> errors, XMLSerializerParams parameters)
        {
            Type   elementType      = GetElementType(this.collectionType);
            var    elementTypeCache = SerializerCache.GetSerializableTypeCacheFor(elementType);
            string elementTypeName  = elementType.Name;

            foreach (var obj in (collection as IEnumerable))
            {
                XElement _targetElement = new XElement("li");
                targetElement.Add(_targetElement);

                if (obj == null)
                {
                    _targetElement.Add(XMLSerializer.TokenNull);
                    continue;
                }

                var objType = obj.GetType();
                IXMLDataSerializer serializer = SerializerCache.GetBestSerializerFor(objType);
                if (!ReferenceEquals(serializer, null))
                {
                    try
                    {
                        if (!SerializerValidation.SerializerWasFound(parameters, null, serializer, "Collection", null, objType, "SERIALIZE", errors))
                        {
                            continue;
                        }

                        serializer.Serialize(obj, _targetElement, parameters);
                    }
                    catch (Exception ex)
                    {
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, "SERIALIZE", -1, "Serializer threw exception on collection of type " + objType + ":\n\n" + ex.ToString() + "\n\nSkipping field!"));
                    }
                }
                else if (typeof(ISerializableRoot).IsAssignableFrom(elementType))
                {
                    // object ref
                    _targetElement.Value = (obj as ISerializableRoot).identifier;
                }
                else
                {
                    // Determine type
                    var    serializableTypeCache = elementTypeCache;
                    string typeName = elementTypeName;

                    // Write class attribute
                    _targetElement.SetAttributeValue(XMLSerializer.AttributeType, typeName);

                    // Validity checks
                    // Field not serializable?
                    if (ReferenceEquals(serializableTypeCache, null))
                    {
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, "SERIALIZER", -1, "Collection element with unknown type " + typeName + " unserializable! Skipping element!"));
                        continue;
                    }

                    // Add element
                    new SerializedData(serializableTypeCache, _targetElement).WriteFromObject(obj, errors, parameters);
                }
            }
        }
Exemple #4
0
        public void WriteFromObject(object obj, List <SerializerError> errors, XMLSerializerParams parameters)
        {
            targetType.GetAllFields(_fields);
            foreach (var fieldData in _fields)
            {
                if (!fieldData.isSerialized)
                {
                    continue;
                }

                string   fieldName     = fieldData.fieldInfo.Name;
                XElement targetElement = new XElement(fieldName);
                xElement.Add(targetElement);

                var fieldObj = fieldData.fieldInfo.GetValue(obj);
                if (fieldObj == null)                 // Null special case
                {
                    if (!fieldData.fieldInfo.GetCustomAttributes(true).Any((attrib) => attrib is AlwaysSerializedAttribute))
                    {
                        return;
                    }

                    targetElement.Value = XMLSerializer.TokenNull;
                    continue;
                }

                var  fieldObjType             = SerializerCache.GetSerializableTypeCacheFor(fieldObj.GetType());
                bool isCollection             = SerializedCollectionData.IsCollection(fieldData.fieldInfo.FieldType);
                IXMLDataSerializer serializer = SerializerCache.GetBestSerializerFor(fieldData.fieldInfo.FieldType);

                if (!ReferenceEquals(serializer, null))
                {
                    try
                    {
                        if (!SerializerValidation.SerializerWasFound(parameters, targetElement, serializer, fieldName, fieldObjType == null ? null : fieldObjType.type, fieldObjType == null ? null : fieldObjType.type, "SERIALIZE", errors))
                        {
                            continue;
                        }

                        serializer.Serialize(fieldObj, targetElement, parameters);
                    }
                    catch (Exception ex)
                    {
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, "SERIALIZE", -1, "Serializer threw exception on field " + fieldName + " on type " + fieldObjType.type + ":\n\n" + ex.ToString() + "\n\nSkipping field!"));
                    }
                }
                else if (isCollection)
                {
                    var col = new SerializedCollectionData(fieldData.fieldInfo.FieldType, targetElement);
                    col.WriteFromObject(fieldObj, targetElement, errors, parameters);
                }
                else
                {
                    // root reference?
                    if (fieldData.isSerializableRoot)
                    {
                        targetElement.Value = (fieldObj as ISerializableRoot).identifier;
                    }
                    else
                    {
                        // Determine which type to serialize
                        string typeName = fieldObjType.type.Name;

                        // Write class attribute
                        if (!ReferenceEquals(fieldData.fieldInfo.FieldType, fieldObjType.type))
                        {
                            targetElement.SetAttributeValue(XMLSerializer.AttributeType, typeName);
                        }

                        // Field not serializable?
                        if (!SerializerValidation.DataFieldSerializerValid(parameters, targetElement, fieldObjType, typeName, fieldName, "SERIALIZE", errors))
                        {
                            continue;
                        }

                        // Write field
                        var d = new SerializedData(fieldObjType, targetElement);
                        d.WriteFromObject(fieldObj, errors, parameters);
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// It will for every field with string data deserialize this data using <see cref="IXMLDataSerializer"/>.
        ///
        /// For every sub-data field a <see cref="SerializedData"/> object is being written to <see cref="fields"/>.
        /// </summary>
        /// <param name="filename">Only used for error reporting</param>
        public void LoadFieldsFromXML(string filename, List <SerializerError> errors, XMLSerializerParams parameters)
        {
            foreach (var xNode in xElement.Nodes())
            {
                if (!SerializerValidation.NodeIsElement(parameters, xNode, filename, errors))                 // Malformed XML
                {
                    continue;
                }

                var xElement    = xNode as XElement;
                var elementName = xElement.Name.LocalName;

                // Field unknown?
                if (!SerializerValidation.FieldKnown(parameters, xElement, targetType, elementName, filename, errors))
                {
                    continue;
                }

                debug.Add(elementName, xElement);
                var _fieldData = targetType.GetFieldData(elementName);
                var fieldData  = _fieldData.Value;
                var fieldType  = fieldData.serializableTypeCache;

                if (xElement.Value == XMLSerializer.TokenNull)                 // Null special case
                {
                    fields.Add(elementName, null);
                    continue;
                }

                bool isCollection             = SerializedCollectionData.IsCollection(fieldData.fieldInfo.FieldType);
                IXMLDataSerializer serializer = SerializerCache.GetBestSerializerFor(fieldData.fieldInfo.FieldType);

                if (!ReferenceEquals(serializer, null))
                {
                    try
                    {
                        if (!SerializerValidation.SerializerWasFound(parameters, xElement, serializer, elementName, targetType == null ? null : targetType.type, fieldType == null ? null : fieldType.type, filename, errors))
                        {
                            continue;
                        }

                        fields.Add(elementName, serializer.Deserialize(fieldData.fieldInfo.FieldType, xElement, parameters));
                    }
                    catch (Exception ex)
                    {
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, filename, -1, "Serializer threw exception on field " + elementName + " on type " + targetType.type + ":\n\n" + ex.ToString() + "\n\nSkipping field!"));
                    }
                }
                else if (isCollection)
                {
                    var col = new SerializedCollectionData(fieldData.fieldInfo.FieldType, xElement);
                    col.ParseAndLoadData(filename, errors, parameters);
                    fields.Add(elementName, col);

                    // Collection override action?
                    var collectionOverrideAttrib = xElement.Attribute(XMLSerializer.AttributeCollectionOverrideAction);
                    if (!ReferenceEquals(collectionOverrideAttrib, null))
                    {
                        collectionOverrideActions.Set(elementName, (CollectionOverrideAction)Enum.Parse(typeof(CollectionOverrideAction), collectionOverrideAttrib.Value));
                    }
                }
                else
                {
                    // root reference?
                    if (fieldData.isSerializableRoot)
                    {
                        fields.Add(elementName, new SerializedRootObjectReference()
                        {
                            identifier = xElement.Value as string
                        });
                    }
                    else
                    {
                        // Determine which type to serialize
                        var    targetType = fieldData.serializableTypeCache;
                        string typeName   = fieldData.fieldInfo.Name;

                        // Check if element explicitly overwrites the type to support polymorphism
                        // The field type might be some base class type and the xml overwrites this type with a class extending from the base
                        var classAttrib = xElement.Attribute(XMLSerializer.AttributeType);
                        if (!ReferenceEquals(classAttrib, null))
                        {
                            targetType = SerializerCache.GetSerializableTypeCacheFor(classAttrib.Value, parameters.standardNamespace);
                            typeName   = classAttrib.Value;
                        }

                        // Field not serializable?
                        if (!SerializerValidation.DataFieldSerializerValid(parameters, xElement, targetType, typeName, elementName, filename, errors))
                        {
                            continue;
                        }

                        // Resolve field name type
                        var d = new SerializedData(targetType, xElement as XElement);
                        d.LoadFieldsFromXML(filename, errors, parameters);
                        fields.Add(elementName, d);
                    }
                }
            }
        }