-Changed BsonReader/BsonWriter to close the underlying stream by default when closed
-Added CloseInput/CloseOutput to JsonReader/JsonWriter to control whether the underlying stream is closed. Default to true
This commit is contained in:
@@ -40,6 +40,24 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
{
|
||||
private const char Euro = '\u20ac';
|
||||
|
||||
[Test]
|
||||
public void CloseInput()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
reader.Close();
|
||||
Assert.IsFalse(ms.CanRead);
|
||||
|
||||
ms = new MemoryStream();
|
||||
reader = new BsonReader(ms) { CloseInput = false };
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
reader.Close();
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadSingleObject()
|
||||
{
|
||||
|
||||
@@ -38,6 +38,24 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
{
|
||||
public class BsonWriterTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void CloseOutput()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
writer.Close();
|
||||
Assert.IsFalse(ms.CanRead);
|
||||
|
||||
ms = new MemoryStream();
|
||||
writer = new BsonWriter(ms) { CloseOutput = false };
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
writer.Close();
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteSingleObject()
|
||||
{
|
||||
|
||||
@@ -35,6 +35,24 @@ namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
public class JsonTextReaderTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void CloseInput()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
JsonTextReader reader = new JsonTextReader(new StreamReader(ms));
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
reader.Close();
|
||||
Assert.IsFalse(ms.CanRead);
|
||||
|
||||
ms = new MemoryStream();
|
||||
reader = new JsonTextReader(new StreamReader(ms)) { CloseInput = false };
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
reader.Close();
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void YahooFinance()
|
||||
{
|
||||
|
||||
@@ -34,6 +34,24 @@ namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
public class JsonTextWriterTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void CloseOutput()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
JsonTextWriter writer = new JsonTextWriter(new StreamWriter(ms));
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
writer.Close();
|
||||
Assert.IsFalse(ms.CanRead);
|
||||
|
||||
ms = new MemoryStream();
|
||||
writer = new JsonTextWriter(new StreamWriter(ms)) { CloseOutput = false };
|
||||
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
writer.Close();
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValueFormatting()
|
||||
{
|
||||
|
||||
@@ -30,6 +30,11 @@ namespace Newtonsoft.Json.Bson
|
||||
_writer.Flush();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
_writer.Close();
|
||||
}
|
||||
|
||||
public void WriteToken(BsonToken t)
|
||||
{
|
||||
CalculateSize(t);
|
||||
|
||||
@@ -242,6 +242,14 @@ namespace Newtonsoft.Json.Bson
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
|
||||
if (CloseInput && _reader != null)
|
||||
_reader.Close();
|
||||
}
|
||||
|
||||
private bool ReadCodeWScope()
|
||||
{
|
||||
switch (_bsonReaderState)
|
||||
|
||||
@@ -156,6 +156,14 @@ namespace Newtonsoft.Json.Bson
|
||||
_propertyName = name;
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
|
||||
if (CloseOutput && _writer != null)
|
||||
_writer.Close();
|
||||
}
|
||||
|
||||
private void AddParent(BsonToken container)
|
||||
{
|
||||
AddToken(container);
|
||||
|
||||
@@ -117,6 +117,16 @@ namespace Newtonsoft.Json
|
||||
|
||||
private readonly List<JTokenType> _stack;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the underlying stream or
|
||||
/// <see cref="TextReader"/> should be closed when the reader is closed.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// true to close the underlying stream or <see cref="TextReader"/> when
|
||||
/// the reader is closed; otherwise false. The default is true.
|
||||
/// </value>
|
||||
public bool CloseInput { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the quotation mark character used to enclose the value of a string.
|
||||
/// </summary>
|
||||
@@ -173,6 +183,9 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
_currentState = State.Start;
|
||||
_stack = new List<JTokenType>();
|
||||
|
||||
CloseInput = true;
|
||||
|
||||
Push(JTokenType.None);
|
||||
}
|
||||
|
||||
|
||||
@@ -991,7 +991,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
base.Close();
|
||||
|
||||
if (_reader != null)
|
||||
if (CloseInput && _reader != null)
|
||||
_reader.Close();
|
||||
|
||||
if (_buffer != null)
|
||||
|
||||
@@ -134,7 +134,8 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
base.Close();
|
||||
|
||||
_writer.Close();
|
||||
if (CloseOutput && _writer != null)
|
||||
_writer.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -126,6 +126,16 @@ namespace Newtonsoft.Json
|
||||
private State _currentState;
|
||||
private Formatting _formatting;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the underlying stream or
|
||||
/// <see cref="TextReader"/> should be closed when the writer is closed.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// true to close the underlying stream or <see cref="TextReader"/> when
|
||||
/// the writer is closed; otherwise false. The default is true.
|
||||
/// </value>
|
||||
public bool CloseOutput { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the top.
|
||||
/// </summary>
|
||||
@@ -179,12 +189,14 @@ namespace Newtonsoft.Json
|
||||
/// <summary>
|
||||
/// Creates an instance of the <c>JsonWriter</c> class.
|
||||
/// </summary>
|
||||
public JsonWriter()
|
||||
protected JsonWriter()
|
||||
{
|
||||
_stack = new List<JTokenType>(8);
|
||||
_stack.Add(JTokenType.None);
|
||||
_currentState = State.Start;
|
||||
_formatting = Formatting.None;
|
||||
|
||||
CloseOutput = true;
|
||||
}
|
||||
|
||||
private void Push(JTokenType value)
|
||||
|
||||
Reference in New Issue
Block a user