Skip to content

Commit

Permalink
-Added support for reading multiple comma delimited values with JsonR…
Browse files Browse the repository at this point in the history
…eader.SupportMultipleContent
  • Loading branch information
JamesNK committed Jul 1, 2017
1 parent c21d264 commit 6aed848
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
53 changes: 53 additions & 0 deletions Src/Newtonsoft.Json.Tests/JsonTextReaderTests/MiscAsyncTests.cs
Expand Up @@ -48,6 +48,59 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif
public class MiscAsyncTests : TestFixtureBase
{
[Test]
public async Task ReadWithSupportMultipleContentCommaDelimitedAsync()
{
string json = @"{ 'name': 'Admin' },{ 'name': 'Publisher' },1,null,[],,'string'";

JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.SupportMultipleContent = true;

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.String, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.String, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.Null, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.Undefined, reader.TokenType);

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(JsonToken.String, reader.TokenType);

Assert.IsFalse(await reader.ReadAsync());
}

[Test]
public async Task LineInfoAndNewLinesAsync()
{
Expand Down
53 changes: 53 additions & 0 deletions Src/Newtonsoft.Json.Tests/JsonTextReaderTests/MiscTests.cs
Expand Up @@ -62,6 +62,59 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif
public class MiscTests : TestFixtureBase
{
[Test]
public void ReadWithSupportMultipleContentCommaDelimited()
{
string json = @"{ 'name': 'Admin' },{ 'name': 'Publisher' },1,null,[],,'string'";

JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.SupportMultipleContent = true;

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, 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.PropertyName, reader.TokenType);

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Null, 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.Undefined, reader.TokenType);

Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);

Assert.IsFalse(reader.Read());
}

[Test]
public void LineInfoAndNewLines()
{
Expand Down
4 changes: 2 additions & 2 deletions Src/Newtonsoft.Json/JsonReader.cs
Expand Up @@ -1032,7 +1032,7 @@ protected void SetToken(JsonToken newToken, object value, bool updateIndex)

internal void SetPostValueState(bool updateIndex)
{
if (Peek() != JsonContainerType.None)
if (Peek() != JsonContainerType.None || SupportMultipleContent)
{
_currentState = State.PostValue;
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ private void ValidateEnd(JsonToken endToken)
throw JsonReaderException.Create(this, "JsonToken {0} is not valid for closing JsonType {1}.".FormatWith(CultureInfo.InvariantCulture, endToken, currentObject));
}

if (Peek() != JsonContainerType.None)
if (Peek() != JsonContainerType.None || SupportMultipleContent)
{
_currentState = State.PostValue;
}
Expand Down
7 changes: 7 additions & 0 deletions Src/Newtonsoft.Json/JsonTextReader.Async.cs
Expand Up @@ -176,6 +176,13 @@ private async Task<bool> ParsePostValueAsync(bool ignoreComments, CancellationTo
}
else
{
// handle multiple content without comma delimiter
if (SupportMultipleContent && Depth == 0)
{
SetStateBasedOnCurrent();
return false;
}

throw JsonReaderException.Create(this, "After parsing a value an unexpected character was encountered: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
}

Expand Down
7 changes: 7 additions & 0 deletions Src/Newtonsoft.Json/JsonTextReader.cs
Expand Up @@ -1458,6 +1458,13 @@ private bool ParsePostValue(bool ignoreComments)
}
else
{
// handle multiple content without comma delimiter
if (SupportMultipleContent && Depth == 0)
{
SetStateBasedOnCurrent();
return false;
}

throw JsonReaderException.Create(this, "After parsing a value an unexpected character was encountered: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
}
break;
Expand Down

0 comments on commit 6aed848

Please sign in to comment.