-Fixed converting XElements without namespace attribute to JSON

This commit is contained in:
James Newton-King
2016-04-10 11:36:21 +12:00
parent 93ba0c4b84
commit 55c87a0e73
4 changed files with 172 additions and 3 deletions
@@ -2670,6 +2670,32 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual("null", json);
}
[Test]
public void SerializeExplicitAttributeNamespace()
{
var original = XElement.Parse("<MyElement xmlns=\"http://example.com\" />");
Assert.AreEqual(@"<MyElement xmlns=""http://example.com"" />", original.ToString());
var json = JsonConvert.SerializeObject(original);
Assert.AreEqual(@"{""MyElement"":{""@xmlns"":""http://example.com""}}", json);
var deserialized = JsonConvert.DeserializeObject<XElement>(json);
Assert.AreEqual(@"<MyElement xmlns=""http://example.com"" />", deserialized.ToString());
}
[Test]
public void SerializeImplicitAttributeNamespace()
{
var original = new XElement("{http://example.com}MyElement");
Assert.AreEqual(@"<MyElement xmlns=""http://example.com"" />", original.ToString());
var json = JsonConvert.SerializeObject(original);
Assert.AreEqual(@"{""MyElement"":{""@xmlns"":""http://example.com""}}", json);
var deserialized = JsonConvert.DeserializeObject<XElement>(json);
Assert.AreEqual(@"<MyElement xmlns=""http://example.com"" />", deserialized.ToString());
}
#endif
}
}