Skip to content

Commit

Permalink
JSONPatch API review adjustments (Azure#15968)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored and annelo-msft committed Feb 17, 2021
1 parent 3e3cb18 commit 72110fa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(string path, T value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
public void AppendAdd<T>(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<T>(string path, T value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
public void AppendReplace<T>(string path, T value) { }
public void AppendTestRaw(string path, string rawJsonValue) { }
public void AppendTest<T>(string path, T value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
public void AppendTest<T>(string path, T value) { }
public override string ToString() { throw null; }
public void WriteTo(System.Text.Json.Utf8JsonWriter writer) { }
}
}
namespace Azure.Core.Amqp
Expand Down
47 changes: 25 additions & 22 deletions sdk/core/Azure.Core.Experimental/src/JsonPatchDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace Azure.Core
/// </summary>
public class JsonPatchDocument
{
private ObjectSerializer _serializer;
private Collection<JsonPatchOperation> Operations { get; }
private readonly ObjectSerializer _serializer;
private readonly Collection<JsonPatchOperation> _operations;

/// <summary>
/// Initializes a new instance of <see cref="JsonPatchDocument"/> that uses <see cref="JsonObjectSerializer"/> as the default serializer.
Expand All @@ -33,7 +33,7 @@ public JsonPatchDocument() : this(new JsonObjectSerializer())
/// <param name="serializer">The <see cref="ObjectSerializer"/> instance to use for value serialization.</param>
public JsonPatchDocument(ObjectSerializer serializer)
{
Operations = new Collection<JsonPatchOperation>();
_operations = new Collection<JsonPatchOperation>();
_serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
}

Expand All @@ -44,18 +44,17 @@ public JsonPatchDocument(ObjectSerializer serializer)
/// <param name="rawJsonValue">The raw JSON value to add to the path.</param>
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));
}

/// <summary>
/// Appends an "add" operation to this <see cref="JsonPatchDocument"/>.
/// </summary>
/// <param name="path">The path to apply the addition to.</param>
/// <param name="value">The value to add to the path.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
public void AppendAdd<T>(string path, T value, CancellationToken cancellationToken = default)
public void AppendAdd<T>(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)));
}

/// <summary>
Expand All @@ -65,17 +64,16 @@ public void AppendAdd<T>(string path, T value, CancellationToken cancellationTok
/// <param name="rawJsonValue">The raw JSON value to replace with.</param>
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));
}
/// <summary>
/// Appends a "replace" operation to this <see cref="JsonPatchDocument"/>.
/// </summary>
/// <param name="path">The path to replace.</param>
/// <param name="value">The value to replace with.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
public void AppendReplace<T>(string path, T value, CancellationToken cancellationToken = default)
public void AppendReplace<T>(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)));
}

/// <summary>
Expand All @@ -85,7 +83,7 @@ public void AppendReplace<T>(string path, T value, CancellationToken cancellatio
/// <param name="path">The path to copy to.</param>
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));
}

/// <summary>
Expand All @@ -95,7 +93,7 @@ public void AppendCopy(string from, string path)
/// <param name="path">The path to move to.</param>
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));
}

/// <summary>
Expand All @@ -104,7 +102,7 @@ public void AppendMove(string from, string path)
/// <param name="path">The path to remove.</param>
public void AppendRemove(string path)
{
Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Remove, path, null, null));
_operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Remove, path, null, null));
}

/// <summary>
Expand All @@ -114,18 +112,17 @@ public void AppendRemove(string path)
/// <param name="rawJsonValue">The raw JSON value to test against.</param>
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));
}

/// <summary>
/// Appends a "test" operation to this <see cref="JsonPatchDocument"/>.
/// </summary>
/// <param name="path">The path to test.</param>
/// <param name="value">The value to replace with.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
public void AppendTest<T>(string path, T value, CancellationToken cancellationToken = default)
public void AppendTest<T>(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)));
}

/// <summary>
Expand All @@ -142,10 +139,16 @@ public override string ToString()
return Encoding.UTF8.GetString(memoryStream.ToArray());
}

private void WriteTo(Utf8JsonWriter writer)
/// <summary>
/// Writes the document to a <see cref="Utf8JsonWriter"/> in JSON Path format.
/// </summary>
/// <param name="writer">The <see cref="Utf8JsonWriter"/> instance to write this document to</param>
#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());
Expand All @@ -165,10 +168,10 @@ private void WriteTo(Utf8JsonWriter writer)
writer.WriteEndArray();
}

private string Serialize<T>(T value, CancellationToken cancellationToken)
private string Serialize<T>(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());
}
}
Expand Down

0 comments on commit 72110fa

Please sign in to comment.