-Added AutoCompleteOnClose to JsonWriter to control whether JSON is auto-completed on JsonWriter.Close

This commit is contained in:
James Newton-King
2017-01-19 14:19:52 +13:00
parent 56e3d7575b
commit 55afb9aa72
7 changed files with 120 additions and 4 deletions
@@ -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
}
}
@@ -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()
+1
View File
@@ -172,6 +172,7 @@ namespace Newtonsoft.Json.Bson
/// <summary>
/// Closes this writer.
/// If <see cref="JsonWriter.CloseOutput"/> is set to <c>true</c>, the underlying <see cref="Stream"/> is also closed.
/// If <see cref="JsonWriter.AutoCompleteOnClose"/> is set to <c>true</c>, the JSON is auto-completed.
/// </summary>
public override void Close()
{
@@ -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
+1
View File
@@ -178,6 +178,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Closes this writer.
/// If <see cref="JsonWriter.CloseOutput"/> is set to <c>true</c>, the underlying <see cref="TextWriter"/> is also closed.
/// If <see cref="JsonWriter.AutoCompleteOnClose"/> is set to <c>true</c>, the JSON is auto-completed.
/// </summary>
public override void Close()
{
+15 -2
View File
@@ -126,6 +126,14 @@ namespace Newtonsoft.Json
/// </value>
public bool CloseOutput { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the JSON should be auto-completed when this writer is closed.
/// </summary>
/// <value>
/// <c>true</c> to auto-complete the JSON when this writer is closed; otherwise <c>false</c>. The default is <c>true</c>.
/// </value>
public bool AutoCompleteOnClose { get; set; }
/// <summary>
/// Gets the top.
/// </summary>
@@ -339,6 +347,7 @@ namespace Newtonsoft.Json
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
CloseOutput = true;
AutoCompleteOnClose = true;
}
internal void UpdateScopeWithFinishedValue()
@@ -393,11 +402,15 @@ namespace Newtonsoft.Json
/// <summary>
/// Closes this writer.
/// If <see cref="JsonWriter.CloseOutput"/> is set to <c>true</c>, the destination is also closed.
/// If <see cref="CloseOutput"/> is set to <c>true</c>, the destination is also closed.
/// If <see cref="AutoCompleteOnClose"/> is set to <c>true</c>, the JSON is auto-completed.
/// </summary>
public virtual void Close()
{
AutoCompleteAll();
if (AutoCompleteOnClose)
{
AutoCompleteAll();
}
}
/// <summary>
+1
View File
@@ -96,6 +96,7 @@ namespace Newtonsoft.Json.Linq
/// <summary>
/// Closes this writer.
/// If <see cref="JsonWriter.AutoCompleteOnClose"/> is set to <c>true</c>, the JSON is auto-completed.
/// </summary>
/// <remarks>
/// Setting <see cref="JsonWriter.CloseOutput"/> to <c>true</c> has no additional effect, since the underlying <see cref="JContainer"/> is a type that cannot be closed.