Skip to content

Commit

Permalink
Fix XmlSerialization for MongoSymbol.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 22, 2010
1 parent 922b3d8 commit 9fbba0f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
13 changes: 13 additions & 0 deletions source/MongoDB.Tests/UnitTests/TestMongoSymbol.cs
Expand Up @@ -147,5 +147,18 @@ public void CanBeXmlSerialized()

Assert.AreEqual(source, dest);
}

[Test]
public void CanBeXmlSerializedWhenValueNull()
{
var source = new MongoSymbol(null);
var serializer = new XmlSerializer(typeof(MongoSymbol));

var writer = new StringWriter();
serializer.Serialize(writer, source);
var dest = (MongoSymbol)serializer.Deserialize(new StringReader(writer.ToString()));

Assert.AreEqual(source, dest);
}
}
}
38 changes: 37 additions & 1 deletion source/MongoDB/MongoSymbol.cs
@@ -1,12 +1,15 @@
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace MongoDB
{
/// <summary>
/// Type to hold an interned string that maps to the bson symbol type.
/// </summary>
[Serializable]
public struct MongoSymbol : IEquatable<MongoSymbol>, IEquatable<String>, IComparable<MongoSymbol>, IComparable<String>
public struct MongoSymbol : IEquatable<MongoSymbol>, IEquatable<String>, IComparable<MongoSymbol>, IComparable<String>, IXmlSerializable
{
/// <summary>
/// Gets or sets the empty.
Expand Down Expand Up @@ -232,5 +235,38 @@ public MongoSymbol(string value)
public override int GetHashCode(){
return (Value != null ? Value.GetHashCode() : 0);
}

/// <summary>
/// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
/// </summary>
/// <returns>
/// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}

/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
if(reader.IsEmptyElement)
return;

Value = string.Intern(reader.ReadString());
}

/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
if(Value != null)
writer.WriteString(Value);
}
}
}

0 comments on commit 9fbba0f

Please sign in to comment.