-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imported Tingle.AspNetCore.JsonPatch (#231)
- Loading branch information
1 parent
cf0997d
commit 7ff76e5
Showing
97 changed files
with
11,113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Tingle.AspNetCore.JsonPatch/Adapters/AdapterFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Collections; | ||
using System.Dynamic; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Tingle.AspNetCore.JsonPatch.Internal; | ||
|
||
namespace Tingle.AspNetCore.JsonPatch.Adapters; | ||
|
||
/// <summary> | ||
/// The default AdapterFactory to be used for resolving <see cref="IAdapter"/>. | ||
/// </summary> | ||
public class AdapterFactory : IAdapterFactory | ||
{ | ||
internal static AdapterFactory Default { get; } = new(); | ||
|
||
/// <inheritdoc /> | ||
public virtual IAdapter Create(object target, JsonSerializerOptions serializerOptions) | ||
{ | ||
ArgumentNullException.ThrowIfNull(target); | ||
|
||
ArgumentNullException.ThrowIfNull(serializerOptions); | ||
|
||
if (target is JsonObject) | ||
{ | ||
return new JsonObjectAdapter(); | ||
} | ||
if (target is IList) | ||
{ | ||
return new ListAdapter(); | ||
} | ||
else if (target is IDictionary || target is IDictionary<string, object?>) // ExpandoObject implements IDictionary<string, object?> | ||
{ | ||
var intf = target.GetType().GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)); | ||
if (intf != null) | ||
{ | ||
var type = typeof(DictionaryAdapter<,>).MakeGenericType(intf.GetGenericArguments()); | ||
return (IAdapter)Activator.CreateInstance(type)!; | ||
} | ||
} | ||
else if (target is DynamicObject) | ||
{ | ||
return new DynamicObjectAdapter(); | ||
} | ||
|
||
return new PocoAdapter(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Tingle.AspNetCore.JsonPatch/Adapters/IAdapterFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json; | ||
using Tingle.AspNetCore.JsonPatch.Internal; | ||
|
||
namespace Tingle.AspNetCore.JsonPatch.Adapters; | ||
|
||
/// <summary> | ||
/// Defines the operations used for loading an <see cref="IAdapter"/> based on the current object and ContractResolver. | ||
/// </summary> | ||
public interface IAdapterFactory | ||
{ | ||
/// <summary> | ||
/// Creates an <see cref="IAdapter"/> for the current object | ||
/// </summary> | ||
/// <param name="target">The target object</param> | ||
/// <param name="serializerOptions">The current <see cref="JsonSerializerOptions"/></param> | ||
/// <returns>The needed <see cref="IAdapter"/></returns> | ||
IAdapter Create(object target, JsonSerializerOptions serializerOptions); | ||
} |
108 changes: 108 additions & 0 deletions
108
src/Tingle.AspNetCore.JsonPatch/Adapters/IObjectAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using Tingle.AspNetCore.JsonPatch.Operations; | ||
|
||
namespace Tingle.AspNetCore.JsonPatch.Adapters; | ||
|
||
/// <summary> | ||
/// Defines the operations that can be performed on a JSON patch document. | ||
/// </summary> | ||
public interface IObjectAdapter | ||
{ | ||
/// <summary> | ||
/// Using the "add" operation a new value is inserted into the root of the target | ||
/// document, into the target array at the specified valid index, or to a target object at | ||
/// the specified location. | ||
/// | ||
/// When adding to arrays, the specified index MUST NOT be greater than the number of elements in the array. | ||
/// To append the value to the array, the index of "-" character is used (see [RFC6901]). | ||
/// | ||
/// When adding to an object, if an object member does not already exist, a new member is added to the object at the | ||
/// specified location or if an object member does exist, that member's value is replaced. | ||
/// | ||
/// The operation object MUST contain a "value" member whose content | ||
/// specifies the value to be added. | ||
/// | ||
/// For example: | ||
/// | ||
/// { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] } | ||
/// | ||
/// See RFC 6902 https://tools.ietf.org/html/rfc6902#page-4 | ||
/// </summary> | ||
/// <param name="operation">The add operation.</param> | ||
/// <param name="objectToApplyTo">Object to apply the operation to.</param> | ||
void Add(Operation operation, object objectToApplyTo); | ||
|
||
/// <summary> | ||
/// Using the "copy" operation, a value is copied from a specified location to the | ||
/// target location. | ||
/// | ||
/// The operation object MUST contain a "from" member, which references the location in the | ||
/// target document to copy the value from. | ||
/// | ||
/// The "from" location MUST exist for the operation to be successful. | ||
/// | ||
/// For example: | ||
/// | ||
/// { "op": "copy", "from": "/a/b/c", "path": "/a/b/e" } | ||
/// | ||
/// See RFC 6902 https://tools.ietf.org/html/rfc6902#page-7 | ||
/// </summary> | ||
/// <param name="operation">The copy operation.</param> | ||
/// <param name="objectToApplyTo">Object to apply the operation to.</param> | ||
void Copy(Operation operation, object objectToApplyTo); | ||
|
||
/// <summary> | ||
/// Using the "move" operation the value at a specified location is removed and | ||
/// added to the target location. | ||
/// | ||
/// The operation object MUST contain a "from" member, which references the location in the | ||
/// target document to move the value from. | ||
/// | ||
/// The "from" location MUST exist for the operation to be successful. | ||
/// | ||
/// For example: | ||
/// | ||
/// { "op": "move", "from": "/a/b/c", "path": "/a/b/d" } | ||
/// | ||
/// A location cannot be moved into one of its children. | ||
/// | ||
/// See RFC 6902 https://tools.ietf.org/html/rfc6902#page-6 | ||
/// </summary> | ||
/// <param name="operation">The move operation.</param> | ||
/// <param name="objectToApplyTo">Object to apply the operation to.</param> | ||
void Move(Operation operation, object objectToApplyTo); | ||
|
||
/// <summary> | ||
/// Using the "remove" operation the value at the target location is removed. | ||
/// | ||
/// The target location MUST exist for the operation to be successful. | ||
/// | ||
/// For example: | ||
/// | ||
/// { "op": "remove", "path": "/a/b/c" } | ||
/// | ||
/// If removing an element from an array, any elements above the | ||
/// specified index are shifted one position to the left. | ||
/// | ||
/// See RFC 6902 https://tools.ietf.org/html/rfc6902#page-6 | ||
/// </summary> | ||
/// <param name="operation">The remove operation.</param> | ||
/// <param name="objectToApplyTo">Object to apply the operation to.</param> | ||
void Remove(Operation operation, object objectToApplyTo); | ||
|
||
/// <summary> | ||
/// Using the "replace" operation the value at the target location is replaced | ||
/// with a new value. The operation object MUST contain a "value" member | ||
/// which specifies the replacement value. | ||
/// | ||
/// The target location MUST exist for the operation to be successful. | ||
/// | ||
/// For example: | ||
/// | ||
/// { "op": "replace", "path": "/a/b/c", "value": 42 } | ||
/// | ||
/// See RFC 6902 https://tools.ietf.org/html/rfc6902#page-6 | ||
/// </summary> | ||
/// <param name="operation">The replace operation.</param> | ||
/// <param name="objectToApplyTo">Object to apply the operation to.</param> | ||
void Replace(Operation operation, object objectToApplyTo); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Tingle.AspNetCore.JsonPatch/Adapters/IObjectAdapterWithTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Tingle.AspNetCore.JsonPatch.Operations; | ||
|
||
namespace Tingle.AspNetCore.JsonPatch.Adapters; | ||
|
||
/// <summary> | ||
/// Defines the operations that can be performed on a JSON patch document, including "test". | ||
/// </summary> | ||
public interface IObjectAdapterWithTest : IObjectAdapter | ||
{ | ||
/// <summary> | ||
/// Using the "test" operation a value at the target location is compared for | ||
/// equality to a specified value. | ||
/// | ||
/// The operation object MUST contain a "value" member that specifies | ||
/// value to be compared to the target location's value. | ||
/// | ||
/// The target location MUST be equal to the "value" value for the | ||
/// operation to be considered successful. | ||
/// | ||
/// For example: | ||
/// { "op": "test", "path": "/a/b/c", "value": "foo" } | ||
/// | ||
/// See RFC 6902 https://tools.ietf.org/html/rfc6902#page-7 | ||
/// </summary> | ||
/// <param name="operation">The test operation.</param> | ||
/// <param name="objectToApplyTo">Object to apply the operation to.</param> | ||
void Test(Operation operation, object objectToApplyTo); | ||
} |
Oops, something went wrong.