protected internal override T[] Deserialize(IntermediateReader input, ContentSerializerAttribute format, T[] existingInstance) { List <T> list = new List <T>(); this.listHelper.Deserialize(input, format, list); return(list.ToArray()); }
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(); }
internal void ReadSharedResources() { if (!MoveToElement("Resources")) { return; } var resources = new Dictionary <string, object>(); var resourceFormat = new ContentSerializerAttribute { ElementName = "Resource" }; // Read all the resources. Xml.ReadStartElement(); while (MoveToElement("Resource")) { var id = Xml.GetAttribute("ID"); var resource = ReadObject <object>(resourceFormat); resources.Add(id, resource); } Xml.ReadEndElement(); // Execute the fixups. foreach (var fixup in _resourceFixups) { object resource; if (!resources.TryGetValue(fixup.Key, out resource)) { throw new InvalidContentException("Missing shared resource \"" + fixup.Key + "\"."); } fixup.Value(resource); } }
/// <summary>Writes a single object to the output XML stream as an instance of the specified type.</summary> /// <param name="value">The value to write.</param> /// <param name="format">The format of the XML.</param> /// <param name="typeSerializer">The type serializer.</param> public void WriteRawObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer) { if (value == null) { throw new ArgumentException("value"); } if (format == null) { throw new ArgumentNullException("format"); } if (typeSerializer == null) { throw new ArgumentNullException("typeSerializer"); } if (!format.FlattenContent) { if (string.IsNullOrEmpty(format.ElementName)) { throw new ArgumentException(Resources.NullElementName); } this.Xml.WriteStartElement(format.ElementName); } typeSerializer.Serialize(this, value, format); if (!format.FlattenContent) { this.Xml.WriteEndElement(); } }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { var result = existingInstance; if (result == null) { try { result = Activator.CreateInstance(TargetType, true); } catch (MissingMethodException e) { throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e); } } // First deserialize the base type. if (_baseSerializer != null) { _baseSerializer.Deserialize(input, format, result); } // Now deserialize our own elements. foreach (var info in _elements) { if (!info.Attribute.FlattenContent) { if (!input.MoveToElement(info.Attribute.ElementName)) { // If the the element was optional then we can // safely skip it and continue. if (info.Attribute.Optional) { continue; } // We failed to find a required element. throw new InvalidContentException(string.Format("The Xml element `{0}` is required!", info.Attribute.ElementName)); } } if (info.Attribute.SharedResource) { Action <object> fixup = (o) => info.Setter(result, o); input.ReadSharedResource(info.Attribute, fixup); } else if (info.Setter == null) { var value = info.Getter(result); input.ReadObject(info.Attribute, info.Serializer, value); } else { var value = input.ReadObject <object>(info.Attribute, info.Serializer); info.Setter(result, value); } } return(result); }
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); }
public void Setup() { attribute = new ContentSerializerAttribute(); attribute.AllowNull = true; attribute.ElementName = "Name"; attribute.FlattenContent = true; }
public void ReadSharedResource <T>(ContentSerializerAttribute format, Action <T> fixup) { string str; if (format.FlattenContent) { str = Xml.ReadContentAsString(); } else { if (!MoveToElement(format.ElementName)) { throw NewInvalidContentException(null, "Element '{0}' was not found.", format.ElementName); } str = Xml.ReadElementContentAsString(); } // Do we already have one for this? Action <object> prevFixup; if (!_resourceFixups.TryGetValue(str, out prevFixup)) { _resourceFixups.Add(str, (o) => fixup((T)o)); } else { _resourceFixups[str] = (o) => { prevFixup(o); fixup((T)o); }; } }
public void Deserialize(IntermediateReader input, ContentSerializerAttribute format, object collection) { if (input == null) { throw new ArgumentNullException("input"); } if (format == null) { throw new ArgumentNullException("format"); } this.ValidateCollectionType(collection); IXmlListItemSerializer xmlListItemSerializer = this.contentSerializer as IXmlListItemSerializer; if (xmlListItemSerializer != null) { XmlListReader xmlListReader = new XmlListReader(input); while (!xmlListReader.AtEnd) { this.addToCollection(collection, xmlListItemSerializer.Deserialize(xmlListReader)); } return; } ContentSerializerAttribute contentSerializerAttribute = new ContentSerializerAttribute(); contentSerializerAttribute.ElementName = format.CollectionItemName; while (input.MoveToElement(format.CollectionItemName)) { this.addToCollection(collection, input.ReadObject <object>(contentSerializerAttribute, this.contentSerializer)); } }
/// <summary>Writes a single object to the output XML stream, using the specified type hint.</summary> /// <param name="value">The value to write.</param> /// <param name="format">The format of the XML.</param> /// <param name="typeSerializer">The type serializer.</param> public void WriteObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer) { if (format == null) { throw new ArgumentNullException("format"); } if (typeSerializer == null) { throw new ArgumentNullException("typeSerializer"); } if (!format.FlattenContent) { if (string.IsNullOrEmpty(format.ElementName)) { throw new ArgumentException(Resources.NullElementName); } this.Xml.WriteStartElement(format.ElementName); } if (value == null) { if (format.FlattenContent) { throw new InvalidOperationException(Resources.CantWriteNullInFlattenContentMode); } this.Xml.WriteAttributeString("Null", "true"); } else { Type type = value.GetType(); if (type.IsSubclassOf(typeof(Type))) { type = typeof(Type); } if (type != typeSerializer.BoxedTargetType) { if (format.FlattenContent) { throw new InvalidOperationException(Resources.CantWriteDynamicTypesInFlattenContentMode); } typeSerializer = this.Serializer.GetTypeSerializer(type); this.Xml.WriteStartAttribute("Type"); this.WriteTypeName(typeSerializer.TargetType); this.Xml.WriteEndAttribute(); } if (this.recurseDetector.ContainsKey(value)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.FoundCyclicReference, new object[] { value })); } this.recurseDetector.Add(value, true); typeSerializer.Serialize(this, value, format); this.recurseDetector.Remove(value); } if (!format.FlattenContent) { this.Xml.WriteEndElement(); } }
public NonGenericIDictionarySerializer(Type targetType) : base(targetType) { this.keyFormat = new ContentSerializerAttribute(); this.valueFormat = new ContentSerializerAttribute(); this.keyFormat.ElementName = "Key"; this.valueFormat.ElementName = "Value"; }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { if (input == null) { throw new ArgumentNullException("input"); } if (format == null) { throw new ArgumentNullException("format"); } IDictionary dictionary; if (existingInstance == null) { dictionary = (IDictionary)Activator.CreateInstance(base.TargetType); } else { dictionary = this.CastType(existingInstance); } while (input.MoveToElement(format.CollectionItemName)) { input.Xml.ReadStartElement(); object key = input.ReadObject <object>(this.keyFormat); object value = input.ReadObject <object>(this.valueFormat); dictionary.Add(key, value); input.Xml.ReadEndElement(); } return(dictionary); }
protected internal override List <T> Deserialize(IntermediateReader input, ContentSerializerAttribute format, List <T> existingInstance) { var result = existingInstance ?? new List <T>(); var elementSerializer = _itemSerializer as ElementSerializer <T>; if (elementSerializer != null) { elementSerializer.Deserialize(input, result); } else { // Create the item serializer attribute. var itemFormat = new ContentSerializerAttribute(); itemFormat.ElementName = format.CollectionItemName; // Read all the items. while (input.MoveToElement(itemFormat.ElementName)) { var value = input.ReadObject <T>(itemFormat, _itemSerializer); result.Add(value); } } return(result); }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { if (input == null) { throw new ArgumentNullException("input"); } if (format == null) { throw new ArgumentNullException("format"); } IList list; if (existingInstance == null) { list = (IList)Activator.CreateInstance(base.TargetType); } else { list = this.CastType(existingInstance); } ContentSerializerAttribute contentSerializerAttribute = new ContentSerializerAttribute(); contentSerializerAttribute.ElementName = format.CollectionItemName; while (input.MoveToElement(format.CollectionItemName)) { list.Add(input.ReadObject <object>(contentSerializerAttribute)); } return(list); }
public static T Deserialize <T>(XmlReader input, string referenceRelocationPath) { var serializer = new IntermediateSerializer(); var reader = new IntermediateReader(serializer, input, referenceRelocationPath); if (!reader.MoveToElement("XnaContent")) { throw new InvalidContentException(string.Format("Could not find XnaContent element in '{0}'.", referenceRelocationPath)); } // Initialize the namespace lookups from // the attributes on the XnaContent element. serializer.CreateNamespaceLookup(input); // Move past the XnaContent. input.ReadStartElement(); // Read the asset. var format = new ContentSerializerAttribute { ElementName = "Asset" }; var asset = reader.ReadObject <T>(format); // TODO: Read the shared resources and external // references here! // Move past the closing XnaContent element. input.ReadEndElement(); return(asset); }
protected override void Serialize(IntermediateWriter output, SharedResourceDictionary <TKey, TValue> value, ContentSerializerAttribute format) { foreach (TKey key in value.Keys) { if (default(TKey) is ValueType) { output.WriteObject(key, Keyformat); } else { output.WriteSharedResource(key, Keyformat); } if (default(TValue) is ValueType) { output.WriteObject(value[key], Valueformat); } else { output.WriteSharedResource(value[key], Valueformat); } } }
/// <summary> /// Deserializes the derived fields. /// </summary> /// <param name="input">The input.</param> /// <param name="format">The format.</param> /// <param name="existingInstance">The existing instance.</param> protected override void DeserializeDerivedFields(IntermediateReader input, ContentSerializerAttribute format, BoxEmitter existingInstance) { existingInstance.Width = input.ReadObject <Single>("Width"); existingInstance.Height = input.ReadObject <Single>("Height"); existingInstance.Depth = input.ReadObject <Single>("Depth"); existingInstance.Rotation = input.ReadObject <Vector3>("Rotation"); }
/// <summary>Adds a shared reference to the output XML and records the object to be serialized later.</summary> /// <param name="value">The value to write.</param> /// <param name="format">The format of the XML.</param> public void WriteSharedResource <T>(T value, ContentSerializerAttribute format) { if (format == null) { throw new ArgumentNullException("format"); } if (!format.FlattenContent) { if (string.IsNullOrEmpty(format.ElementName)) { throw new ArgumentException(Resources.NullElementName); } this.Xml.WriteStartElement(format.ElementName); } if (value != null) { string text; if (!this.sharedResourceNames.TryGetValue(value, out text)) { text = "#Resource" + (this.sharedResourceNames.Count + 1).ToString(CultureInfo.InvariantCulture); this.sharedResourceNames.Add(value, text); this.sharedResources.Enqueue(value); } this.Xml.WriteString(text); } if (!format.FlattenContent) { this.Xml.WriteEndElement(); } }
protected override LayerMask Deserialize(IntermediateReader input, ContentSerializerAttribute format, LayerMask existingInstance) { return(new LayerMask() { value = input.Xml.ReadContentAsInt() }); }
protected internal override T?Deserialize(IntermediateReader input, ContentSerializerAttribute format, T?existingInstance) { var list = new List <T>(); _serializer.Deserialize(input, list); return(list.First()); }
internal void ReadSharedResources() { if (!MoveToElement("Resources")) { return; } var resources = new Dictionary <string, object>(); var resourceFormat = new ContentSerializerAttribute { ElementName = "Resource" }; // Read all the resources. Xml.ReadStartElement(); while (MoveToElement("Resource")) { var id = Xml.GetAttribute("ID"); var resource = ReadObject <object>(resourceFormat); resources.Add(id, resource); } Xml.ReadEndElement(); // Execute the fixups. foreach (var fixup in _resourceFixups) { var resouce = resources[fixup.Key]; fixup.Value(resouce); } }
protected override Vector4[] Deserialize(IntermediateReader input, ContentSerializerAttribute format, Vector4[] existingInstance) { string str = input.Xml.ReadContentAsString().Trim(); if (string.IsNullOrWhiteSpace(str)) { return(new Vector4[0]); } string[] s = str.Split(' '); if ((s.Length % 4) != 0) { throw new Exception(String.Format("Not enough floats in the string for Vector4[] in element {0}. Was {1}.", format.ElementName, s.Length)); } Vector4[] vs = new Vector4[s.Length / 4]; for (int i = 0; i < vs.Length; i++) { try { int triIndex = i * 4; vs[i][0] = float.Parse(s[triIndex]); vs[i][1] = float.Parse(s[triIndex + 1]); vs[i][2] = float.Parse(s[triIndex + 2]); } catch (IndexOutOfRangeException) { throw new IndexOutOfRangeException("Error reading Vector4[] from string in element " + format.ElementName + ". i was " + i + " for a limit of " + vs.Length + " using " + s.Length + " numbers"); } } return(vs); }
/// <summary> /// Deserializes a $safeitemname$ object from intermediate XML format. /// </summary> /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param> /// <param name="format">Specifies the intermediate source XML format.</param> /// <param name="existingInstance">The strongly typed object containing the received data, or null if the /// deserializer should construct a new instance.</param> /// <returns>A deserialized $safeitemname$ instance.</returns> protected override $safeitemname$ Deserialize(IntermediateReader input, ContentSerializerAttribute format, $safeitemname$ existingInstance) { $safeitemname$ value = existingInstance ?? default($safeitemname$); // TODO deserialize $safeitemname$ fields & properties... return(value); }
protected internal override Type Deserialize(IntermediateReader input, ContentSerializerAttribute format, Type existingInstance) { if (input == null) { throw new ArgumentNullException("input"); } return(input.ReadTypeName()); }
/// <summary> /// Deserializes a CooldownController object from intermediate XML format. /// </summary> /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param> /// <param name="format">Specifies the intermediate source XML format.</param> /// <param name="existingInstance">The strongly typed object containing the received data, or null if the /// deserializer should construct a new instance.</param> /// <returns>A deserialized CooldownController instance.</returns> protected override CooldownController Deserialize(IntermediateReader input, ContentSerializerAttribute format, CooldownController existingInstance) { CooldownController value = existingInstance ?? new CooldownController(); value.CooldownPeriod = input.ReadObject <Single>("CooldownPeriod"); return(value); }
protected internal override DateTime Deserialize(IntermediateReader input, ContentSerializerAttribute format, DateTime existingInstance) { if (input == null) { throw new ArgumentNullException("input"); } return(XmlConvert.ToDateTime(input.Xml.ReadContentAsString(), XmlDateTimeSerializationMode.RoundtripKind)); }
protected internal override TimeSpan Deserialize(IntermediateReader input, ContentSerializerAttribute format, TimeSpan existingInstance) { if (input == null) { throw new ArgumentNullException("input"); } return(XmlConvert.ToTimeSpan(input.Xml.ReadContentAsString())); }
/// <summary> /// Deserializes a RotationModifier object from intermediate XML format. /// </summary> /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param> /// <param name="format">Specifies the intermediate source XML format.</param> /// <param name="existingInstance">The strongly typed object containing the received data, or null if the /// deserializer should construct a new instance.</param> /// <returns>A deserialized RotationModifier instance.</returns> protected override RotationModifier Deserialize(IntermediateReader input, ContentSerializerAttribute format, RotationModifier existingInstance) { RotationModifier value = existingInstance ?? new RotationModifier(); value.RotationRate = input.ReadObject <Vector3>("RotationRate"); return(value); }
protected internal override void Initialize(IntermediateSerializer serializer) { _serializer = serializer.GetTypeSerializer(typeof(T)); _format = new ContentSerializerAttribute { FlattenContent = true }; }
/// <summary> /// Deserializes a HueShiftModifier object from intermediate XML format. /// </summary> /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param> /// <param name="format">Specifies the intermediate source XML format.</param> /// <param name="existingInstance">The strongly typed object containing the received data, or null if the /// deserializer should construct a new instance.</param> /// <returns>A deserialized HueShiftModifier instance.</returns> protected override HueShiftModifier Deserialize(IntermediateReader input, ContentSerializerAttribute format, HueShiftModifier existingInstance) { HueShiftModifier value = existingInstance ?? new HueShiftModifier(); value.HueShift = input.ReadObject <Single>("HueShift"); return(value); }
protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format) { // Create the item serializer attribute. var itemFormat = new ContentSerializerAttribute(); itemFormat.ElementName = format.CollectionItemName; // Read all the items. foreach (var item in (IList) value) output.WriteObject(item, itemFormat); }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { var str = input.Xml.ReadString(); try { return Enum.Parse(TargetType, str, true); } catch (Exception ex) { throw input.NewInvalidContentException(ex, "Invalid enum value '{0}' for type '{1}'", str, TargetType.Name); } }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { var result = (IList) (existingInstance ?? Activator.CreateInstance(TargetType)); // Create the item serializer attribute. var itemFormat = new ContentSerializerAttribute(); itemFormat.ElementName = format.CollectionItemName; // Read all the items. while (input.MoveToElement(itemFormat.ElementName)) { var value = input.ReadObject<object>(itemFormat); result.Add(value); } return result; }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { var result = existingInstance; if (result == null) { try { result = Activator.CreateInstance(TargetType, true); } catch (MissingMethodException e) { throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e); } } // First deserialize the base type. if (_baseSerializer != null) _baseSerializer.Deserialize(input, format, result); // Now deserialize our own elements. foreach (var info in _elements) { if (!info.Attribute.FlattenContent) { if (!input.MoveToElement(info.Attribute.ElementName)) { // If the the element was optional then we can // safely skip it and continue. if (info.Attribute.Optional) continue; // We failed to find a required element. throw new InvalidContentException(string.Format("The Xml element `{0}` is required!", info.Attribute.ElementName)); } } if (info.Attribute.SharedResource) { Action<object> fixup = (o) => info.Setter(result, o); input.ReadSharedResource(info.Attribute, fixup); } else if (info.Setter == null) { var value = info.Getter(result); input.ReadObject(info.Attribute, info.Serializer, value); } else { var value = input.ReadObject<object>(info.Attribute, info.Serializer); info.Setter(result, value); } } return result; }
protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { var result = existingInstance; if (result == null) { try { result = Activator.CreateInstance(TargetType, true); } catch (MissingMethodException e) { throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e); } } var reader = input.Xml; var depth = reader.Depth; // Read the next node. while (reader.Read()) { // Did we reach the end of this object? if (reader.NodeType == XmlNodeType.EndElement) break; Debug.Assert(reader.Depth == depth, "We are not at the right depth!"); if (reader.NodeType == XmlNodeType.Element) { var elementName = reader.Name; reader.ReadStartElement(); ElementInfo info; if (!_elements.TryGetValue(elementName, out info)) throw new InvalidContentException(string.Format("Element `{0}` was not found in type `{1}`.", elementName, TargetType)); var value = info.Serializer.Deserialize(input, format, null); info.Setter(result, value); reader.ReadEndElement(); continue; } // If we got here then we were not interested // in this node... so skip its children. reader.Skip(); } return result; }
protected internal override object Deserialize (IntermediateReader input, ContentSerializerAttribute format, object existingInstance) { throw new System.NotImplementedException(); }
protected internal override void Serialize (IntermediateWriter output, object value, ContentSerializerAttribute format) { throw new System.NotImplementedException(); }
protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format) { Debug.Assert(value.GetType() == TargetType, "Got invalid value type!"); output.Xml.WriteString(value.ToString()); }
protected internal abstract object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance);
protected internal abstract void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format);
protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format) { // First serialize the base type. if (_baseSerializer != null) _baseSerializer.Serialize(output, value, format); // Now serialize our own elements. foreach (var info in _elements) { var elementValue = info.Getter(value); if (info.Attribute.SharedResource) output.WriteSharedResource(elementValue, info.Attribute); else output.WriteObjectInternal(elementValue, info.Attribute, info.Serializer, info.Serializer.TargetType); } }