From 3c30c4883ce98f7d908ca8ccaa0e2ddd10690e13 Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Wed, 5 Jun 2019 14:49:59 -0700 Subject: [PATCH] Update JsonSerializer and JsonElement APIs - drop "As" and parameter re-ordering (dotnet/corefx#38270) * Update JsonSerializer and JsonElement APIs * Fix the test since the call became ambiguous and overload resolution picked the different overload. Commit migrated from https://github.com/dotnet/corefx/commit/5c2ba765f55462dcc5e00179a34398fc70bf37b7 --- .../System.Text.Json/ref/System.Text.Json.cs | 12 +++++----- .../src/System/Text/Json/Document/JsonElement.cs | 18 +++++++------- .../Converters/JsonValueConverterJsonElement.cs | 4 ++-- .../JsonSerializer.Write.HandleDictionary.cs | 2 +- .../Serialization/JsonSerializer.Write.Stream.cs | 14 +++++------ .../System.Text.Json/tests/JsonDocumentTests.cs | 12 +++++----- .../tests/JsonElementWriteTests.cs | 28 +++++++++++----------- .../tests/Serialization/Stream.WriteTests.cs | 8 +++---- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/libraries/System.Text.Json/ref/System.Text.Json.cs b/src/libraries/System.Text.Json/ref/System.Text.Json.cs index fd3fc90..b335fa5 100644 --- a/src/libraries/System.Text.Json/ref/System.Text.Json.cs +++ b/src/libraries/System.Text.Json/ref/System.Text.Json.cs @@ -76,10 +76,10 @@ namespace System.Text.Json public bool ValueEquals(System.ReadOnlySpan utf8Text) { throw null; } public bool ValueEquals(System.ReadOnlySpan text) { throw null; } public bool ValueEquals(string text) { throw null; } - public void WriteAsProperty(System.ReadOnlySpan utf8PropertyName, System.Text.Json.Utf8JsonWriter writer) { } - public void WriteAsProperty(System.ReadOnlySpan propertyName, System.Text.Json.Utf8JsonWriter writer) { } - public void WriteAsProperty(string propertyName, System.Text.Json.Utf8JsonWriter writer) { } - public void WriteAsValue(System.Text.Json.Utf8JsonWriter writer) { } + public void WriteProperty(System.ReadOnlySpan utf8PropertyName, System.Text.Json.Utf8JsonWriter writer) { } + public void WriteProperty(System.ReadOnlySpan propertyName, System.Text.Json.Utf8JsonWriter writer) { } + public void WriteProperty(string propertyName, System.Text.Json.Utf8JsonWriter writer) { } + public void WriteValue(System.Text.Json.Utf8JsonWriter writer) { } public partial struct ArrayEnumerator : System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerator, System.Collections.IEnumerable, System.Collections.IEnumerator, System.IDisposable { private object _dummy; @@ -175,8 +175,8 @@ namespace System.Text.Json public static string ToString(TValue value, System.Text.Json.JsonSerializerOptions options = null) { throw null; } public static byte[] ToUtf8Bytes(object value, System.Type type, System.Text.Json.JsonSerializerOptions options = null) { throw null; } public static byte[] ToUtf8Bytes(TValue value, System.Text.Json.JsonSerializerOptions options = null) { throw null; } - public static System.Threading.Tasks.Task WriteAsync(object value, System.Type type, System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public static System.Threading.Tasks.Task WriteAsync(TValue value, System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public static System.Threading.Tasks.Task WriteAsync(System.IO.Stream utf8Json, object value, System.Type type, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public static System.Threading.Tasks.Task WriteAsync(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } public sealed partial class JsonSerializerOptions { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs index 895c467..e30ce73 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs @@ -1074,7 +1074,7 @@ namespace System.Text.Json } /// - /// Write the element into the provided writer as a named object property. + /// Write the element into the provided writer as a named JSON object property. /// /// The name for this value within the JSON object. /// The writer. @@ -1084,11 +1084,11 @@ namespace System.Text.Json /// /// The parent has been disposed. /// - public void WriteAsProperty(string propertyName, Utf8JsonWriter writer) - => WriteAsProperty(propertyName.AsSpan(), writer); + public void WriteProperty(string propertyName, Utf8JsonWriter writer) + => WriteProperty(propertyName.AsSpan(), writer); /// - /// Write the element into the provided writer as a named object property. + /// Write the element into the provided writer as a named JSON object property. /// /// The name for this value within the JSON object. /// The writer. @@ -1098,7 +1098,7 @@ namespace System.Text.Json /// /// The parent has been disposed. /// - public void WriteAsProperty(ReadOnlySpan propertyName, Utf8JsonWriter writer) + public void WriteProperty(ReadOnlySpan propertyName, Utf8JsonWriter writer) { CheckValidInstance(); @@ -1106,7 +1106,7 @@ namespace System.Text.Json } /// - /// Write the element into the provided writer as a named object property. + /// Write the element into the provided writer as a named JSON object property. /// /// /// The name for this value within the JSON object, as UTF-8 text. @@ -1118,7 +1118,7 @@ namespace System.Text.Json /// /// The parent has been disposed. /// - public void WriteAsProperty(ReadOnlySpan utf8PropertyName, Utf8JsonWriter writer) + public void WriteProperty(ReadOnlySpan utf8PropertyName, Utf8JsonWriter writer) { CheckValidInstance(); @@ -1126,7 +1126,7 @@ namespace System.Text.Json } /// - /// Write the element into the provided writer as a value. + /// Write the element into the provided writer as a JSON value. /// /// The writer. /// @@ -1135,7 +1135,7 @@ namespace System.Text.Json /// /// The parent has been disposed. /// - public void WriteAsValue(Utf8JsonWriter writer) + public void WriteValue(Utf8JsonWriter writer) { CheckValidInstance(); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterJsonElement.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterJsonElement.cs index 423f463..ac2caf6 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterJsonElement.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterJsonElement.cs @@ -25,12 +25,12 @@ namespace System.Text.Json.Serialization.Converters public override void Write(JsonElement value, Utf8JsonWriter writer) { - value.WriteAsValue(writer); + value.WriteValue(writer); } public override void Write(JsonEncodedText propertyName, JsonElement value, Utf8JsonWriter writer) { - value.WriteAsProperty(propertyName.ToString(), writer); + value.WriteProperty(propertyName.ToString(), writer); } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleDictionary.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleDictionary.cs index 1b94ae1..94cbae9 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleDictionary.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleDictionary.cs @@ -143,7 +143,7 @@ namespace System.Text.Json Debug.Assert(entry.Key is string); string propertyName = (string)entry.Key; - element.WriteAsProperty(propertyName, writer); + element.WriteProperty(propertyName, writer); } else { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Stream.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Stream.cs index 734ba29..771ce21 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Stream.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Stream.cs @@ -14,35 +14,35 @@ namespace System.Text.Json /// Convert the provided value to UTF-8 encoded JSON text and write it to the . /// /// A task that represents the asynchronous write operation. - /// The value to convert. /// The UTF-8 to write to. + /// The value to convert. /// Options to control the conversion behavior. /// The which may be used to cancel the write operation. - public static Task WriteAsync(TValue value, Stream utf8Json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default) + public static Task WriteAsync(Stream utf8Json, TValue value, JsonSerializerOptions options = null, CancellationToken cancellationToken = default) { - return WriteAsyncCore(value, typeof(TValue), utf8Json, options, cancellationToken); + return WriteAsyncCore(utf8Json, value, typeof(TValue), options, cancellationToken); } /// /// Convert the provided value to UTF-8 encoded JSON text and write it to the . /// /// A task that represents the asynchronous write operation. + /// The UTF-8 to write to. /// The value to convert. /// The type of the to convert. - /// The UTF-8 to write to. /// Options to control the conversion behavior. /// The which may be used to cancel the write operation. - public static Task WriteAsync(object value, Type type, Stream utf8Json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default) + public static Task WriteAsync(Stream utf8Json, object value, Type type, JsonSerializerOptions options = null, CancellationToken cancellationToken = default) { if (utf8Json == null) throw new ArgumentNullException(nameof(utf8Json)); VerifyValueAndType(value, type); - return WriteAsyncCore(value, type, utf8Json, options, cancellationToken); + return WriteAsyncCore(utf8Json, value, type, options, cancellationToken); } - private static async Task WriteAsyncCore(object value, Type type, Stream utf8Json, JsonSerializerOptions options, CancellationToken cancellationToken) + private static async Task WriteAsyncCore(Stream utf8Json, object value, Type type, JsonSerializerOptions options, CancellationToken cancellationToken) { if (options == null) { diff --git a/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs b/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs index c6634c1..1131ddb 100644 --- a/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs +++ b/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs @@ -1420,19 +1420,19 @@ namespace System.Text.Json.Tests Assert.Throws(() => { Utf8JsonWriter writer = default; - root.WriteAsValue(writer); + root.WriteValue(writer); }); Assert.Throws(() => { Utf8JsonWriter writer = default; - root.WriteAsProperty(ReadOnlySpan.Empty, writer); + root.WriteProperty(ReadOnlySpan.Empty, writer); }); Assert.Throws(() => { Utf8JsonWriter writer = default; - root.WriteAsProperty(ReadOnlySpan.Empty, writer); + root.WriteProperty(ReadOnlySpan.Empty, writer); }); } } @@ -1471,19 +1471,19 @@ namespace System.Text.Json.Tests Assert.Throws(() => { Utf8JsonWriter writer = default; - root.WriteAsValue(writer); + root.WriteValue(writer); }); Assert.Throws(() => { Utf8JsonWriter writer = default; - root.WriteAsProperty(ReadOnlySpan.Empty, writer); + root.WriteProperty(ReadOnlySpan.Empty, writer); }); Assert.Throws(() => { Utf8JsonWriter writer = default; - root.WriteAsProperty(ReadOnlySpan.Empty, writer); + root.WriteProperty(ReadOnlySpan.Empty, writer); }); } diff --git a/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs b/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs index 43d07e2..98f9426 100644 --- a/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs +++ b/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs @@ -846,7 +846,7 @@ null, using (JsonDocument doc = JsonDocument.Parse(jsonIn, optionsCopy)) { var writer = new Utf8JsonWriter(buffer); - doc.RootElement.WriteAsValue(writer); + doc.RootElement.WriteValue(writer); writer.Flush(); ReadOnlySpan formatted = buffer.WrittenSpan; @@ -879,9 +879,9 @@ null, { foreach (JsonElement val in root.EnumerateArray()) { - val.WriteAsProperty(CharLabel, writer); - val.WriteAsProperty(CharLabel.AsSpan(), writer); - val.WriteAsProperty(byteUtf8, writer); + val.WriteProperty(CharLabel, writer); + val.WriteProperty(CharLabel.AsSpan(), writer); + val.WriteProperty(byteUtf8, writer); } writer.Flush(); @@ -902,15 +902,15 @@ null, { JsonTestHelper.AssertThrows( ref writer, - (ref Utf8JsonWriter w) => val.WriteAsProperty(CharLabel, w)); + (ref Utf8JsonWriter w) => val.WriteProperty(CharLabel, w)); JsonTestHelper.AssertThrows( ref writer, - (ref Utf8JsonWriter w) => val.WriteAsProperty(CharLabel.AsSpan(), w)); + (ref Utf8JsonWriter w) => val.WriteProperty(CharLabel.AsSpan(), w)); JsonTestHelper.AssertThrows( ref writer, - (ref Utf8JsonWriter w) => val.WriteAsProperty(byteUtf8, w)); + (ref Utf8JsonWriter w) => val.WriteProperty(byteUtf8, w)); } writer.Flush(); @@ -941,7 +941,7 @@ null, { foreach (JsonElement val in root.EnumerateArray()) { - val.WriteAsValue(writer); + val.WriteValue(writer); } writer.WriteEndObject(); @@ -957,7 +957,7 @@ null, { JsonTestHelper.AssertThrows( ref writer, - (ref Utf8JsonWriter w) => val.WriteAsValue(w)); + (ref Utf8JsonWriter w) => val.WriteValue(w)); } writer.WriteEndObject(); @@ -982,7 +982,7 @@ null, var writer = new Utf8JsonWriter(buffer, options); - target.WriteAsValue(writer); + target.WriteValue(writer); writer.Flush(); AssertContents(jsonOut ?? jsonIn, buffer); @@ -1007,7 +1007,7 @@ null, var writer = new Utf8JsonWriter(buffer, options); - target.WriteAsValue(writer); + target.WriteValue(writer); writer.Flush(); if (indented && s_replaceNewlines) @@ -1071,7 +1071,7 @@ null, var writer = new Utf8JsonWriter(buffer, options); writer.WriteStartObject(); - target.WriteAsProperty(propertyName, writer); + target.WriteProperty(propertyName, writer); writer.WriteEndObject(); writer.Flush(); @@ -1106,7 +1106,7 @@ null, var writer = new Utf8JsonWriter(buffer, options); writer.WriteStartObject(); - target.WriteAsProperty(propertyName, writer); + target.WriteProperty(propertyName, writer); writer.WriteEndObject(); writer.Flush(); @@ -1141,7 +1141,7 @@ null, var writer = new Utf8JsonWriter(buffer, options); writer.WriteStartObject(); - target.WriteAsProperty(propertyName, writer); + target.WriteProperty(propertyName, writer); writer.WriteEndObject(); writer.Flush(); diff --git a/src/libraries/System.Text.Json/tests/Serialization/Stream.WriteTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Stream.WriteTests.cs index 821267c..62bbf85 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Stream.WriteTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Stream.WriteTests.cs @@ -15,21 +15,21 @@ namespace System.Text.Json.Serialization.Tests public async static Task VerifyValueFail() { MemoryStream stream = new MemoryStream(); - await Assert.ThrowsAsync(async () => await JsonSerializer.WriteAsync("", null, stream)); + await Assert.ThrowsAsync(async () => await JsonSerializer.WriteAsync(stream, "", (Type)null)); } [Fact] public async static Task VerifyTypeFail() { MemoryStream stream = new MemoryStream(); - await Assert.ThrowsAsync(async () => await JsonSerializer.WriteAsync(1, typeof(string), stream)); + await Assert.ThrowsAsync(async () => await JsonSerializer.WriteAsync(stream, 1, typeof(string))); } [Fact] public static async Task NullObjectValue() { MemoryStream stream = new MemoryStream(); - await JsonSerializer.WriteAsync((object)null, stream); + await JsonSerializer.WriteAsync(stream, (object)null); stream.Seek(0, SeekOrigin.Begin); @@ -73,7 +73,7 @@ namespace System.Text.Json.Serialization.Tests obj.Initialize(); obj.Verify(); - await JsonSerializer.WriteAsync(obj, stream, options: options); + await JsonSerializer.WriteAsync(stream, obj, options: options); } // Must be changed if the test classes change: -- 2.7.4