-Added support for reading multiple pieces of JSON with a JsonReader

This commit is contained in:
James Newton-King
2013-12-07 22:06:37 +13:00
parent d5f606c435
commit b06f137513
3 changed files with 103 additions and 3 deletions
@@ -2194,6 +2194,54 @@ bye", reader.Value);
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
}
[Test]
public void SupportMultipleContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"1 2 ""name"" [][]null {}{} 1.1"));
reader.SupportMultipleContent = true;
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Null, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.IsFalse(reader.Read());
}
[Test]
public void ParseBooleanWithNoExtraContent()
{
@@ -7280,6 +7280,35 @@ Parameter name: value",
}
#endif
public class MultipleItemsClass
{
public string Name { get; set; }
}
[Test]
public void MultipleItems()
{
IList<MultipleItemsClass> values = new List<MultipleItemsClass>();
JsonTextReader reader = new JsonTextReader(new StringReader(@"{ ""name"": ""bar"" }{ ""name"": ""baz"" }"));
reader.SupportMultipleContent = true;
while (true)
{
if (!reader.Read())
break;
JsonSerializer serializer = new JsonSerializer();
MultipleItemsClass foo = serializer.Deserialize<MultipleItemsClass>(reader);
values.Add(foo);
}
Assert.AreEqual(2, values.Count);
Assert.AreEqual("bar", values[0].Name);
Assert.AreEqual("baz", values[1].Name);
}
private class FooBar
{
public DateTimeOffset Foo { get; set; }
+26 -3
View File
@@ -147,6 +147,15 @@ namespace Newtonsoft.Json
/// </value>
public bool CloseInput { get; set; }
/// <summary>
/// Gets or sets a value indicating whether multiple pieces of JSON content can
/// be read from a stream of content without erroring.
/// </summary>
/// <value>
/// true to support reading multiple pieces of JSON content; otherwise false. The default is false.
/// </value>
public bool SupportMultipleContent { get; set; }
/// <summary>
/// Gets the quotation mark character used to enclose the value of a string.
/// </summary>
@@ -831,7 +840,10 @@ namespace Newtonsoft.Json
case JsonToken.String:
case JsonToken.Raw:
case JsonToken.Bytes:
_currentState = (Peek() != JsonContainerType.None) ? State.PostValue : State.Finished;
if (Peek() != JsonContainerType.None)
_currentState = State.PostValue;
else
SetFinished();
UpdateScopeWithFinishedValue();
break;
@@ -851,7 +863,10 @@ namespace Newtonsoft.Json
if (GetTypeForCloseToken(endToken) != currentObject)
throw JsonReaderException.Create(this, "JsonToken {0} is not valid for closing JsonType {1}.".FormatWith(CultureInfo.InvariantCulture, endToken, currentObject));
_currentState = (Peek() != JsonContainerType.None) ? State.PostValue : State.Finished;
if (Peek() != JsonContainerType.None)
_currentState = State.PostValue;
else
SetFinished();
}
/// <summary>
@@ -873,13 +888,21 @@ namespace Newtonsoft.Json
_currentState = State.Constructor;
break;
case JsonContainerType.None:
_currentState = State.Finished;
SetFinished();
break;
default:
throw JsonReaderException.Create(this, "While setting the reader state back to current object an unexpected JsonType was encountered: {0}".FormatWith(CultureInfo.InvariantCulture, currentObject));
}
}
private void SetFinished()
{
if (SupportMultipleContent)
_currentState = State.Start;
else
_currentState = State.Finished;
}
internal static bool IsPrimitiveToken(JsonToken token)
{
switch (token)