-Fix converting unmatched namespace prefixes in JSON to XML

This commit is contained in:
James Newton-King
2015-03-08 11:11:16 +13:00
parent 95e04ba96e
commit 0effb85a31
2 changed files with 39 additions and 1 deletions
@@ -372,8 +372,46 @@ namespace Newtonsoft.Json.Tests.Converters
var equals = XElement.DeepEquals(xmlBack, xml);
Assert.IsTrue(equals);
}
[Test]
public void DeserializeUndeclaredNamespacePrefix()
{
XmlDocument doc = JsonConvert.DeserializeXmlNode("{ A: { '@xsi:nil': true } }");
Console.WriteLine(doc.OuterXml);
XDocument xdoc = JsonConvert.DeserializeXNode("{ A: { '@xsi:nil': true } }");
Assert.AreEqual(doc.OuterXml, xdoc.ToString());
}
#endif
[Test]
public void DeserializeMultipleRootElements()
{
string json = @"{
""Id"": 1,
""Email"": ""james@example.com"",
""Active"": true,
""CreatedDate"": ""2013-01-20T00:00:00Z"",
""Roles"": [
""User"",
""Admin""
],
""Team"": {
""Id"": 2,
""Name"": ""Software Developers"",
""Description"": ""Creators of fine software products and services.""
}
}";
ExceptionAssert.Throws<JsonSerializationException>(
() =>
{
JsonConvert.DeserializeXmlNode(json);
},
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.");
}
[Test]
public void DocumentSerializeIndented()
{
@@ -1376,7 +1376,7 @@ namespace Newtonsoft.Json.Converters
string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key);
IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix))
? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix), nameValue.Value)
? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix) ?? string.Empty, nameValue.Value)
: document.CreateAttribute(nameValue.Key, nameValue.Value);
element.SetAttributeNode(attribute);