Package com.bazaarvoice.jolt.traversr
Class Traversr<DataType>
java.lang.Object
com.bazaarvoice.jolt.traversr.Traversr<DataType>
- Direct Known Subclasses:
SimpleTraversr
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionNote : Calling this method MAY modify the tree object by adding new Maps and Lists as needed for the traversal.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.handleIntermediateGet
(TraversalStep traversalStep, Object tree, String key, TraversalStep.Operation op) Allow subclasses to control how gets are handled for intermediate traversals.private TraversalStep
makePathElement
(String path, TraversalStep child) Note : Calling this method MAY modify the tree object by adding new Maps and Lists as needed for the traversal.
-
Field Details
-
root
-
traversalLength
private final int traversalLength
-
-
Constructor Details
-
Traversr
-
Traversr
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
-
get
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
- Parameters:
tree
- tree of Map and List JSON structure to navigatedata
- 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
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?
-