Package org.apache.juneau.commons.utils
Class JsonValueSafety
java.lang.Object
org.apache.juneau.commons.utils.JsonValueSafety
Bounded, iterative walker that validates already-materialized JSON values against structural safety limits.
This helper defends against pathological inputs (excessive nesting, unbounded node counts, cyclic references, and
runaway traversal times) before a value is serialized or otherwise consumed. It walks only in-memory maps,
collections, arrays, and scalar leaves; it never resolves
All violations are reported as neutral IllegalArgumentExceptions. Typed or adaptor callers are responsible
for mapping these to the appropriate JSON-RPC error code, which keeps this core class free of any revision-specific
error policy.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum allowed nesting depth of the walked value.static final intMaximum allowed number of distinct nodes (containers and leaves) in the walked value.static final longMaximum allowed wall-clock time, in milliseconds, for a single traversal. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidValidates the specified value against the structural safety limits using a freshdeadline.static voidValidates the specified value against the structural safety limits using the specified traversal deadline.static longComputes a fresh traversal deadline from the current time.static longremainingNanos(long deadlineNanos) Computes the number of nanoseconds remaining before the specified deadline.
-
Field Details
-
MAX_DEPTH
Maximum allowed nesting depth of the walked value.- See Also:
-
MAX_NODES
Maximum allowed number of distinct nodes (containers and leaves) in the walked value.- See Also:
-
MAX_TRAVERSAL_MILLIS
Maximum allowed wall-clock time, in milliseconds, for a single traversal.- See Also:
-
-
Method Details
-
deadlineNanos
Computes a fresh traversal deadline from the current time.- Returns:
- An absolute deadline in
System.nanoTime()units,MAX_TRAVERSAL_MILLISmilliseconds in the future.
-
check
Validates the specified value against the structural safety limits using a freshdeadline.- Parameters:
value- The value to walk. Can benull , in which case this method returns immediately.label- A human-readable label describing the value; used as a prefix in violation messages.- Throws:
IllegalArgumentException- If the value violates any of the depth, node-count, traversal-time, or JSON-shape constraints.
-
check
Validates the specified value against the structural safety limits using the specified traversal deadline.- Parameters:
root- The value to walk. Can benull , in which case this method returns immediately.label- A human-readable label describing the value; used as a prefix in violation messages.deadlineNanos- An absolute deadline inSystem.nanoTime()units after which the traversal is aborted.- Throws:
IllegalArgumentException- If the value exceedsMAX_DEPTH, exceedsMAX_NODES, exceeds the traversal deadline, contains a non-string JSON object key, or contains a leaf that is not a JSON value type (String,Number, orBoolean).
-
remainingNanos
Computes the number of nanoseconds remaining before the specified deadline.- Parameters:
deadlineNanos- An absolute deadline inSystem.nanoTime()units.- Returns:
- The remaining nanoseconds, or
0 if the deadline has already passed.
-