-Added AutoCompleteOnClose to JsonWriter to control whether JSON is auto-completed on JsonWriter.Close
This commit is contained in:
@@ -882,6 +882,63 @@ namespace Newtonsoft.Json.Tests.Bson
|
|||||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||||
Assert.IsNull(reader.ReadAsBytes());
|
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
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1310,6 +1310,51 @@ _____'propertyName': NaN,
|
|||||||
Assert.AreEqual("{'Blah':null}", sw.ToString());
|
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
|
#if !NET20
|
||||||
[Test]
|
[Test]
|
||||||
public void QuoteChar()
|
public void QuoteChar()
|
||||||
|
|||||||
@@ -172,6 +172,7 @@ namespace Newtonsoft.Json.Bson
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes this writer.
|
/// 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.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>
|
/// </summary>
|
||||||
public override void Close()
|
public override void Close()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1652,8 +1652,6 @@ namespace Newtonsoft.Json.Converters
|
|||||||
|
|
||||||
if (!string.IsNullOrEmpty(DeserializeRootElementName))
|
if (!string.IsNullOrEmpty(DeserializeRootElementName))
|
||||||
{
|
{
|
||||||
//rootNode = document.CreateElement(DeserializeRootElementName);
|
|
||||||
//document.AppendChild(rootNode);
|
|
||||||
ReadElement(reader, document, rootNode, DeserializeRootElementName, manager);
|
ReadElement(reader, document, rootNode, DeserializeRootElementName, manager);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -178,6 +178,7 @@ namespace Newtonsoft.Json
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes this writer.
|
/// 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.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>
|
/// </summary>
|
||||||
public override void Close()
|
public override void Close()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -126,6 +126,14 @@ namespace Newtonsoft.Json
|
|||||||
/// </value>
|
/// </value>
|
||||||
public bool CloseOutput { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Gets the top.
|
/// Gets the top.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -339,6 +347,7 @@ namespace Newtonsoft.Json
|
|||||||
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
|
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
|
||||||
|
|
||||||
CloseOutput = true;
|
CloseOutput = true;
|
||||||
|
AutoCompleteOnClose = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void UpdateScopeWithFinishedValue()
|
internal void UpdateScopeWithFinishedValue()
|
||||||
@@ -393,11 +402,15 @@ namespace Newtonsoft.Json
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes this writer.
|
/// 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>
|
/// </summary>
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
AutoCompleteAll();
|
if (AutoCompleteOnClose)
|
||||||
|
{
|
||||||
|
AutoCompleteAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes this writer.
|
/// Closes this writer.
|
||||||
|
/// If <see cref="JsonWriter.AutoCompleteOnClose"/> is set to <c>true</c>, the JSON is auto-completed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <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.
|
/// 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.
|
||||||
|
|||||||
Reference in New Issue
Block a user