-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';
|
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]
|
[Test]
|
||||||
public void ReadSingleObject()
|
public void ReadSingleObject()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,6 +38,24 @@ namespace Newtonsoft.Json.Tests.Bson
|
|||||||
{
|
{
|
||||||
public class BsonWriterTests : TestFixtureBase
|
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]
|
[Test]
|
||||||
public void WriteSingleObject()
|
public void WriteSingleObject()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,6 +35,24 @@ namespace Newtonsoft.Json.Tests
|
|||||||
{
|
{
|
||||||
public class JsonTextReaderTest : TestFixtureBase
|
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]
|
[Test]
|
||||||
public void YahooFinance()
|
public void YahooFinance()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,6 +34,24 @@ namespace Newtonsoft.Json.Tests
|
|||||||
{
|
{
|
||||||
public class JsonTextWriterTest : TestFixtureBase
|
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]
|
[Test]
|
||||||
public void ValueFormatting()
|
public void ValueFormatting()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,6 +30,11 @@ namespace Newtonsoft.Json.Bson
|
|||||||
_writer.Flush();
|
_writer.Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
_writer.Close();
|
||||||
|
}
|
||||||
|
|
||||||
public void WriteToken(BsonToken t)
|
public void WriteToken(BsonToken t)
|
||||||
{
|
{
|
||||||
CalculateSize(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()
|
private bool ReadCodeWScope()
|
||||||
{
|
{
|
||||||
switch (_bsonReaderState)
|
switch (_bsonReaderState)
|
||||||
|
|||||||
@@ -156,6 +156,14 @@ namespace Newtonsoft.Json.Bson
|
|||||||
_propertyName = name;
|
_propertyName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Close()
|
||||||
|
{
|
||||||
|
base.Close();
|
||||||
|
|
||||||
|
if (CloseOutput && _writer != null)
|
||||||
|
_writer.Close();
|
||||||
|
}
|
||||||
|
|
||||||
private void AddParent(BsonToken container)
|
private void AddParent(BsonToken container)
|
||||||
{
|
{
|
||||||
AddToken(container);
|
AddToken(container);
|
||||||
|
|||||||
@@ -117,6 +117,16 @@ namespace Newtonsoft.Json
|
|||||||
|
|
||||||
private readonly List<JTokenType> _stack;
|
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>
|
/// <summary>
|
||||||
/// Gets the quotation mark character used to enclose the value of a string.
|
/// Gets the quotation mark character used to enclose the value of a string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -173,6 +183,9 @@ namespace Newtonsoft.Json
|
|||||||
{
|
{
|
||||||
_currentState = State.Start;
|
_currentState = State.Start;
|
||||||
_stack = new List<JTokenType>();
|
_stack = new List<JTokenType>();
|
||||||
|
|
||||||
|
CloseInput = true;
|
||||||
|
|
||||||
Push(JTokenType.None);
|
Push(JTokenType.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -991,7 +991,7 @@ namespace Newtonsoft.Json
|
|||||||
{
|
{
|
||||||
base.Close();
|
base.Close();
|
||||||
|
|
||||||
if (_reader != null)
|
if (CloseInput && _reader != null)
|
||||||
_reader.Close();
|
_reader.Close();
|
||||||
|
|
||||||
if (_buffer != null)
|
if (_buffer != null)
|
||||||
|
|||||||
@@ -134,7 +134,8 @@ namespace Newtonsoft.Json
|
|||||||
{
|
{
|
||||||
base.Close();
|
base.Close();
|
||||||
|
|
||||||
_writer.Close();
|
if (CloseOutput && _writer != null)
|
||||||
|
_writer.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -126,6 +126,16 @@ namespace Newtonsoft.Json
|
|||||||
private State _currentState;
|
private State _currentState;
|
||||||
private Formatting _formatting;
|
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>
|
/// <summary>
|
||||||
/// Gets the top.
|
/// Gets the top.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -179,12 +189,14 @@ namespace Newtonsoft.Json
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an instance of the <c>JsonWriter</c> class.
|
/// Creates an instance of the <c>JsonWriter</c> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public JsonWriter()
|
protected JsonWriter()
|
||||||
{
|
{
|
||||||
_stack = new List<JTokenType>(8);
|
_stack = new List<JTokenType>(8);
|
||||||
_stack.Add(JTokenType.None);
|
_stack.Add(JTokenType.None);
|
||||||
_currentState = State.Start;
|
_currentState = State.Start;
|
||||||
_formatting = Formatting.None;
|
_formatting = Formatting.None;
|
||||||
|
|
||||||
|
CloseOutput = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Push(JTokenType value)
|
private void Push(JTokenType value)
|
||||||
|
|||||||
Reference in New Issue
Block a user