Ayush
Ayush

Reputation: 284

How to convert String to JsonPatch?

Java String which I retrieved from the database:

[
  { "op": "replace", "path": "/baz", "value": "boo" },
  { "op": "add", "path": "/hello", "value": ["world"] },
  { "op": "remove", "path": "/foo" }
]

How can I convert it to a JsonPatch object?

I am using com.github.fge.jsonpatch library.

Upvotes: 2

Views: 2562

Answers (1)

obourgain
obourgain

Reputation: 9336

Based on JSON Patch documentation, you can build a JsonPatch instance using Jackson deserialization.

 String json = "...";

 final ObjectMapper mapper = new ObjectMapper();
 final InputStream in = new ByteArrayInputStream(json.getBytes());
 final JsonPatch patch = mapper.readValue(in, JsonPatch.class);

Upvotes: 3

Related Questions