From de0c0749339cd9ec1c466c74de4dc5ccc7780b95 Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Mon, 26 Oct 2020 12:31:48 -0500 Subject: [PATCH 1/2] Address JsonElement doc and test feedback --- .../Text/Json/Document/JsonDocument.Parse.cs | 4 +- .../Text/Json/Document/JsonElement.Parse.cs | 6 +- .../tests/JsonElementParseTests.cs | 70 ++++++++++++++++--- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs index e81c52ea548dfa..eaf79c0b15124c 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs @@ -284,7 +284,7 @@ public static JsonDocument Parse(string json, JsonDocumentOptions options = defa /// /// /// - /// is using unsupported options. + /// contains unsupported options. /// /// /// The current token does not start or represent a value. @@ -324,7 +324,7 @@ public static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] /// /// /// - /// is using unsupported options. + /// contains unsupported options. /// /// /// The current token does not start or represent a value. 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..3f5593ef9eddbf 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 @@ -35,7 +35,7 @@ public readonly partial struct JsonElement /// /// /// - /// is using unsupported options. + /// contains unsupported options. /// /// /// The current token does not start or represent a value. @@ -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. /// @@ -82,7 +82,7 @@ public static JsonElement ParseValue(ref Utf8JsonReader reader) /// /// /// - /// is using unsupported options. + /// contains unsupported options. /// /// /// The current token does not start or represent a value. diff --git a/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs b/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs index c29851a603e6f1..fe39320906b160 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,18 @@ 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.Equal(0, reader.BytesConsumed); } [Theory] @@ -72,12 +84,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 +107,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 (JsonException) { } + catch (Exception e) + { + ex = e; + } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } [Theory] @@ -100,6 +130,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 +149,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 (Exception e) + { + ex = e; } - catch (JsonException) { } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } [Theory] @@ -131,12 +172,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 (JsonException) { } + catch (Exception e) + { + ex = e; + } + + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + + Assert.Equal(0, reader.BytesConsumed); } } } From 93da7faaa1a9690d72ba425ebe2fe230ffbb1213 Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Thu, 29 Oct 2020 16:08:19 -0500 Subject: [PATCH 2/2] Add missing Asserts, revert some doc --- .../src/System/Text/Json/Document/JsonDocument.Parse.cs | 4 ++-- .../src/System/Text/Json/Document/JsonElement.Parse.cs | 4 ++-- src/libraries/System.Text.Json/tests/JsonElementParseTests.cs | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs index eaf79c0b15124c..e81c52ea548dfa 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs @@ -284,7 +284,7 @@ public static JsonDocument Parse(string json, JsonDocumentOptions options = defa /// /// /// - /// contains unsupported options. + /// is using unsupported options. /// /// /// The current token does not start or represent a value. @@ -324,7 +324,7 @@ public static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] /// /// /// - /// contains unsupported options. + /// is using unsupported options. /// /// /// The current token does not start or represent a value. 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 3f5593ef9eddbf..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 @@ -35,7 +35,7 @@ public readonly partial struct JsonElement /// /// /// - /// contains unsupported options. + /// is using unsupported options. /// /// /// The current token does not start or represent a value. @@ -82,7 +82,7 @@ public static JsonElement ParseValue(ref Utf8JsonReader reader) /// /// /// - /// contains unsupported options. + /// is using unsupported options. /// /// /// The current token does not start or represent a value. diff --git a/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs b/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs index fe39320906b160..115fec5a624d20 100644 --- a/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs +++ b/src/libraries/System.Text.Json/tests/JsonElementParseTests.cs @@ -75,6 +75,9 @@ public static void ParseValuePartialDataFail(string json) ex = e; } + Assert.NotNull(ex); + Assert.IsAssignableFrom(ex); + Assert.Equal(0, reader.BytesConsumed); }