예제 #1
0
 public void WriteObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     Xml.WriteStartElement(format.ElementName);
     Xml.WriteAttributeString("Type", typeSerializer.XmlTypeName);
     typeSerializer.Serialize(this, value, format);
     Xml.WriteEndElement();
 }
예제 #2
0
        public T ReadRawObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
        {
            if (format.FlattenContent)
            {
                Xml.MoveToContent();
                return((T)typeSerializer.Deserialize(this, format, existingInstance));
            }

            if (!MoveToElement(format.ElementName))
            {
                throw NewInvalidContentException(null, "Element '{0}' was not found.", format.ElementName);
            }

            var isEmpty = Xml.IsEmptyElement;

            if (!isEmpty)
            {
                Xml.ReadStartElement();
            }

            var result = typeSerializer.Deserialize(this, format, existingInstance);

            if (isEmpty)
            {
                Xml.Skip();
            }

            if (!isEmpty)
            {
                Xml.ReadEndElement();
            }

            return((T)result);
        }
예제 #3
0
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            // If we have a base type then we need to deserialize it first.
            if (TargetType.BaseType != null)
            {
                _baseSerializer = serializer.GetTypeSerializer(TargetType.BaseType);
            }

            // Cache all our serializable properties.
            var properties = TargetType.GetProperties(_bindingFlags);

            foreach (var prop in properties)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, prop, out info))
                {
                    _elements.Add(info);
                }
            }

            // Cache all our serializable fields.
            var fields = TargetType.GetFields(_bindingFlags);

            foreach (var field in fields)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, field, out info))
                {
                    _elements.Add(info);
                }
            }
        }
예제 #4
0
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     _serializer = serializer.GetTypeSerializer(typeof(T));
     _format     = new ContentSerializerAttribute
     {
         FlattenContent = true
     };
 }
        public GenericCollectionHelper(IntermediateSerializer serializer, Type type)
        {
            var collectionElementType = GetCollectionElementType(type, false);

            _contentSerializer = serializer.GetTypeSerializer(collectionElementType);

            var collectionType = typeof(ICollection <>).MakeGenericType(collectionElementType);

            _countProperty = collectionType.GetProperty("Count");
            _addMethod     = collectionType.GetMethod("Add", new[] { collectionElementType });
        }
        public void WriteRawObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
        {
            if (!format.FlattenContent)
            {
                Xml.WriteStartElement(format.ElementName);
            }

            typeSerializer.Serialize(this, value, format);

            if (!format.FlattenContent)
            {
                Xml.WriteEndElement();
            }
        }
예제 #7
0
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            _keySerializer   = serializer.GetTypeSerializer(typeof(TKey));
            _valueSerializer = serializer.GetTypeSerializer(typeof(TValue));

            _keyFormat = new ContentSerializerAttribute
            {
                ElementName = "Key",
                AllowNull   = false
            };

            _valueFormat = new ContentSerializerAttribute()
            {
                ElementName = "Value",
                AllowNull   = typeof(TValue).IsValueType
            };
        }
예제 #8
0
        public T ReadObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
        {
            if (!MoveToElement(format.ElementName))
            {
                throw new InvalidContentException(string.Format("Element `{0}` was not found in `{1}`.", format.ElementName, _filePath));
            }

            // Is the object overloading the serialized type?
            if (Xml.MoveToAttribute("Type"))
            {
                var type = ReadTypeName();
                typeSerializer = Serializer.GetTypeSerializer(type);
                Xml.MoveToElement();
            }

            return(ReadRawObject(format, typeSerializer, existingInstance));
        }
예제 #9
0
        public T ReadRawObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
        {
            if (format.FlattenContent)
            {
                Xml.MoveToContent();
                return((T)typeSerializer.Deserialize(this, format, existingInstance));
            }

            if (!MoveToElement(format.ElementName))
            {
                throw new InvalidContentException(string.Format("Element `{0}` was not found in `{1}`.", format.ElementName, _filePath));
            }

            Xml.ReadStartElement();
            var result = typeSerializer.Deserialize(this, format, existingInstance);

            Xml.ReadEndElement();
            return((T)result);
        }
