From 72110fa6cd93c83a71919164e7d20284baae1d55 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Wed, 14 Oct 2020 11:35:35 -0700 Subject: [PATCH] JSONPatch API review adjustments (#15968) --- .../Azure.Core.Experimental.netstandard2.0.cs | 7 +-- .../src/JsonPatchDocument.cs | 47 ++++++++++--------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs index 7c0437c30d610..c072934515731 100644 --- a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs +++ b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs @@ -98,15 +98,16 @@ public partial class JsonPatchDocument public JsonPatchDocument() { } public JsonPatchDocument(Azure.Core.Serialization.ObjectSerializer serializer) { } public void AppendAddRaw(string path, string rawJsonValue) { } - public void AppendAdd(string path, T value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } + public void AppendAdd(string path, T value) { } public void AppendCopy(string from, string path) { } public void AppendMove(string from, string path) { } public void AppendRemove(string path) { } public void AppendReplaceRaw(string path, string rawJsonValue) { } - public void AppendReplace(string path, T value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } + public void AppendReplace(string path, T value) { } public void AppendTestRaw(string path, string rawJsonValue) { } - public void AppendTest(string path, T value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } + public void AppendTest(string path, T value) { } public override string ToString() { throw null; } + public void WriteTo(System.Text.Json.Utf8JsonWriter writer) { } } } namespace Azure.Core.Amqp diff --git a/sdk/core/Azure.Core.Experimental/src/JsonPatchDocument.cs b/sdk/core/Azure.Core.Experimental/src/JsonPatchDocument.cs index fe3bd11218cfa..d2e8451288c3d 100644 --- a/sdk/core/Azure.Core.Experimental/src/JsonPatchDocument.cs +++ b/sdk/core/Azure.Core.Experimental/src/JsonPatchDocument.cs @@ -17,8 +17,8 @@ namespace Azure.Core /// public class JsonPatchDocument { - private ObjectSerializer _serializer; - private Collection Operations { get; } + private readonly ObjectSerializer _serializer; + private readonly Collection _operations; /// /// Initializes a new instance of that uses as the default serializer. @@ -33,7 +33,7 @@ public JsonPatchDocument() : this(new JsonObjectSerializer()) /// The instance to use for value serialization. public JsonPatchDocument(ObjectSerializer serializer) { - Operations = new Collection(); + _operations = new Collection(); _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer)); } @@ -44,7 +44,7 @@ public JsonPatchDocument(ObjectSerializer serializer) /// The raw JSON value to add to the path. public void AppendAddRaw(string path, string rawJsonValue) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Add, path, null, rawJsonValue)); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Add, path, null, rawJsonValue)); } /// @@ -52,10 +52,9 @@ public void AppendAddRaw(string path, string rawJsonValue) /// /// The path to apply the addition to. /// The value to add to the path. - /// The to use. - public void AppendAdd(string path, T value, CancellationToken cancellationToken = default) + public void AppendAdd(string path, T value) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Add, path, null, Serialize(value, cancellationToken))); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Add, path, null, Serialize(value))); } /// @@ -65,17 +64,16 @@ public void AppendAdd(string path, T value, CancellationToken cancellationTok /// The raw JSON value to replace with. public void AppendReplaceRaw(string path, string rawJsonValue) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Replace, path, null, rawJsonValue)); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Replace, path, null, rawJsonValue)); } /// /// Appends a "replace" operation to this . /// /// The path to replace. /// The value to replace with. - /// The to use. - public void AppendReplace(string path, T value, CancellationToken cancellationToken = default) + public void AppendReplace(string path, T value) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Replace, path, null, Serialize(value, cancellationToken))); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Replace, path, null, Serialize(value))); } /// @@ -85,7 +83,7 @@ public void AppendReplace(string path, T value, CancellationToken cancellatio /// The path to copy to. public void AppendCopy(string from, string path) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Copy, path, from, null)); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Copy, path, from, null)); } /// @@ -95,7 +93,7 @@ public void AppendCopy(string from, string path) /// The path to move to. public void AppendMove(string from, string path) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Move, path, from, null)); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Move, path, from, null)); } /// @@ -104,7 +102,7 @@ public void AppendMove(string from, string path) /// The path to remove. public void AppendRemove(string path) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Remove, path, null, null)); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Remove, path, null, null)); } /// @@ -114,7 +112,7 @@ public void AppendRemove(string path) /// The raw JSON value to test against. public void AppendTestRaw(string path, string rawJsonValue) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Test, path, null, rawJsonValue)); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Test, path, null, rawJsonValue)); } /// @@ -122,10 +120,9 @@ public void AppendTestRaw(string path, string rawJsonValue) /// /// The path to test. /// The value to replace with. - /// The to use. - public void AppendTest(string path, T value, CancellationToken cancellationToken = default) + public void AppendTest(string path, T value) { - Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Test, path, null, Serialize(value, cancellationToken))); + _operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Test, path, null, Serialize(value))); } /// @@ -142,10 +139,16 @@ public override string ToString() return Encoding.UTF8.GetString(memoryStream.ToArray()); } - private void WriteTo(Utf8JsonWriter writer) + /// + /// Writes the document to a in JSON Path format. + /// + /// The instance to write this document to +#pragma warning disable AZC0014 // do not expose Json types in public APIs + public void WriteTo(Utf8JsonWriter writer) +#pragma warning restore AZC0014 { writer.WriteStartArray(); - foreach (var operation in Operations) + foreach (var operation in _operations) { writer.WriteStartObject(); writer.WriteString("op", operation.Kind.ToString()); @@ -165,10 +168,10 @@ private void WriteTo(Utf8JsonWriter writer) writer.WriteEndArray(); } - private string Serialize(T value, CancellationToken cancellationToken) + private string Serialize(T value) { using MemoryStream memoryStream = new MemoryStream(); - _serializer.Serialize(memoryStream, value, typeof(T), cancellationToken); + _serializer.Serialize(memoryStream, value, typeof(T), default); return Encoding.UTF8.GetString(memoryStream.ToArray()); } }