# Class SnmpMessageTree

java.lang.Object
com.snmp4j.smi.ber.SnmpMessageTree

public class SnmpMessageTree extends Object
Parses a raw SNMP message byte array into a `BERNode` tree that reflects the BER (Basic Encoding Rules) structure of the message. Supports SNMPv1, SNMPv2c, and SNMPv3, including decryption of AUTH_PRIV ScopedPDUs when matching `DirectUserTarget` credentials are provided.

The resulting tree can be rendered as indented text (`formatAsText(BERNode)`) or as a JSON object tree (`formatAsJson(BERNode)`).

Since:
  2.0.0
Author:
  Frank Fock

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SnmpMessageTree(List<? extends org.snmp4j.DirectUserTarget<?>> targets)`
  Creates a new `SnmpMessageTree` instance.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `BERNode`
  `buildTreeOfBER(byte[] messageBytes)`
  Parse `messageBytes` and return the root `BERNode` of the BER tree.
  `String`
  `formatAsJson(BERNode root)`
  Render the BER node tree as a JSON object tree (pretty-printed, 2-space indent).
  `String`
  `formatAsText(BERNode root)`
  Render the BER node tree as an indented text tree using box-drawing characters.
  `static String`
  `getBerNodeJsonSchema()`
  Return the JSON Schema (Draft 2020-12) describing the `BERNode` / `BERNodeError` JSON structure produced by `formatAsJson(BERNode)`.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### SnmpMessageTree

    public SnmpMessageTree(List<? extends org.snmp4j.DirectUserTarget<?>> targets)
    Creates a new `SnmpMessageTree` instance.
    Parameters:
      `targets` - SNMPv3 credentials for decrypting AUTH_PRIV messages; may be null (decryption will be skipped). Targets that have a non-empty `authoritativeEngineID` are used as-is (their keys must already be localized). Targets without an engine ID are kept in a deferred pool: when the packet's engine ID is extracted at parse time, their passphrase is localized on the fly.

- ## Method Details

  - ### buildTreeOfBER

    public BERNode buildTreeOfBER(byte[] messageBytes)
    Parse `messageBytes` and return the root `BERNode` of the BER tree. Errors are represented as `BERNodeError` leaf nodes rather than thrown.
    Parameters:
      `messageBytes` - the message bytes to parse
    Returns:
      the root `BERNode` of the BER tree

  - ### formatAsText

    public String formatAsText(BERNode root)
    Render the BER node tree as an indented text tree using box-drawing characters.

        SEQUENCE (SNMP Message)  [tag=0x30, pos=0, len=117]
        ├── INTEGER (version) = 1 → SNMPv2c  [tag=0x02, pos=2, len=3]
        └── GetResponse-PDU  [tag=0xa2, pos=13, len=104]
            ├── ...

    Parameters:
      `root` - the root `BERNode` of the BER tree to render
    Returns:
      the rendered text tree.

  - ### formatAsJson

    public String formatAsJson(BERNode root)
    Render the BER node tree as a JSON object tree (pretty-printed, 2-space indent).
    Parameters:
      `root` - the root `BERNode` of the BER tree to render.
    Returns:
      the rendered JSON object tree.

  - ### getBerNodeJsonSchema

    public static String getBerNodeJsonSchema()
    Return the JSON Schema (Draft 2020-12) describing the `BERNode` / `BERNodeError` JSON structure produced by `formatAsJson(BERNode)`.
    Returns:
      the JSON Schema.