Class MarshallingTraverseContext.Builder<SELF extends MarshallingTraverseContext.Builder<SELF>>

java.lang.Object
org.apache.juneau.marshall.Context.Builder<SELF>
org.apache.juneau.marshall.MarshallingContextable.Builder<SELF>
org.apache.juneau.marshall.MarshallingTraverseContext.Builder<SELF>
Direct Known Subclasses:
JsonSchemaGenerator.Builder, Serializer.Builder
Enclosing class:
MarshallingTraverseContext

public abstract static class MarshallingTraverseContext.Builder<SELF extends MarshallingTraverseContext.Builder<SELF>> extends MarshallingContextable.Builder<SELF>
Builder class.
  • Constructor Details

  • Method Details

    • detectRecursions

      Automatically detect POJO recursions.

      When enabled, specifies that recursions should be checked for during traversal.

      Recursions can occur when traversing models that aren't true trees but rather contain loops.
      When recursion detection is disabled, such loops are instead bounded by maxDepth (default 100): the over-depth branch is silently truncated, producing finite but semantically-incomplete output.
      If the stack is exhausted before maxDepth is reached, the resulting StackOverflowError is converted to a MarshallingRecursionException with the message "Depth too deep. Stack overflow occurred.".

      Notes:
      • Checking for recursion can cause a small performance penalty.
      • This is the recommended way to fail-fast on cyclic bean graphs such as parent/child @ParentProperty references. Under the default config such cycles are instead silently truncated at maxDepth (see the ParentProperty Javadoc).
      Example:

      // Create a serializer that automatically checks for recursions. WriterSerializer serializer = JsonSerializer .create() .detectRecursions() .build(); // Create a POJO model with a recursive loop. public class MyBean { public Object f; } MyBean bean = new MyBean(); bean.f = bean; // Throws a SerializeException and not a StackOverflowError String json = serializer.write(bean);

      Returns:
      This object.
    • detectRecursions

      public SELF detectRecursions(boolean value)
      Same as detectRecursions() but allows you to explicitly specify the value.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.
    • hashKey

      public HashKey hashKey()
      Description copied from class: Context.Builder
      Returns the hashkey of this builder.

      Used to return previously instantiated context beans that have matching hashkeys. The HashKey object is suitable for use as a hashmap key of a map of context beans. A context bean is considered equivalent if the HashKey.equals(Object) method is the same.

      Overrides:
      hashKey in class MarshallingContextable.Builder<SELF extends MarshallingTraverseContext.Builder<SELF>>
      Returns:
      The hashkey of this builder.
    • ignoreRecursions

      Ignore recursion errors.

      When enabled, when we encounter the same object when traversing a tree, we set the value to null.

      For example, if a model contains the links A->B->C->A, then the JSON generated will look like the following when this setting is true...

      {A:{B:{C:null}}}

      Notes:
      • Checking for recursion can cause a small performance penalty.
      • Use this to serialize cyclic bean graphs (such as parent/child @ParentProperty references) by emitting the repeated node as null so the output round-trips cleanly.
      Example:

      // Create a serializer ignores recursions. WriterSerializer serializer = JsonSerializer .create() .ignoreRecursions() .build(); // Create a POJO model with a recursive loop. public class MyBean { public Object f; } MyBean bean = new MyBean(); bean.f = bean; // Produces "{f:null}" String json = serializer.write(bean);

      Returns:
      This object.
    • ignoreRecursions

      public SELF ignoreRecursions(boolean value)
      Same as ignoreRecursions() but allows you to explicitly specify the value.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.
    • initialDepth

      public SELF initialDepth(int value)
      Initial depth.

      The initial indentation level at the root.

      Useful when constructing document fragments that need to be indented at a certain level when whitespace is enabled.

      Example:

      // Create a serializer with whitespace enabled and an initial depth of 2. WriterSerializer serializer = JsonSerializer .create() .ws() .initialDepth(2) .build(); // Produces "\t\t{\n\t\t\t'foo':'bar'\n\t\t}\n" String json = serializer.write(new MyBean());

      Parameters:
      value - The new value for this setting.
      The default is 0.
      Returns:
      This object.
    • maxDepth

      public SELF maxDepth(int value)
      Max traversal depth.

      Specifies the maximum depth traversed in the POJO tree.

      When this depth is exceeded, the over-depth nodes are silently dropped (truncated) from the output — no exception is thrown. This is a size guard that bounds the output of deeply-nested (or cyclic) models; it is not a cycle detector. For genuine recursive references, enable detectRecursions() to fail-fast with a MarshallingRecursionException, or ignoreRecursions() to emit repeated nodes as null so the output round-trips cleanly.

      Example:

      // Create a serializer that truncates output beyond a depth of 20. WriterSerializer serializer = JsonSerializer .create() .maxDepth(20) .build();

      See Also:
      Parameters:
      value - The new value for this setting.
      The default is 100.
      Returns:
      This object.