Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonPatchDocument #14618

Merged
merged 5 commits into from
Aug 27, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add initial JSON patch prototype
pakrym committed Jul 23, 2020
commit 76a002d50562da8b543bf1c6a01b1cd593c1966b
80 changes: 80 additions & 0 deletions sdk/core/Azure.Core.Experimental/src/JsonPatchDocument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Text.Json;

#pragma warning disable 1591

namespace Azure.Core.JsonPatch
{
public class JsonPatchDocument
{
public JsonPatchDocument()
{
Operations = new Collection<JsonPatchOperation>();
}

public Collection<JsonPatchOperation> Operations { get; }

public void AppendAdd(string path, string rawJsonValue)
{
Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Add, path, null,rawJsonValue));
}

public void AppendCopy(string from, string path, string rawJsonValue)
{
Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Copy, path, from, rawJsonValue));
}

public void AppendMove(string from, string path, string rawJsonValue)
{
Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Move, path, from, rawJsonValue));
}

public void AppendRemove(string path)
{
Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Remove, path, null, null));
}

public void AppendTest(string path, string rawJsonValue)
{
Operations.Add(new JsonPatchOperation(JsonPatchOperationKind.Test, path, null, rawJsonValue));
}

public override string ToString()
{
using var memoryStream = new MemoryStream();
using (var writer = new Utf8JsonWriter(memoryStream))
{
WriteTo(writer);
}
return Encoding.UTF8.GetString(memoryStream.ToArray());
}

internal void WriteTo(Utf8JsonWriter writer)
{
writer.WriteStartArray();
foreach (var operation in Operations)
{
writer.WriteStartObject();
writer.WriteString("op", operation.Kind.ToString());
writer.WriteString("path", operation.Path);
if (operation.From != null)
{
writer.WriteString("path", operation.From);
}
if (operation.RawJsonValue != null)
{
using var parsedValue = JsonDocument.Parse(operation.RawJsonValue);
writer.WritePropertyName("value");
parsedValue.WriteTo(writer);
}
writer.WriteEndObject();
}
writer.WriteEndArray();
}
}
}
25 changes: 25 additions & 0 deletions sdk/core/Azure.Core.Experimental/src/JsonPatchOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Text.Json;

#pragma warning disable 1591

namespace Azure.Core.JsonPatch
{
public class JsonPatchOperation
{
public JsonPatchOperation(JsonPatchOperationKind kind, string path, string? from, string? rawJsonValue)
{
Kind = kind;
Path = path;
From = from;
RawJsonValue = rawJsonValue;
}

public JsonPatchOperationKind Kind { get; }
public string Path { get; }
public string? From { get; }
public string? RawJsonValue { get; }
}
}
21 changes: 21 additions & 0 deletions sdk/core/Azure.Core.Experimental/src/JsonPatchOperationKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma warning disable 1591

namespace Azure.Core.JsonPatch
{
public readonly struct JsonPatchOperationKind
{
private readonly string _operation;

public JsonPatchOperationKind(string operation)
{
_operation = operation;
}

public static JsonPatchOperationKind Add { get; } = new JsonPatchOperationKind("add");
public static JsonPatchOperationKind Remove { get; } = new JsonPatchOperationKind("remove");
public static JsonPatchOperationKind Replace { get; } = new JsonPatchOperationKind("replace");
public static JsonPatchOperationKind Move { get; } = new JsonPatchOperationKind("move");
public static JsonPatchOperationKind Copy { get; } = new JsonPatchOperationKind("copy");
public static JsonPatchOperationKind Test { get; } = new JsonPatchOperationKind("test");
}
}