diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.Parse.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.Parse.cs index 516159a5f6ad0c..b02d6fa8936911 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.Parse.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.Parse.cs @@ -58,7 +58,7 @@ public static JsonElement ParseValue(ref Utf8JsonReader reader) /// The reader to read. /// Receives the parsed element. /// - /// if a value was read and parsed into a JsonElement, + /// if a value was read and parsed into a JsonElement; /// if the reader ran out of data while parsing. /// All other situations result in an exception being thrown. /// diff --git a/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs b/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs index c29851a603e6f1..115fec5a624d20 100644 --- a/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs +++ b/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs @@ -16,6 +16,9 @@ public static IEnumerable ElementParseCases yield return new object[] { "true", JsonValueKind.True }; yield return new object[] { "false", JsonValueKind.False }; yield return new object[] { "\"MyString\"", JsonValueKind.String }; + yield return new object[] { @"""\u0033\u002e\u0031""", JsonValueKind.String }; // "3.12" + yield return new object[] { "1", JsonValueKind.Number }; + yield return new object[] { "3.125e7", JsonValueKind.Number }; yield return new object[] { "{}", JsonValueKind.Object }; yield return new object[] { "[]", JsonValueKind.Array }; } @@ -29,6 +32,7 @@ public static void ParseValue(string json, JsonValueKind kind) JsonElement? element = JsonElement.ParseValue(ref reader); Assert.Equal(kind, element!.Value.ValueKind); + Assert.Equal(json.Length, reader.BytesConsumed); } [Theory] @@ -40,6 +44,7 @@ public static void TryParseValue(string json, JsonValueKind kind) bool success = JsonElement.TryParseValue(ref reader, out JsonElement? element); Assert.True(success); Assert.Equal(kind, element!.Value.ValueKind); + Assert.Equal(json.Length, reader.BytesConsumed); } public static IEnumerable ElementParsePartialDataCases @@ -49,6 +54,7 @@ public static IEnumerable ElementParsePartialDataCases yield return new object[] { "\"MyString"}; yield return new object[] { "{" }; yield return new object[] { "[" }; + yield return new object[] { " \n" }; } } @@ -58,12 +64,21 @@ public static void ParseValuePartialDataFail(string json) { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json)); + Exception ex; try { JsonElement.ParseValue(ref reader); - Assert.True(false, "Expected exception."); + ex = null; } - catch (JsonException) { } + catch (Exception e) + { + ex = e; + } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } [Theory] @@ -72,12 +87,21 @@ public static void TryParseValuePartialDataFail(string json) { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json)); + Exception ex; try { JsonElement.TryParseValue(ref reader, out JsonElement? element); - Assert.True(false, "Expected exception."); + ex = null; + } + catch (Exception e) + { + ex = e; } - catch (JsonException) { } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } [Theory] @@ -86,12 +110,21 @@ public static void ParseValueOutOfData(string json) { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json), isFinalBlock: false, new JsonReaderState()); + Exception ex; try { JsonElement.ParseValue(ref reader); - Assert.True(false, "Expected exception."); + ex = null; + } + catch (Exception e) + { + ex = e; } - catch (JsonException) { } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } [Theory] @@ -100,6 +133,8 @@ public static void TryParseValueOutOfData(string json) { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json), isFinalBlock: false, new JsonReaderState()); Assert.False(JsonElement.TryParseValue(ref reader, out JsonElement? element)); + Assert.Null(element); + Assert.Equal(0, reader.BytesConsumed); } public static IEnumerable ElementParseInvalidDataCases @@ -117,12 +152,21 @@ public static void ParseValueInvalidDataFail(string json) { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json)); + Exception ex; try { JsonElement.ParseValue(ref reader); - Assert.True(false, "Expected exception."); + ex = null; } - catch (JsonException) { } + catch (Exception e) + { + ex = e; + } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } [Theory] @@ -131,12 +175,21 @@ public static void TryParseValueInvalidDataFail(string json) { var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json)); + Exception ex; try { JsonElement.TryParseValue(ref reader, out JsonElement? element); - Assert.True(false, "Expected exception."); + ex = null; + } + catch (Exception e) + { + ex = e; } - catch (JsonException) { } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } } }