-Improved error when attempting to convert root JSON object property to an XML attribute
This commit is contained in:
@@ -2701,6 +2701,35 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
Assert.AreEqual(json, json2);
|
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<JsonSerializationException>(
|
||||||
|
() => 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]
|
[Test]
|
||||||
public void SerializeEmptyNodeAndOmitRoot()
|
public void SerializeEmptyNodeAndOmitRoot()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1538,7 +1538,7 @@ namespace Newtonsoft.Json.Converters
|
|||||||
string attributeName = propertyName.Substring(1);
|
string attributeName = propertyName.Substring(1);
|
||||||
string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName);
|
string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName);
|
||||||
|
|
||||||
AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix);
|
AddAttribute(reader, document, currentNode, propertyName, attributeName, manager, attributePrefix);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1557,7 +1557,7 @@ namespace Newtonsoft.Json.Converters
|
|||||||
case JsonTypeReflector.ValuePropertyName:
|
case JsonTypeReflector.ValuePropertyName:
|
||||||
string attributeName = propertyName.Substring(1);
|
string attributeName = propertyName.Substring(1);
|
||||||
string attributePrefix = manager.LookupPrefix(JsonNamespaceUri);
|
string attributePrefix = manager.LookupPrefix(JsonNamespaceUri);
|
||||||
AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix);
|
AddAttribute(reader, document, currentNode, propertyName, attributeName, manager, attributePrefix);
|
||||||
return;
|
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 encodedName = XmlConvert.EncodeName(attributeName);
|
||||||
string attributeValue = reader.Value.ToString();
|
string attributeValue = reader.Value.ToString();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user