From 0c6b0748f8863a5465fda88a976f7eb9aac2e70f Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 15 Oct 2018 02:18:44 +1300 Subject: [PATCH] Fix including array attribute in XML with namespaces (#1880) --- Src/Newtonsoft.Json.Tests/Issues/Issue1734.cs | 318 ++++++++++++++++++ .../Converters/XmlNodeConverter.cs | 7 +- 2 files changed, 323 insertions(+), 2 deletions(-) create mode 100644 Src/Newtonsoft.Json.Tests/Issues/Issue1734.cs diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue1734.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue1734.cs new file mode 100644 index 00000000..a8447519 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue1734.cs @@ -0,0 +1,318 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(DNXCORE50 || PORTABLE40) || NETSTANDARD1_3 || NETSTANDARD2_0 +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Linq.JsonPath; +using System; +using System.Collections.Generic; +using System.Xml; +using System.IO; +#if !NET20 +using System.Xml.Linq; +#endif +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests.Issues +{ + [TestFixture] + public class Issue1734 + { +#if !(PORTABLE || PORTABLE40) + [Test] + public void Test_XmlNode() + { + XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(JsonWithoutNamespace, "", true); + + StringAssert.AreEqual(@" + mark + joye + saab + 9741 + true + ABS + AirBags + childdoorlock + test Car + + 1 + 11 + + DOM + 8787 + + + COM + 4564 + + 54657667 + + + + gf + 123 + + 123 + + + +", IndentXml(xmlDoc.OuterXml)); + + xmlDoc = JsonConvert.DeserializeXmlNode(JsonWithNamespace, "", true); + + StringAssert.AreEqual(@" + mark + joye + saab + 9741 + true + ABS + AirBags + childdoorlock + test Car + + 1 + 11 + + DOM + 8787 + + + COM + 4564 + + 54657667 + + + + gf + 123 + + 123 + + + +", IndentXml(xmlDoc.OuterXml)); + } + + private string IndentXml(string xml) + { + XmlReader reader = XmlReader.Create(new StringReader(xml)); + + StringWriter sw = new StringWriter(); + XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }); + + while (reader.Read()) + { + writer.WriteNode(reader, false); + } + + writer.Flush(); + + return sw.ToString(); + } +#endif + +#if !NET20 + [Test] + public void Test_XNode() + { + XDocument xmlDoc = JsonConvert.DeserializeXNode(JsonWithoutNamespace, "", true); + + string xml = xmlDoc.ToString(); + StringAssert.AreEqual(@" + mark + joye + saab + 9741 + true + ABS + AirBags + childdoorlock + test Car + + 1 + 11 + + DOM + 8787 + + + COM + 4564 + + 54657667 + + + + gf + 123 + + 123 + + + +", xml); + + xmlDoc = JsonConvert.DeserializeXNode(JsonWithNamespace, "", true); + + xml = xmlDoc.ToString(); + StringAssert.AreEqual(@" + mark + joye + saab + 9741 + true + ABS + AirBags + childdoorlock + test Car + + 1 + 11 + + DOM + 8787 + + + COM + 4564 + + 54657667 + + + + gf + 123 + + 123 + + + +", xml); + } +#endif + + private const string JsonWithoutNamespace = @"{ + ""Test_Service"": { + ""fname"": ""mark"", + ""lname"": ""joye"", + ""CarCompany"": ""saab"", + ""CarNumber"": ""9741"", + ""IsInsured"": ""true"", + ""safty"": [ + ""ABS"", + ""AirBags"", + ""childdoorlock"" + ], + ""CarDescription"": ""test Car"", + ""collections"": [ + { + ""XYZ"": ""1"", + ""PQR"": ""11"", + ""contactdetails"": [ + { + ""contname"": ""DOM"", + ""contnumber"": ""8787"" + }, + { + ""contname"": ""COM"", + ""contnumber"": ""4564"", + ""addtionaldetails"": [ + { + ""description"": ""54657667"" + } + ] + }, + { + ""contname"": ""gf"", + ""contnumber"": ""123"", + ""addtionaldetails"": [ + { + ""description"": ""123"" + } + ] + } + ] + } + ] + } +}"; + + private const string JsonWithNamespace = @"{ + ""ns3:Test_Service"": { + ""@xmlns:ns3"": ""http://www.CCKS.org/XRT/Form"", + ""ns3:fname"": ""mark"", + ""ns3:lname"": ""joye"", + ""ns3:CarCompany"": ""saab"", + ""ns3:CarNumber"": ""9741"", + ""ns3:IsInsured"": ""true"", + ""ns3:safty"": [ + ""ABS"", + ""AirBags"", + ""childdoorlock"" + ], + ""ns3:CarDescription"": ""test Car"", + ""ns3:collections"": [ + { + ""ns3:XYZ"": ""1"", + ""ns3:PQR"": ""11"", + ""ns3:contactdetails"": [ + { + ""ns3:contname"": ""DOM"", + ""ns3:contnumber"": ""8787"" + }, + { + ""ns3:contname"": ""COM"", + ""ns3:contnumber"": ""4564"", + ""ns3:addtionaldetails"": [ + { + ""ns3:description"": ""54657667"" + } + ] + }, + { + ""ns3:contname"": ""gf"", + ""ns3:contnumber"": ""123"", + ""ns3:addtionaldetails"": [ + { + ""ns3:description"": ""123"" + } + ] + } + ] + } + ] + } +}"; + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index ac598929..6e7ba059 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -1464,7 +1464,7 @@ namespace Newtonsoft.Json.Converters } #endregion -#region Reading + #region Reading /// /// Reads the JSON representation of the object. /// @@ -2086,9 +2086,12 @@ namespace Newtonsoft.Json.Converters if (count == 1 && WriteArrayAttribute) { + MiscellaneousUtils.GetQualifiedNameParts(propertyName, out string elementPrefix, out string localName); + string ns = string.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix); + foreach (IXmlNode childNode in currentNode.ChildNodes) { - if (childNode is IXmlElement element && element.LocalName == propertyName) + if (childNode is IXmlElement element && element.LocalName == localName && element.NamespaceUri == ns) { AddJsonArrayAttribute(element, document); break;