-Fix converting unmatched namespace prefixes in JSON to XML
This commit is contained in:
@@ -372,8 +372,46 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
var equals = XElement.DeepEquals(xmlBack, xml);
|
var equals = XElement.DeepEquals(xmlBack, xml);
|
||||||
Assert.IsTrue(equals);
|
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
|
#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]
|
[Test]
|
||||||
public void DocumentSerializeIndented()
|
public void DocumentSerializeIndented()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1376,7 +1376,7 @@ namespace Newtonsoft.Json.Converters
|
|||||||
string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key);
|
string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key);
|
||||||
|
|
||||||
IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix))
|
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);
|
: document.CreateAttribute(nameValue.Key, nameValue.Value);
|
||||||
|
|
||||||
element.SetAttributeNode(attribute);
|
element.SetAttributeNode(attribute);
|
||||||
|
|||||||
Reference in New Issue
Block a user