-Fixed XmlNodeConverter not turning all attribute properties into attributes

-Added .gitattributes file
This commit is contained in:
James Newton-King
2012-03-31 15:39:53 +13:00
parent b10ad030ac
commit a139d7bf20
5 changed files with 3520 additions and 3445 deletions
+49
View File
@@ -0,0 +1,49 @@
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
*.jpg binary
*.png binary
*.gif binary
*.cs text diff=csharp
*.vb text
*.c text
*.cpp text
*.cxx text
*.h text
*.hxx text
*.py text
*.rb text
*.java text
*.html text
*.htm text
*.css text
*.scss text
*.sass text
*.less text
*.js text
*.lisp text
*.clj text
*.sql text
*.php text
*.lua text
*.m text
*.asm text
*.erl text
*.fs text
*.fsx text
*.hs text
*.csproj text merge=union
*.vbproj text merge=union
*.fsproj text merge=union
*.dbproj text merge=union
*.sln text eol=crlf merge=union
+1 -1
View File
@@ -17,7 +17,7 @@
$workingDir = "$baseDir\Working" $workingDir = "$baseDir\Working"
$builds = @( $builds = @(
@{Name = "Newtonsoft.Json"; TestsName = "Newtonsoft.Json.Tests"; Constants=""; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json"; TestsName = "Newtonsoft.Json.Tests"; Constants=""; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Metro"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="Metro"; NuGetDir = "winrt45"; Framework="net-4.5"; Sign=$true}, #@{Name = "Newtonsoft.Json.Metro"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="Metro"; NuGetDir = "winrt45"; Framework="net-4.5"; Sign=$true},
# unsigned SL/WP # unsigned SL/WP
@{Name = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "sl3-wp,sl4-windowsphone71"; Framework="net-4.0"; Sign=$false}, @{Name = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "sl3-wp,sl4-windowsphone71"; Framework="net-4.0"; Sign=$false},
@@ -383,12 +383,12 @@ namespace Newtonsoft.Json.Tests.Converters
""@class"": ""vevent"", ""@class"": ""vevent"",
""a"": { ""a"": {
""@class"": ""url"", ""@class"": ""url"",
""@href"": ""http://www.web2con.com/"",
""span"": { ""span"": {
""@class"": ""summary"", ""@class"": ""summary"",
""#text"": ""Web 2.0 Conference"", ""#text"": ""Web 2.0 Conference"",
""#cdata-section"": ""my escaped text"" ""#cdata-section"": ""my escaped text""
} },
""@href"": ""http://www.web2con.com/""
} }
} }
}"; }";
@@ -481,7 +481,7 @@ namespace Newtonsoft.Json.Tests.Converters
[Test] [Test]
public void OtherElementDataTypes() public void OtherElementDataTypes()
{ {
string jsonText = @"{""?xml"":{""@version"":""1.0"",""@standalone"":""no""},""root"":{""person"":[{""@id"":""1"",""Float"":2.5,""Integer"":99},{""@id"":""2"",""Boolean"":true,""date"":""\/Date(954374400000)\/""}]}}"; string jsonText = @"{""?xml"":{""@version"":""1.0"",""@standalone"":""no""},""root"":{""person"":[{""@id"":""1"",""Float"":2.5,""Integer"":99},{""Boolean"":true,""@id"":""2"",""date"":""\/Date(954374400000)\/""}]}}";
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText); XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
@@ -1796,6 +1796,16 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(expectedXaml, xaml2); Assert.AreEqual(expectedXaml, xaml2);
} }
[Test]
public void DeserializeAttributePropertyNotAtStart()
{
string json = @"{""item"": {""@action"": ""update"", ""@itemid"": ""1"", ""elements"": [{""@action"": ""none"", ""@id"": ""2""},{""@action"": ""none"", ""@id"": ""3""}],""@description"": ""temp""}}";
XmlDocument xmldoc = JsonConvert.DeserializeXmlNode(json);
Assert.AreEqual(@"<item action=""update"" itemid=""1"" description=""temp""><elements action=""none"" id=""2"" /><elements action=""none"" id=""3"" /></item>", xmldoc.InnerXml);
}
} }
} }
#endif #endif
@@ -1183,44 +1183,60 @@ namespace Newtonsoft.Json.Converters
string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName);
IXmlElement element = CreateElement(propertyName, document, elementPrefix, manager); if (propertyName.StartsWith("@"))
currentNode.AppendChild(element);
// add attributes to newly created element
foreach (KeyValuePair<string, string> nameValue in attributeNameValues)
{ {
string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key); var attributeName = propertyName.Substring(1);
var attributeValue = reader.Value.ToString();
IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) var attributePrefix = MiscellaneousUtils.GetPrefix(attributeName);
? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix), nameValue.Value)
: document.CreateAttribute(nameValue.Key, nameValue.Value);
element.SetAttributeNode(attribute); var attribute = (!string.IsNullOrEmpty(attributePrefix))
} ? document.CreateAttribute(attributeName, manager.LookupNamespace(attributePrefix), attributeValue)
: document.CreateAttribute(attributeName, attributeValue);
if (reader.TokenType == JsonToken.String ((IXmlElement)currentNode).SetAttributeNode(attribute);
|| reader.TokenType == JsonToken.Integer
|| reader.TokenType == JsonToken.Float
|| reader.TokenType == JsonToken.Boolean
|| reader.TokenType == JsonToken.Date)
{
element.AppendChild(document.CreateTextNode(ConvertTokenToXmlValue(reader)));
}
else if (reader.TokenType == JsonToken.Null)
{
// empty element. do nothing
} }
else else
{ {
// finished element will have no children to deserialize IXmlElement element = CreateElement(propertyName, document, elementPrefix, manager);
if (reader.TokenType != JsonToken.EndObject)
currentNode.AppendChild(element);
// add attributes to newly created element
foreach (KeyValuePair<string, string> nameValue in attributeNameValues)
{ {
manager.PushScope(); string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key);
DeserializeNode(reader, document, manager, element); IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix))
? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix), nameValue.Value)
: document.CreateAttribute(nameValue.Key, nameValue.Value);
manager.PopScope(); element.SetAttributeNode(attribute);
}
if (reader.TokenType == JsonToken.String
|| reader.TokenType == JsonToken.Integer
|| reader.TokenType == JsonToken.Float
|| reader.TokenType == JsonToken.Boolean
|| reader.TokenType == JsonToken.Date)
{
element.AppendChild(document.CreateTextNode(ConvertTokenToXmlValue(reader)));
}
else if (reader.TokenType == JsonToken.Null)
{
// empty element. do nothing
}
else
{
// finished element will have no children to deserialize
if (reader.TokenType != JsonToken.EndObject)
{
manager.PushScope();
DeserializeNode(reader, document, manager, element);
manager.PopScope();
}
} }
} }
} }