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) { if (GetElementInfo(serializer, prop, out ElementInfo info)) { _elements.Add(info); } } // Cache all our serializable fields. var fields = TargetType.GetFields(_bindingFlags); foreach (var field in fields) { if (GetElementInfo(serializer, field, out ElementInfo info)) { _elements.Add(info); } } if (GenericCollectionHelper.IsGenericCollectionType(TargetType, false)) { _collectionHelper = serializer.GetCollectionHelper(TargetType); } }
protected internal override void Initialize(IntermediateSerializer serializer) { _serializer = serializer.GetTypeSerializer(typeof(T)); _format = new ContentSerializerAttribute { FlattenContent = true }; }
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(); } }
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 }; }
public GenericCollectionHelper(IntermediateSerializer serializer, Type type) { var collectionElementType = GetCollectionElementType(type, false); if (collectionElementType == null) throw new ArgumentException($"Failed to get collection element type of {type}."); _contentSerializer = serializer.GetTypeSerializer(collectionElementType); var collectionType = typeof(ICollection<>).MakeGenericType(collectionElementType); var countProperty = collectionType.GetProperty("Count"); if (countProperty == null) throw new Exception("Failed to get property for reflection."); _countProperty = countProperty; var addMethod = collectionType.GetMethod("Add", new[] { collectionElementType }); if (addMethod == null) throw new Exception("Failed to get method for reflection."); _addMethod = addMethod; }
public T ReadObject <T>( ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance) { if (!format.FlattenContent) { if (!MoveToElement(format.ElementName)) { throw Exception_InvalidContent(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 Exception_InvalidContent(null, "Element '{0}' cannot be null.", format.ElementName); } Xml.Skip(); return(default);
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)); }
public T ReadObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer) { return(ReadObject(format, typeSerializer, default(T))); }
protected internal override void Initialize(IntermediateSerializer serializer) { _itemSerializer = serializer.GetTypeSerializer(typeof(T)); }