Skip to content

Commit

Permalink
-Added AutoCompleteOnClose to JsonWriter to control whether JSON is a…
Browse files Browse the repository at this point in the history
…uto-completed on JsonWriter.Close
  • Loading branch information
JamesNK committed Jan 19, 2017
1 parent 56e3d75 commit 55afb9a
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 4 deletions.
57 changes: 57 additions & 0 deletions Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,63 @@ public void WriteByteArray()
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
}
}
45 changes: 45 additions & 0 deletions Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,51 @@ public void WriteEndOnProperty()
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()
Expand Down
1 change: 1 addition & 0 deletions Src/Newtonsoft.Json/Bson/BsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public override void WritePropertyName(string name)
/// <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()
{
Expand Down
2 changes: 0 additions & 2 deletions Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,8 +1652,6 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

if (!string.IsNullOrEmpty(DeserializeRootElementName))
{
//rootNode = document.CreateElement(DeserializeRootElementName);
//document.AppendChild(rootNode);
ReadElement(reader, document, rootNode, DeserializeRootElementName, manager);
}
else
Expand Down
1 change: 1 addition & 0 deletions Src/Newtonsoft.Json/JsonTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public override void Flush()
/// <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()
{
Expand Down
17 changes: 15 additions & 2 deletions Src/Newtonsoft.Json/JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ static JsonWriter()
/// </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>
Expand Down Expand Up @@ -339,6 +347,7 @@ protected JsonWriter()
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;

CloseOutput = true;
AutoCompleteOnClose = true;
}

internal void UpdateScopeWithFinishedValue()
Expand Down Expand Up @@ -393,11 +402,15 @@ private JsonContainerType Peek()

/// <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>
Expand Down
1 change: 1 addition & 0 deletions Src/Newtonsoft.Json/Linq/JTokenWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public override void Flush()

/// <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.
Expand Down

0 comments on commit 55afb9a

Please sign in to comment.