-Fixed performance issue serializing XML document with a large number of nodes
This commit is contained in:
@@ -1432,7 +1432,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
}
|
||||
}
|
||||
|
||||
#if !(NET20 || NET35 || NETFX_CORE)
|
||||
#if !(NETFX_CORE)
|
||||
public void Utf8Text()
|
||||
{
|
||||
string badText = System.IO.File.ReadAllText(@"PoisonText.txt");
|
||||
|
||||
@@ -315,6 +315,17 @@
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="bunny_pancake.jpg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="large_sample.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="PoisonText.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
@@ -537,10 +537,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="bunny_pancake.jpg">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="large_sample.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="PoisonText.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -345,10 +345,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="bunny_pancake.jpg">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="large_sample.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="PoisonText.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -342,10 +342,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="bunny_pancake.jpg">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="large_sample.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="PoisonText.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -336,6 +336,9 @@
|
||||
</AppxManifest>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="bunny_pancake.jpg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Images\UnitTestLogo.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
@@ -348,6 +351,12 @@
|
||||
<Content Include="Images\UnitTestStoreLogo.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="large_sample.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="PoisonText.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.WinRT.csproj">
|
||||
|
||||
@@ -493,7 +493,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="bunny_pancake.jpg">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="large_sample.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="PoisonText.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Xml;
|
||||
#if !(NET20 || NET35 || NETFX_CORE || PORTABLE)
|
||||
using System;
|
||||
using System.Collections;
|
||||
@@ -180,6 +181,32 @@ namespace Newtonsoft.Json.Tests
|
||||
SerializeSize(image);
|
||||
}
|
||||
|
||||
#if !(PORTABLE40)
|
||||
[Test]
|
||||
public void ConvertXmlNode()
|
||||
{
|
||||
XmlDocument doc = new XmlDocument();
|
||||
using (FileStream file = System.IO.File.OpenRead("large_sample.xml"))
|
||||
{
|
||||
doc.Load(file);
|
||||
}
|
||||
|
||||
JsonConvert.SerializeXmlNode(doc);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertXNode()
|
||||
{
|
||||
XDocument doc;
|
||||
using (FileStream file = System.IO.File.OpenRead("large_sample.xml"))
|
||||
{
|
||||
doc = XDocument.Load(file);
|
||||
}
|
||||
|
||||
JsonConvert.SerializeXNode(doc);
|
||||
}
|
||||
#endif
|
||||
|
||||
private T TimeOperation<T>(Func<T> operation, string name)
|
||||
{
|
||||
// warm up
|
||||
|
||||
@@ -75,4 +75,4 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.3.17308")]
|
||||
[assembly: AssemblyFileVersion("6.0.3.17309")]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -226,6 +226,7 @@ namespace Newtonsoft.Json.Converters
|
||||
internal class XmlNodeWrapper : IXmlNode
|
||||
{
|
||||
private readonly XmlNode _node;
|
||||
private IList<IXmlNode> _childNodes;
|
||||
|
||||
public XmlNodeWrapper(XmlNode node)
|
||||
{
|
||||
@@ -249,7 +250,15 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
public IList<IXmlNode> ChildNodes
|
||||
{
|
||||
get { return _node.ChildNodes.Cast<XmlNode>().Select(n => WrapNode(n)).ToList(); }
|
||||
get
|
||||
{
|
||||
// childnodes is read multiple times
|
||||
// cache results to prevent multiple reads which kills perf in large documents
|
||||
if (_childNodes == null)
|
||||
_childNodes = _node.ChildNodes.Cast<XmlNode>().Select(WrapNode).ToList();
|
||||
|
||||
return _childNodes;
|
||||
}
|
||||
}
|
||||
|
||||
private IXmlNode WrapNode(XmlNode node)
|
||||
@@ -257,11 +266,11 @@ namespace Newtonsoft.Json.Converters
|
||||
switch (node.NodeType)
|
||||
{
|
||||
case XmlNodeType.Element:
|
||||
return new XmlElementWrapper((XmlElement)node);
|
||||
return new XmlElementWrapper((XmlElement) node);
|
||||
case XmlNodeType.XmlDeclaration:
|
||||
return new XmlDeclarationWrapper((XmlDeclaration)node);
|
||||
return new XmlDeclarationWrapper((XmlDeclaration) node);
|
||||
case XmlNodeType.DocumentType:
|
||||
return new XmlDocumentTypeWrapper((XmlDocumentType)node);
|
||||
return new XmlDocumentTypeWrapper((XmlDocumentType) node);
|
||||
default:
|
||||
return new XmlNodeWrapper(node);
|
||||
}
|
||||
@@ -274,7 +283,7 @@ namespace Newtonsoft.Json.Converters
|
||||
if (_node.Attributes == null)
|
||||
return null;
|
||||
|
||||
return _node.Attributes.Cast<XmlAttribute>().Select(a => WrapNode(a)).ToList();
|
||||
return _node.Attributes.Cast<XmlAttribute>().Select(WrapNode).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,7 +292,7 @@ namespace Newtonsoft.Json.Converters
|
||||
get
|
||||
{
|
||||
XmlNode node = (_node is XmlAttribute)
|
||||
? ((XmlAttribute)_node).OwnerElement
|
||||
? ((XmlAttribute) _node).OwnerElement
|
||||
: _node.ParentNode;
|
||||
|
||||
if (node == null)
|
||||
@@ -301,8 +310,9 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
public IXmlNode AppendChild(IXmlNode newChild)
|
||||
{
|
||||
XmlNodeWrapper xmlNodeWrapper = (XmlNodeWrapper)newChild;
|
||||
XmlNodeWrapper xmlNodeWrapper = (XmlNodeWrapper) newChild;
|
||||
_node.AppendChild(xmlNodeWrapper._node);
|
||||
_childNodes = null;
|
||||
|
||||
return newChild;
|
||||
}
|
||||
@@ -459,7 +469,7 @@ namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
IList<IXmlNode> childNodes = base.ChildNodes;
|
||||
|
||||
if (Document.Declaration != null)
|
||||
if (Document.Declaration != null && childNodes[0].NodeType != XmlNodeType.XmlDeclaration)
|
||||
childNodes.Insert(0, new XDeclarationWrapper(Document.Declaration));
|
||||
|
||||
return childNodes;
|
||||
@@ -640,6 +650,8 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
internal class XContainerWrapper : XObjectWrapper
|
||||
{
|
||||
private IList<IXmlNode> _childNodes;
|
||||
|
||||
private XContainer Container
|
||||
{
|
||||
get { return (XContainer)WrappedNode; }
|
||||
@@ -652,7 +664,15 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
public override IList<IXmlNode> ChildNodes
|
||||
{
|
||||
get { return Container.Nodes().Select(n => WrapNode(n)).ToList(); }
|
||||
get
|
||||
{
|
||||
// childnodes is read multiple times
|
||||
// cache results to prevent multiple reads which kills perf in large documents
|
||||
if (_childNodes == null)
|
||||
_childNodes = Container.Nodes().Select(WrapNode).ToList();
|
||||
|
||||
return _childNodes;
|
||||
}
|
||||
}
|
||||
|
||||
public override IXmlNode ParentNode
|
||||
@@ -691,6 +711,8 @@ namespace Newtonsoft.Json.Converters
|
||||
public override IXmlNode AppendChild(IXmlNode newChild)
|
||||
{
|
||||
Container.Add(newChild.WrappedNode);
|
||||
_childNodes = null;
|
||||
|
||||
return newChild;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,5 +91,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.3.17308")]
|
||||
[assembly: AssemblyFileVersion("6.0.3.17309")]
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
Reference in New Issue
Block a user