Skip to content

Commit 7483c60

Browse files
committed
Added S.T.J-serialization based JsonPatch
1 parent 6842853 commit 7483c60

File tree

72 files changed

+7886
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+7886
-0
lines changed

AspNetCore.slnx

+4
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@
211211
<Project Path="src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/Microsoft.AspNetCore.Tests.csproj" />
212212
</Folder>
213213
<Folder Name="/src/Features/" Id="e81caa0a-8d6a-e3b6-28b3-ab416e1657a7" />
214+
<Folder Name="/src/Features/JsonPatch.SystemTextJson/" Id="02ea681e-c7d8-13c7-8484-4ac65e1b71e8">
215+
<Project Path="src/Features/JsonPatch.SystemTextJson/src/Microsoft.AspNetCore.JsonPatch.SystemTextJson.csproj" />
216+
<Project Path="src/Features/JsonPatch.SystemTextJson/test/Microsoft.AspNetCore.JsonPatch.SystemTextJson.Tests.csproj" />
217+
</Folder>
214218
<Folder Name="/src/Features/JsonPatch/" Id="077a40ee-d664-9e06-acde-21ca54b47638">
215219
<Project Path="src/Features/JsonPatch/src/Microsoft.AspNetCore.JsonPatch.csproj" />
216220
<Project Path="src/Features/JsonPatch/test/Microsoft.AspNetCore.JsonPatch.Tests.csproj" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1.0",
3+
"components": [
4+
"Microsoft.Net.Component.4.6.2.TargetingPack",
5+
"Microsoft.Net.Component.4.7.2.SDK",
6+
"Microsoft.Net.Component.4.7.2.TargetingPack",
7+
"Microsoft.VisualStudio.Workload.ManagedDesktop",
8+
"Microsoft.VisualStudio.Workload.NetCoreTools",
9+
"Microsoft.VisualStudio.Workload.NetWeb",
10+
"Microsoft.VisualStudio.Workload.VisualStudioExtension"
11+
]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"solution": {
3+
"path": "..\\..\\..\\AspNetCore.slnx",
4+
"projects" : [
5+
"src\\Features\\JsonPatch.SystemTextJson\\src\\Microsoft.AspNetCore.JsonPatch.SystemTextJson.csproj",
6+
"src\\Features\\JsonPatch.SystemTextJson\\test\\Microsoft.AspNetCore.JsonPatch.SystemTextJson.Tests.csproj"
7+
]
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
@ECHO OFF
3+
SET RepoRoot=%~dp0..\..\..
4+
%RepoRoot%\eng\build.cmd -projects %~dp0**\*.*proj %*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
repo_root="$DIR/../.."
7+
"$repo_root/eng/build.sh" --projects "$DIR/**/*.*proj" "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.Text.Json.Nodes;
8+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Internal;
9+
using Microsoft.AspNetCore.Shared;
10+
11+
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Adapters;
12+
13+
/// <summary>
14+
/// The default AdapterFactory to be used for resolving <see cref="IAdapter"/>.
15+
/// </summary>
16+
internal class AdapterFactory : IAdapterFactory
17+
{
18+
internal static AdapterFactory Default { get; } = new();
19+
20+
/// <inheritdoc />
21+
public virtual IAdapter Create(object target)
22+
{
23+
ArgumentNullThrowHelper.ThrowIfNull(target);
24+
25+
var typeToConvert = target.GetType();
26+
if (typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(Dictionary<,>))
27+
{
28+
return (IAdapter)Activator.CreateInstance(typeof(DictionaryAdapter<,>).MakeGenericType(typeToConvert.GenericTypeArguments[0], typeToConvert.GenericTypeArguments[1]));
29+
}
30+
31+
return target switch
32+
{
33+
JsonObject => new JsonObjectAdapter(),
34+
JsonArray => new ListAdapter(),
35+
IList => new ListAdapter(),
36+
_ => new PocoAdapter()
37+
};
38+
}
39+
}
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Internal;
5+
6+
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Adapters;
7+
8+
/// <summary>
9+
/// Defines the operations used for loading an <see cref="IAdapter"/> based on the current object and ContractResolver.
10+
/// </summary>
11+
public interface IAdapterFactory
12+
{
13+
/// <summary>
14+
/// Creates an <see cref="IAdapter"/> for the current object
15+
/// </summary>
16+
/// <param name="target">The target object</param>
17+
/// <returns>The needed <see cref="IAdapter"/></returns>
18+
IAdapter Create(object target);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations;
5+
6+
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Adapters;
7+
8+
/// <summary>
9+
/// Defines the operations that can be performed on a JSON patch document.
10+
/// </summary>
11+
public interface IObjectAdapter
12+
{
13+
/// <summary>
14+
/// Using the "add" operation a new value is inserted into the root of the target
15+
/// document, into the target array at the specified valid index, or to a target object at
16+
/// the specified location.
17+
///
18+
/// When adding to arrays, the specified index MUST NOT be greater than the number of elements in the array.
19+
/// To append the value to the array, the index of "-" character is used (see [RFC6901]).
20+
///
21+
/// When adding to an object, if an object member does not already exist, a new member is added to the object at the
22+
/// specified location or if an object member does exist, that member's value is replaced.
23+
///
24+
/// The operation object MUST contain a "value" member whose content
25+
/// specifies the value to be added.
26+
///
27+
/// For example:
28+
///
29+
/// { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] }
30+
///
31+
/// See RFC 6902 <see href="https://tools.ietf.org/html/rfc6902#page-4"/>
32+
/// </summary>
33+
/// <param name="operation">The add operation.</param>
34+
/// <param name="objectToApplyTo">Object to apply the operation to.</param>
35+
void Add(Operation operation, object objectToApplyTo);
36+
37+
/// <summary>
38+
/// Using the "copy" operation, a value is copied from a specified location to the
39+
/// target location.
40+
///
41+
/// The operation object MUST contain a "from" member, which references the location in the
42+
/// target document to copy the value from.
43+
///
44+
/// The "from" location MUST exist for the operation to be successful.
45+
///
46+
/// For example:
47+
///
48+
/// { "op": "copy", "from": "/a/b/c", "path": "/a/b/e" }
49+
///
50+
/// See RFC 6902 <see href="https://tools.ietf.org/html/rfc6902#page-7"/>
51+
/// </summary>
52+
/// <param name="operation">The copy operation.</param>
53+
/// <param name="objectToApplyTo">Object to apply the operation to.</param>
54+
void Copy(Operation operation, object objectToApplyTo);
55+
56+
/// <summary>
57+
/// Using the "move" operation the value at a specified location is removed and
58+
/// added to the target location.
59+
///
60+
/// The operation object MUST contain a "from" member, which references the location in the
61+
/// target document to move the value from.
62+
///
63+
/// The "from" location MUST exist for the operation to be successful.
64+
///
65+
/// For example:
66+
///
67+
/// { "op": "move", "from": "/a/b/c", "path": "/a/b/d" }
68+
///
69+
/// A location cannot be moved into one of its children.
70+
///
71+
/// See RFC 6902 <see href="https://tools.ietf.org/html/rfc6902#page-6"/>
72+
/// </summary>
73+
/// <param name="operation">The move operation.</param>
74+
/// <param name="objectToApplyTo">Object to apply the operation to.</param>
75+
void Move(Operation operation, object objectToApplyTo);
76+
77+
/// <summary>
78+
/// Using the "remove" operation the value at the target location is removed.
79+
///
80+
/// The target location MUST exist for the operation to be successful.
81+
///
82+
/// For example:
83+
///
84+
/// { "op": "remove", "path": "/a/b/c" }
85+
///
86+
/// If removing an element from an array, any elements above the
87+
/// specified index are shifted one position to the left.
88+
///
89+
/// See RFC 6902 <see href="https://tools.ietf.org/html/rfc6902#page-6"/>
90+
/// </summary>
91+
/// <param name="operation">The remove operation.</param>
92+
/// <param name="objectToApplyTo">Object to apply the operation to.</param>
93+
void Remove(Operation operation, object objectToApplyTo);
94+
95+
/// <summary>
96+
/// Using the "replace" operation the value at the target location is replaced
97+
/// with a new value. The operation object MUST contain a "value" member
98+
/// which specifies the replacement value.
99+
///
100+
/// The target location MUST exist for the operation to be successful.
101+
///
102+
/// For example:
103+
///
104+
/// { "op": "replace", "path": "/a/b/c", "value": 42 }
105+
///
106+
/// See RFC 6902 <see href="https://tools.ietf.org/html/rfc6902#page-6"/>
107+
/// </summary>
108+
/// <param name="operation">The replace operation.</param>
109+
/// <param name="objectToApplyTo">Object to apply the operation to.</param>
110+
void Replace(Operation operation, object objectToApplyTo);
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations;
5+
6+
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Adapters;
7+
8+
/// <summary>
9+
/// Defines the operations that can be performed on a JSON patch document, including "test".
10+
/// </summary>
11+
public interface IObjectAdapterWithTest : IObjectAdapter
12+
{
13+
/// <summary>
14+
/// Using the "test" operation a value at the target location is compared for
15+
/// equality to a specified value.
16+
///
17+
/// The operation object MUST contain a "value" member that specifies
18+
/// value to be compared to the target location's value.
19+
///
20+
/// The target location MUST be equal to the "value" value for the
21+
/// operation to be considered successful.
22+
///
23+
/// For example:
24+
/// { "op": "test", "path": "/a/b/c", "value": "foo" }
25+
///
26+
/// See RFC 6902 <see href="https://tools.ietf.org/html/rfc6902#page-7"/>
27+
/// </summary>
28+
/// <param name="operation">The test operation.</param>
29+
/// <param name="objectToApplyTo">Object to apply the operation to.</param>
30+
void Test(Operation operation, object objectToApplyTo);
31+
}

0 commit comments

Comments
 (0)