Class Traversr<DataType>

java.lang.Object
com.bazaarvoice.jolt.traversr.Traversr<DataType>
Direct Known Subclasses:
SimpleTraversr

public abstract class Traversr<DataType> extends Object
Traversr allows you to walk JSON tree structures of data, and to GET and SET operations. Corner cases that arise during tree walk, are handled by subclasses. Ex: If no data exists mid tree walk quit or insert a new container? Or if there is data but it is the wrong type : overwrite or skip? Traversr analyzes the path path to be traversed and creates a "linked list" of Traversal objects. Then that list of Traversals can be used many times to write data into different JSON tree structures with different key values. For example given a Shiftr output path of : "tuna[invalid input: '&'1].bob.invalid input: '&'3[]" some of the keys are known, "tuna" and "bob", but other keys will only be known later. However, the structure of the output path will not change, which means we can do some work before the keys are known. First the output path is turned into its canonical form : "tuna.[4].[invalid input: '&'1].bob.invalid input: '&'3.[]". Then, a series of Traversals is created. tuna -> MapTraversal [invalid input: '&'1] -> ArrayTraversal bob -> MapTraversal invalid input: '&'3 -> MapTraversal [] -> AutoExpandArrayTraversal Later, a list of keys can then be provided, such as [ "tuna", "2", "bob", "smith", "[]" ], and they can be quickly used without having to build or parse any more objects. The list of keys are all Strings, which ArrayTraversals will convert to Integers as needed.
  • Field Details

    • root

      private final TraversalStep root
    • traversalLength

      private final int traversalLength
  • Constructor Details

    • Traversr

      public Traversr(String humanPath)
    • Traversr

      public Traversr(List<String> paths)
      Constructor where we provide a known good set of pathElement Strings in a list. Aka, no need to extract it from a "Human Readable" form.
  • Method Details

    • makePathElement

      private TraversalStep makePathElement(String path, TraversalStep child)
    • get

      public Optional<DataType> get(Object tree, List<String> keys)
      Note : Calling this method MAY modify the tree object by adding new Maps and Lists as needed for the traversal. This is determined by the behavior of the implementations of the abstract methods of this class.
    • set

      public Optional<DataType> set(Object tree, List<String> keys, DataType data)
      Parameters:
      tree - tree of Map and List JSON structure to navigate
      data - JSON style data object you want to set
      Returns:
      returns the data object if successfully set, otherwise null if there was a problem walking the path
    • remove

      public Optional<DataType> remove(Object tree, List<String> keys)
      Note : Calling this method MAY modify the tree object by adding new Maps and Lists as needed for the traversal. This is determined by the behavior of the implementations of the abstract methods of this class.
    • handleFinalSet

      public abstract Optional<DataType> handleFinalSet(TraversalStep traversalStep, Object tree, String key, DataType data)
      Allow subclasses to control how "sets" are done, if/once the traversal has made it to the the last element. Overwrite existing data? List-ize existing data with new data?
      Returns:
      the data object if the set was successful, or null if not
    • handleIntermediateGet

      public abstract Optional<DataType> handleIntermediateGet(TraversalStep traversalStep, Object tree, String key, TraversalStep.Operation op)
      Allow subclasses to control how gets are handled for intermediate traversals. Example: we are a MapTraversal and out key is "foo". We simply do a 'tree.get( "foo" )'. However, if we get a null back, or we get back a data type incompatible with our child Traversal, what do we do? Overwrite or just return?