JSON来自 JSONPatch+JSON 的补丁转义斜杠“/”

JSONPatch escape slash '/' from JSONPatch+JSON

我低于 JSON,我想从中更新几个字段

{
        "process": "my-process",
        "pod": "some-pod",
        "org": "some-org",
        "config": {
            "version": "436_601_83.0.0",
            "path": "companyName/ccg",
            "description": "update the version",
            "dependencies": null
        }
}

使用邮递员 PATCH API 调用以下 JSON 补丁有效负载 API 工作正常

[
    {
        "path": "/config/version",
        "op": "replace",
        "value": "436_605_83.0.0"
    },
    {
        "path": "/config/description",
        "op": "replace",
        "value": "foo bar"
    }
]

但是,我想使用 Java 实现相同的功能。我试过了

JsonPatch jsonPatch = new JsonPatch(
                        Arrays.asList(               
                           new ReplaceOperation(JsonPointer.of("/config/version"),
                              new TextNode("436_605_83.0.0"))
                        )
                );

计算结果为:

[{"op":"replace","path":"/~1config~1version","value":"436_605_83.0.0"}]

该文档提到 http://jsonpatch.com/#json-pointer 我们必须使用 ~0~1 来转义字符,但运气不好,我使用 ~1 转义了 /"~1config~1version" 但它的计算结果为 "/~01config~01version"

我认为问题出在 JsonPointer 定义中。请改用类似的方法:

JsonPatch jsonPatch = new JsonPatch(
  Arrays.asList(               
    new ReplaceOperation(
      // Note we should provide the different paths tokens here
      JsonPointer.of("config", "version"),
      new TextNode("436_605_83.0.0")
    )
  )
);

或者,等价地:

JsonPatch jsonPatch = new JsonPatch(
  Arrays.asList(               
    new ReplaceOperation(
      // Create the JsonPointer with the full path
      new JsonPointer("/config/version"),
      new TextNode("436_605_83.0.0")
    )
  )
);

请参阅 this test,它提供了有关如何从战术上构建 JsonPointer 以及转义保留字符的含义的指导。