-Added support for reading multiple pieces of JSON with a JsonReader
This commit is contained in:
@@ -2194,6 +2194,54 @@ bye", reader.Value);
|
|||||||
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
|
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]
|
[Test]
|
||||||
public void ParseBooleanWithNoExtraContent()
|
public void ParseBooleanWithNoExtraContent()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7280,6 +7280,35 @@ Parameter name: value",
|
|||||||
}
|
}
|
||||||
#endif
|
#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
|
private class FooBar
|
||||||
{
|
{
|
||||||
public DateTimeOffset Foo { get; set; }
|
public DateTimeOffset Foo { get; set; }
|
||||||
|
|||||||
@@ -147,6 +147,15 @@ namespace Newtonsoft.Json
|
|||||||
/// </value>
|
/// </value>
|
||||||
public bool CloseInput { get; set; }
|
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>
|
/// <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>
|
||||||
@@ -831,7 +840,10 @@ namespace Newtonsoft.Json
|
|||||||
case JsonToken.String:
|
case JsonToken.String:
|
||||||
case JsonToken.Raw:
|
case JsonToken.Raw:
|
||||||
case JsonToken.Bytes:
|
case JsonToken.Bytes:
|
||||||
_currentState = (Peek() != JsonContainerType.None) ? State.PostValue : State.Finished;
|
if (Peek() != JsonContainerType.None)
|
||||||
|
_currentState = State.PostValue;
|
||||||
|
else
|
||||||
|
SetFinished();
|
||||||
|
|
||||||
UpdateScopeWithFinishedValue();
|
UpdateScopeWithFinishedValue();
|
||||||
break;
|
break;
|
||||||
@@ -851,7 +863,10 @@ namespace Newtonsoft.Json
|
|||||||
if (GetTypeForCloseToken(endToken) != currentObject)
|
if (GetTypeForCloseToken(endToken) != currentObject)
|
||||||
throw JsonReaderException.Create(this, "JsonToken {0} is not valid for closing JsonType {1}.".FormatWith(CultureInfo.InvariantCulture, 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>
|
/// <summary>
|
||||||
@@ -873,13 +888,21 @@ namespace Newtonsoft.Json
|
|||||||
_currentState = State.Constructor;
|
_currentState = State.Constructor;
|
||||||
break;
|
break;
|
||||||
case JsonContainerType.None:
|
case JsonContainerType.None:
|
||||||
_currentState = State.Finished;
|
SetFinished();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw JsonReaderException.Create(this, "While setting the reader state back to current object an unexpected JsonType was encountered: {0}".FormatWith(CultureInfo.InvariantCulture, currentObject));
|
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)
|
internal static bool IsPrimitiveToken(JsonToken token)
|
||||||
{
|
{
|
||||||
switch (token)
|
switch (token)
|
||||||
|
|||||||
Reference in New Issue
Block a user