diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
index c53bcd9a..3c4b04e9 100644
--- a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
@@ -882,6 +882,63 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.IsNull(reader.ReadAsBytes());
}
+
+ [Test]
+ public void WriteEndOnProperty()
+ {
+ MemoryStream ms = new MemoryStream();
+ BsonWriter writer = new BsonWriter(ms);
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ writer.WriteEnd();
+
+ Assert.AreEqual("0B-00-00-00-0A-42-6C-61-68-00-00", (BitConverter.ToString(ms.ToArray())));
+ }
+
+ [Test]
+ public void WriteEndOnProperty_Close()
+ {
+ MemoryStream ms = new MemoryStream();
+ BsonWriter writer = new BsonWriter(ms);
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ writer.Close();
+
+ Assert.AreEqual("0B-00-00-00-0A-42-6C-61-68-00-00", (BitConverter.ToString(ms.ToArray())));
+ }
+
+ [Test]
+ public void WriteEndOnProperty_Dispose()
+ {
+ MemoryStream ms = new MemoryStream();
+ using (BsonWriter writer = new BsonWriter(ms))
+ {
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ }
+
+ Assert.AreEqual("0B-00-00-00-0A-42-6C-61-68-00-00", (BitConverter.ToString(ms.ToArray())));
+ }
+
+ [Test]
+ public void AutoCompleteOnClose_False()
+ {
+ MemoryStream ms = new MemoryStream();
+ using (BsonWriter writer = new BsonWriter(ms))
+ {
+ writer.AutoCompleteOnClose = false;
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+
+ writer.Flush();
+ }
+
+ // nothing is written because a BSON document needs to be completed before it can be written
+ Assert.AreEqual(string.Empty, (BitConverter.ToString(ms.ToArray())));
+ }
#endif
}
}
\ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
index fb8afb4c..99c9d386 100644
--- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
+++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
@@ -1310,6 +1310,51 @@ _____'propertyName': NaN,
Assert.AreEqual("{'Blah':null}", sw.ToString());
}
+ [Test]
+ public void WriteEndOnProperty_Close()
+ {
+ StringWriter sw = new StringWriter();
+ JsonTextWriter writer = new JsonTextWriter(sw);
+ writer.QuoteChar = '\'';
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ writer.Close();
+
+ Assert.AreEqual("{'Blah':null}", sw.ToString());
+ }
+
+ [Test]
+ public void WriteEndOnProperty_Dispose()
+ {
+ StringWriter sw = new StringWriter();
+ using (JsonTextWriter writer = new JsonTextWriter(sw))
+ {
+ writer.QuoteChar = '\'';
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ }
+
+ Assert.AreEqual("{'Blah':null}", sw.ToString());
+ }
+
+ [Test]
+ public void AutoCompleteOnClose_False()
+ {
+ StringWriter sw = new StringWriter();
+ using (JsonTextWriter writer = new JsonTextWriter(sw))
+ {
+ writer.AutoCompleteOnClose = false;
+ writer.QuoteChar = '\'';
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ }
+
+ Assert.AreEqual("{'Blah':", sw.ToString());
+ }
+
#if !NET20
[Test]
public void QuoteChar()
diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs
index e4e277eb..a3de3a2a 100644
--- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs
+++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs
@@ -172,6 +172,7 @@ namespace Newtonsoft.Json.Bson
///
/// Closes this writer.
/// If is set to true, the underlying is also closed.
+ /// If is set to true, the JSON is auto-completed.
///
public override void Close()
{
diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
index b8fe7400..6775bbb1 100644
--- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
+++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
@@ -1652,8 +1652,6 @@ namespace Newtonsoft.Json.Converters
if (!string.IsNullOrEmpty(DeserializeRootElementName))
{
- //rootNode = document.CreateElement(DeserializeRootElementName);
- //document.AppendChild(rootNode);
ReadElement(reader, document, rootNode, DeserializeRootElementName, manager);
}
else
diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs
index 560d5d23..743d760e 100644
--- a/Src/Newtonsoft.Json/JsonTextWriter.cs
+++ b/Src/Newtonsoft.Json/JsonTextWriter.cs
@@ -178,6 +178,7 @@ namespace Newtonsoft.Json
///
/// Closes this writer.
/// If is set to true, the underlying is also closed.
+ /// If is set to true, the JSON is auto-completed.
///
public override void Close()
{
diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs
index fa3a13ef..34a2d6b0 100644
--- a/Src/Newtonsoft.Json/JsonWriter.cs
+++ b/Src/Newtonsoft.Json/JsonWriter.cs
@@ -126,6 +126,14 @@ namespace Newtonsoft.Json
///
public bool CloseOutput { get; set; }
+ ///
+ /// Gets or sets a value indicating whether the JSON should be auto-completed when this writer is closed.
+ ///
+ ///
+ /// true to auto-complete the JSON when this writer is closed; otherwise false. The default is true.
+ ///
+ public bool AutoCompleteOnClose { get; set; }
+
///
/// Gets the top.
///
@@ -339,6 +347,7 @@ namespace Newtonsoft.Json
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
CloseOutput = true;
+ AutoCompleteOnClose = true;
}
internal void UpdateScopeWithFinishedValue()
@@ -393,11 +402,15 @@ namespace Newtonsoft.Json
///
/// Closes this writer.
- /// If is set to true, the destination is also closed.
+ /// If is set to true, the destination is also closed.
+ /// If is set to true, the JSON is auto-completed.
///
public virtual void Close()
{
- AutoCompleteAll();
+ if (AutoCompleteOnClose)
+ {
+ AutoCompleteAll();
+ }
}
///
diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs
index 4d86d563..8750bc66 100644
--- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs
+++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs
@@ -96,6 +96,7 @@ namespace Newtonsoft.Json.Linq
///
/// Closes this writer.
+ /// If is set to true, the JSON is auto-completed.
///
///
/// Setting to true has no additional effect, since the underlying is a type that cannot be closed.