From 29c84fd7c2cd7be96864b0d1d1b4a22ff84ab606 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Tue, 25 Oct 2016 14:43:27 +1300 Subject: [PATCH] -Improved error when attempting to convert root JSON object property to an XML attribute --- .../Converters/XmlNodeConverterTest.cs | 29 +++++++++++++++++++ .../Converters/XmlNodeConverter.cs | 11 +++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs index 10521a17..142fba48 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs @@ -2701,6 +2701,35 @@ namespace Newtonsoft.Json.Tests.Converters Assert.AreEqual(json, json2); } + [Test] + public void RootPropertyError() + { + string json = @"{ + ""$id"": ""1"", + ""AOSLocaleName"": ""en-US"", + ""AXLanguage"": ""EN-AU"", + ""Company"": ""AURE"", + ""CompanyTimeZone"": 8, + ""CurrencyInfo"": { + ""$id"": ""2"", + ""CurrencyCode"": ""AUD"", + ""Description"": ""Australian Dollar"", + ""ExchangeRate"": 100.0, + ""ISOCurrencyCode"": ""AUD"", + ""Prefix"": """", + ""Suffix"": """" + }, + ""IsSysAdmin"": true, + ""UserId"": ""lamar.miller"", + ""UserPreferredCalendar"": 0, + ""UserPreferredTimeZone"": 8 +}"; + + ExceptionAssert.Throws( + () => JsonConvert.DeserializeXmlNode(json), + "JSON root object has property '$id' that will be converted to an attribute. A root object cannot have any attribute properties. Consider specifing a DeserializeRootElementName. Path '$id', line 2, position 12."); + } + [Test] public void SerializeEmptyNodeAndOmitRoot() { diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index 731be23b..61411cd1 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -1538,7 +1538,7 @@ namespace Newtonsoft.Json.Converters string attributeName = propertyName.Substring(1); string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName); - AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix); + AddAttribute(reader, document, currentNode, propertyName, attributeName, manager, attributePrefix); return; } @@ -1557,7 +1557,7 @@ namespace Newtonsoft.Json.Converters case JsonTypeReflector.ValuePropertyName: string attributeName = propertyName.Substring(1); string attributePrefix = manager.LookupPrefix(JsonNamespaceUri); - AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix); + AddAttribute(reader, document, currentNode, propertyName, attributeName, manager, attributePrefix); return; } } @@ -1614,8 +1614,13 @@ namespace Newtonsoft.Json.Converters } } - private static void AddAttribute(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string attributeName, XmlNamespaceManager manager, string attributePrefix) + private static void AddAttribute(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, string attributeName, XmlNamespaceManager manager, string attributePrefix) { + if (currentNode.NodeType == XmlNodeType.Document) + { + throw JsonSerializationException.Create(reader, "JSON root object has property '{0}' that will be converted to an attribute. A root object cannot have any attribute properties. Consider specifing a DeserializeRootElementName.".FormatWith(CultureInfo.InvariantCulture, propertyName)); + } + string encodedName = XmlConvert.EncodeName(attributeName); string attributeValue = reader.Value.ToString();