All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.inversion.json.JSPatch Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
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