Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.5 Incorrect deserialization of whitespaces #82

Merged
merged 1 commit into from Jul 7, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -242,6 +242,28 @@ public void TestDataContractSerializer()
Debug.WriteLine("deserializing: " + sw.Elapsed);
}

[Test]
public void NestedObjectWithNullPropertiesShouldBeSerialized()
{
var mapper = new MessageMapper();
var serializer = new MessageSerializer();
serializer.MessageMapper = mapper;

serializer.MessageTypes = new List<Type> { typeof(MessageWithNestedObject) };

var msg = new MessageWithNestedObject { NestedObject = new MessageWithListItem() };

using (var stream = new MemoryStream())
{
serializer.Serialize(new[] { msg }, stream);
stream.Position = 0;

var msgArray = serializer.Deserialize(stream);
var actualMessage = (MessageWithNestedObject)msgArray[0];
Assert.IsNotNull(actualMessage.NestedObject);
}
}

private void DataContractSerialize(XmlWriterSettings xws, DataContractSerializer dcs, IMessage[] messages, Stream str)
{
ArrayList o = new ArrayList(messages);
Expand Down Expand Up @@ -381,4 +403,9 @@ public class MessageWithList : IMessage
{
public List<MessageWithListItem> Items { get; set; }
}

public class MessageWithNestedObject : IMessage
{
public MessageWithListItem NestedObject { get; set; }
}
}
Expand Up @@ -442,25 +442,12 @@ private FieldInfo GetField(Type t, string name)

private object GetPropertyValue(Type type, XmlNode n)
{
if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlWhitespace)
{
if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
{
var list = CreateList(type);
if (type.IsArray)
return list.GetType().GetMethod("ToArray").Invoke(list, null);
return list;
}

return n.ChildNodes[0].InnerText;
}

if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlText)
if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlCharacterData)
{
var text = n.ChildNodes[0].InnerText;

var args = type.GetGenericArguments();
if (args.Length == 1)
if (args.Length == 1 && args[0].IsValueType)
{
var nullableType = typeof (Nullable<>).MakeGenericType(args);
if (type == nullableType)
Expand Down Expand Up @@ -535,7 +522,8 @@ private object GetPropertyValue(Type type, XmlNode n)
if (type == typeof(Uri))
return new Uri(text);

throw new Exception("Type not supported by the serializer: " + type.AssemblyQualifiedName);
if (!(n.ChildNodes[0] is XmlWhitespace))
throw new Exception("Type not supported by the serializer: " + type.AssemblyQualifiedName);
}

//Handle dictionaries
Expand Down