예제 #10
0
        public T ReadObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
        {
            if (!format.FlattenContent)
            {
                if (!MoveToElement(format.ElementName))
                {
                    throw NewInvalidContentException(null, "Element '{0}' was not found.", format.ElementName);
                }

                // Is the object null?
                var isNull = Xml.GetAttribute("Null");
                if (isNull != null && XmlConvert.ToBoolean(isNull))
                {
                    if (!format.AllowNull)
                    {
                        throw NewInvalidContentException(null, "Element '{0}' cannot be null.", format.ElementName);
                    }

                    Xml.Skip();
                    return(default(T));
                }

                // Is the object overloading the serialized type?
                if (Xml.MoveToAttribute("Type"))
                {
                    var type = ReadTypeName();
                    if (type == null)
                    {
                        throw NewInvalidContentException(null, "Could not resolve type '{0}'.", Xml.ReadContentAsString());
                    }
                    if (!typeSerializer.TargetType.IsAssignableFrom(type))
                    {
                        throw NewInvalidContentException(null, "Type '{0}' is not assignable to '{1}'.", type.FullName, typeSerializer.TargetType.FullName);
                    }

                    typeSerializer = Serializer.GetTypeSerializer(type);
                    Xml.MoveToElement();
                }
            }

            return(ReadRawObject(format, typeSerializer, existingInstance));
        }
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     _itemSerializer = serializer.GetTypeSerializer(typeof(T));
 }
        internal void WriteObjectInternal(object value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Type declaredType)
        {
            if (format.Optional && (value == null || typeSerializer.ObjectIsEmpty(value)))
            {
                return;
            }

            var isReferenceObject = false;

            if (value != null && !typeSerializer.TargetType.IsValueType)
            {
                if (_currentObjectStack.Contains(value))
                {
                    throw new InvalidOperationException("Cyclic reference found during serialization. You may be missing a [ContentSerializer(SharedResource=true)] attribute.");
                }
                _currentObjectStack.Push(value);
                isReferenceObject = true;
            }

            if (!format.FlattenContent)
            {
                Xml.WriteStartElement(format.ElementName);

                if (value == null)
                {
                    if (!format.AllowNull)
                    {
                        throw new InvalidOperationException(string.Format("Element {0} cannot be null.", format.ElementName));
                    }

                    Xml.WriteAttributeString("Null", "true");
                }
                else if (value.GetType() != typeSerializer.TargetType && !IsNullableType(declaredType))
                {
                    Xml.WriteStartAttribute("Type");
                    WriteTypeName(value.GetType());
                    Xml.WriteEndAttribute();

                    typeSerializer = Serializer.GetTypeSerializer(value.GetType());
                }
            }

            if (value != null && !typeSerializer.ObjectIsEmpty(value))
            {
                typeSerializer.Serialize(this, value, format);
            }

            if (!format.FlattenContent)
            {
                Xml.WriteEndElement();
            }

            if (isReferenceObject)
            {
                _currentObjectStack.Pop();
            }
        }
 public void WriteObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     WriteObjectInternal(value, format, typeSerializer, typeof(T));
 }
예제 #14
0
 public T ReadObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     return(ReadObject(format, typeSerializer, default(T)));
 }
예제 #15
0
 public T ReadObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
 {
     throw new NotImplementedException();
 }
예제 #16
0
 public T ReadRawObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     throw new NotImplementedException();
 }
예제 #17
0
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            // If we have a base type then we need to deserialize it first.
            if (TargetType.BaseType != null)
                _baseSerializer = serializer.GetTypeSerializer(TargetType.BaseType);

            // Cache all our serializable properties.
            var properties = TargetType.GetProperties(_bindingFlags);
            foreach (var prop in properties)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, prop, out info))
                    _elements.Add(info);
            }

            // Cache all our serializable fields.
            var fields = TargetType.GetFields(_bindingFlags);
            foreach (var field in fields)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, field, out info))
                    _elements.Add(info);                
            }
        }
예제 #18
0
 public void WriteRawObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     throw new NotImplementedException();
 }