-Added support for reading multiple comma delimited values with JsonReader.SupportMultipleContent

This commit is contained in:
James Newton-King
2017-07-01 16:43:15 +12:00
parent c21d2649ac
commit 6aed848eca
5 changed files with 122 additions and 2 deletions
@@ -48,6 +48,59 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif #endif
public class MiscAsyncTests : TestFixtureBase 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] [Test]
public async Task LineInfoAndNewLinesAsync() public async Task LineInfoAndNewLinesAsync()
{ {
@@ -62,6 +62,59 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif #endif
public class MiscTests : TestFixtureBase 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] [Test]
public void LineInfoAndNewLines() public void LineInfoAndNewLines()
{ {
+2 -2
View File
@@ -1032,7 +1032,7 @@ namespace Newtonsoft.Json
internal void SetPostValueState(bool updateIndex) internal void SetPostValueState(bool updateIndex)
{ {
if (Peek() != JsonContainerType.None) if (Peek() != JsonContainerType.None || SupportMultipleContent)
{ {
_currentState = State.PostValue; _currentState = State.PostValue;
} }
@@ -1064,7 +1064,7 @@ namespace Newtonsoft.Json
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));
} }
if (Peek() != JsonContainerType.None) if (Peek() != JsonContainerType.None || SupportMultipleContent)
{ {
_currentState = State.PostValue; _currentState = State.PostValue;
} }
@@ -176,6 +176,13 @@ namespace Newtonsoft.Json
} }
else 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)); throw JsonReaderException.Create(this, "After parsing a value an unexpected character was encountered: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
} }
+7
View File
@@ -1458,6 +1458,13 @@ namespace Newtonsoft.Json
} }
else 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)); throw JsonReaderException.Create(this, "After parsing a value an unexpected character was encountered: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
} }
break; break;