Class CopyPathsAction

java.lang.Object
net.minidev.json.actions.navigate.CopyPathsAction
All Implemented Interfaces:
JSONNavigateAction, NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>

public class CopyPathsAction extends Object implements JSONNavigateAction
Creates a copy of a JSONObject containing just the nodes on the paths specified.

Specified paths that do not exist in the source object are ignored silently. Specifying an empty list of paths to navigate or only non-existent paths will result in an empty object being returned.

See package-info for more details

Example:

To copy the branch k1.k2 from {k1:{k2:v1}, k3:{k4:v2}} instantiate the copier like so: new JSONObjectCopier("k1.k2") The resulting copy would be {k1:{k2:v1}}

See unit tests for more examples

Since:
15 March 2016.
Author:
adoneitan@gmail.com
  • Field Details

    • destTree

      protected net.minidev.json.JSONObject destTree
    • destBranch

      protected net.minidev.json.JSONObject destBranch
    • destNodeStack

      protected Stack<Object> destNodeStack
  • Constructor Details

    • CopyPathsAction

      public CopyPathsAction()
  • Method Details

    • start

      public boolean start(net.minidev.json.JSONObject source, Collection<String> pathsToCopy)
      Description copied from interface: NavigateAction
      called before any navigation of the NavigateAction starts
      Specified by:
      start in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      source - TODO
      pathsToCopy - TODO
      Returns:
      true if navigation should start at all
    • recurInto

      public boolean recurInto(TreePath jp, net.minidev.json.JSONObject o)
      Description copied from interface: NavigateAction
      called when an object node is encountered on the path
      Specified by:
      recurInto in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - TODO
      o - TODO
      Returns:
      true if the navigator should navigate into the object
    • recurInto

      public boolean recurInto(TreePath jp, net.minidev.json.JSONArray o)
      Description copied from interface: NavigateAction
      called when an array node is encountered on the path
      Specified by:
      recurInto in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - TODO
      o - TODO
      Returns:
      true if the navigator should navigate into the array
    • foundLeafBeforePathEnd

      public void foundLeafBeforePathEnd(TreePath jp, Object obj)
      Description copied from interface: NavigateAction
      reached leaf node (not a container) in source but specified path expects children - the specified path does not exist in the source
      Specified by:
      foundLeafBeforePathEnd in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - TODO
      obj - TODO
    • pathTailNotFound

      public void pathTailNotFound(TreePath jp, Object source)
      Description copied from interface: NavigateAction
      reached end of branch in source before end of specified path - the specified path does not exist in the source
      Specified by:
      pathTailNotFound in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - TODO
      source - TODO
    • handleLeaf

      public void handleLeaf(TreePath jp, Object o)
      Description copied from interface: NavigateAction
      called when a leaf node is reached in a M. a leaf in a M is a key-value pair where the value is not a container itself (it is not a M nor a L)
      Specified by:
      handleLeaf in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - - the JsonPath pointing to the leaf
      o - TODO
    • handleLeaf

      public void handleLeaf(TreePath jp, int arrIndex, Object o)
      Description copied from interface: NavigateAction
      called when a leaf in a L is reached. a leaf in a L is a non-container item (it is not a M nor a L)
      Specified by:
      handleLeaf in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - -
      arrIndex - -
      o - - the item
    • recurEnd

      public void recurEnd(TreePath jp, net.minidev.json.JSONObject jo)
      Description copied from interface: NavigateAction
      called when navigation of an NavigateAction type object ends
      Specified by:
      recurEnd in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - the path pointing to the object
      jo - TODO
    • recurEnd

      public void recurEnd(TreePath jp, net.minidev.json.JSONArray ja)
      Description copied from interface: NavigateAction
      called when navigation of an NavigateAction type object ends
      Specified by:
      recurEnd in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      jp - the path pointing to the object
      ja - TODO
    • pathStart

      public boolean pathStart(String path)
      Description copied from interface: NavigateAction
      called before navigation of a new path starts
      Specified by:
      pathStart in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      path - TODO
      Returns:
      true if the specified path should be navigated
    • pathEnd

      public void pathEnd(String path)
      Description copied from interface: NavigateAction
      called after the navigation of a path ends
      Specified by:
      pathEnd in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      path - TODO
    • failSilently

      public boolean failSilently(String path, Exception e)
      Description copied from interface: NavigateAction
      called if navigation of a path throws an exception
      Specified by:
      failSilently in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      path - TODO
      e - TODO
      Returns:
      true if the failure on this path should not abort the rest of the navigation
    • failFast

      public boolean failFast(String path, Exception e)
      Description copied from interface: NavigateAction
      called if navigation of a path throws an exception
      Specified by:
      failFast in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Parameters:
      path - TODO
      e - TODO
      Returns:
      true if the failure on this path should abort the rest of the navigation
    • end

      public void end()
      Description copied from interface: NavigateAction
      called after all navigation ends, and just before the navigation method exits
      Specified by:
      end in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
    • result

      public Object result()
      Description copied from interface: NavigateAction
      holds the result of the navigation, as assigned by the action implementing this interface
      Specified by:
      result in interface NavigateAction<net.minidev.json.JSONObject,net.minidev.json.JSONArray>
      Returns:
      - result