如何将 json-patch 应用到 .net core 中的普通 json?
How to apply json-patch to plain json in .net core?
JsonPatchDocument.Apply 方法适用于对象图,但我想将 json 补丁应用于普通 json.
例如,假设我有这个 json:
{ "name": "JSON Patch", "text": "OLD" }
如何使用 C# 应用这样的补丁?
[ { "op": "replace", "path": "/text", "value": "NEW VALUE" } ]
这是如何使用 C# 和 .NET 核心完成的?
这是应用补丁的代码片段:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var json="{ \"name\": \"JSON Patch\", \"text\": \"OLD\" }";
var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var operationStrings = "[ { \"op\": \"replace\", \"path\": \"/text\", \"value\": \"NEW VALUE\" } ] ";
var ops = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Microsoft.AspNetCore.JsonPatch.Operations.Operation>>(operationStrings);
var patchDocument = new Microsoft.AspNetCore.JsonPatch.JsonPatchDocument(ops, new Newtonsoft.Json.Serialization.DefaultContractResolver());
patchDocument.ApplyTo(jsonObj);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj));
}
}
JsonPatchDocument.Apply 方法适用于对象图,但我想将 json 补丁应用于普通 json.
例如,假设我有这个 json:
{ "name": "JSON Patch", "text": "OLD" }
如何使用 C# 应用这样的补丁?
[ { "op": "replace", "path": "/text", "value": "NEW VALUE" } ]
这是如何使用 C# 和 .NET 核心完成的?
这是应用补丁的代码片段:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var json="{ \"name\": \"JSON Patch\", \"text\": \"OLD\" }";
var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var operationStrings = "[ { \"op\": \"replace\", \"path\": \"/text\", \"value\": \"NEW VALUE\" } ] ";
var ops = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Microsoft.AspNetCore.JsonPatch.Operations.Operation>>(operationStrings);
var patchDocument = new Microsoft.AspNetCore.JsonPatch.JsonPatchDocument(ops, new Newtonsoft.Json.Serialization.DefaultContractResolver());
patchDocument.ApplyTo(jsonObj);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj));
}
}