12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.AspNetCore.JsonPatch;
- using Microsoft.AspNetCore.JsonPatch.Operations;
- namespace System
- {
- public static class JsonPatchHelper
- {
- public static T Add<T>(this T t, string path, object value) where T : class
- {
- JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
- jsonPatch.Operations.Add(new Operation<T>("add", path, null, value));
- jsonPatch.ApplyTo(t);
- return t;
- }
- public static T Remove<T>(this T t, string path) where T : class
- {
- JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
- jsonPatch.Operations.Add(new Operation<T>("remove", path, null, null));
- jsonPatch.ApplyTo(t);
- return t;
- }
- public static T Replace<T>(this T t, string path, object value) where T : class
- {
- JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
- jsonPatch.Operations.Add(new Operation<T>("replace", path, null, value));
- jsonPatch.ApplyTo(t);
- return t;
- }
- public static T Move<T>(this T t, string from, string path) where T : class
- {
- JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
- jsonPatch.Operations.Add(new Operation<T>("move", path, from));
- jsonPatch.ApplyTo(t);
- return t;
- }
- public static T Copy<T>(this T t, string from, string path) where T : class
- {
- JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
- jsonPatch.Operations.Add(new Operation<T>("copy", path, from));
- jsonPatch.ApplyTo(t);
- return t;
- }
- }
- }
|