io.inversion.json.JSPatch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of inversion-json Show documentation
Show all versions of inversion-json Show documentation
Inversion Cloud API Engine
package io.inversion.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flipkart.zjsonpatch.JsonPatch;
import io.inversion.utils.Utils;
public interface JSPatch {
JSNode getJson();
default void patch(JSList patches) {
//-- migrate legacy "." based paths to JSONPointer
for (JSNode patch : patches.asMapList()) {
Object pathVal = patch.get("path");
String path = pathVal != null ? pathVal.toString() : null;
if (path != null && !path.startsWith("/")) {
path = "/" + path.replace(".", "/");
}
patch.put("path", path);
Object fromVal = patch.get("from");
path = fromVal != null ? fromVal.toString() : null;
if (path != null && !path.startsWith("/")) {
path = "/" + path.replace(".", "/");
patch.put("from", path);
}
}
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode target = JsonPatch.apply(mapper.readValue(patches.toString(), JsonNode.class), mapper.readValue(getJson().toString(), JsonNode.class));
JSNode patched = JSParser.asJSNode(target.toString());
getJson().clear();
for(String key : patched.keySet()){
getJson().put(key, patched.get(key));
}
if (getJson().isList()) {
JSList arr = (JSList)getJson();
arr.clear();
arr.addAll(((JSList) patched));
}
} catch (Exception e) {
Utils.rethrow(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy