Class JsonState

java.lang.Object
com.scivicslab.pojoactor.core.JsonState

public class JsonState extends Object
JSON state container with XPath-style path accessor.

This class provides a lightweight way to store and retrieve dynamic state using XPath-like path expressions. It is designed for workflow state management where compile-time type safety is not required.

Path Syntax

  • $.key or key - access object property
  • $.parent.child - nested property access
  • $.array[0] - array index access
  • $.data[0].name - combined access

Usage Example


 JsonState state = new JsonState();

 // Write values
 state.put("workflow.retry", 3);
 state.put("hosts[0]", "server1.example.com");

 // Read values
 int retry = state.getInt("$.workflow.retry", 0);
 String host = state.getString("$.hosts[0]");

 // Check existence
 if (state.has("$.workflow.timeout")) {
     // ...
 }
 
Since:
2.10.0
Author:
devteam@scivics-lab.com
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty JsonState.
    JsonState(com.fasterxml.jackson.databind.node.ObjectNode root)
    Constructs a JsonState from an existing ObjectNode.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears all values.
    Creates a deep copy of this JsonState.
    Returns an iterator over the top-level field names.
    Optional<com.fasterxml.jackson.databind.JsonNode>
    get(String path)
    Gets an Optional value at the path.
    boolean
    getBoolean(String path, boolean defaultValue)
    Gets a boolean value at the path.
    double
    getDouble(String path, double defaultValue)
    Gets a double value at the path.
    int
    getInt(String path, int defaultValue)
    Gets an integer value at the path.
    long
    getLong(String path, long defaultValue)
    Gets a long value at the path.
    com.fasterxml.jackson.databind.node.ObjectNode
    Returns the root ObjectNode for direct manipulation.
    Gets a string value at the path.
    getString(String path, String defaultValue)
    Gets a string value at the path with default.
    boolean
    has(String path)
    Checks if a value exists at the path.
    boolean
    Checks if the state is empty.
    put(String path, Object value)
    Sets a value at the given path, creating intermediate nodes as needed.
    boolean
    remove(String path)
    Removes a value at the given path.
    com.fasterxml.jackson.databind.JsonNode
    select(String path)
    Selects a JsonNode at the given path.
    int
    Returns the number of top-level keys.
    Returns a pretty-printed JSON string.
    Returns the JSON string representation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • JsonState

      public JsonState()
      Constructs an empty JsonState.
    • JsonState

      public JsonState(com.fasterxml.jackson.databind.node.ObjectNode root)
      Constructs a JsonState from an existing ObjectNode.
      Parameters:
      root - the root node
  • Method Details

    • select

      public com.fasterxml.jackson.databind.JsonNode select(String path)
      Selects a JsonNode at the given path.

      Path syntax:

      • $.key or key - object property
      • $.parent.child - nested property
      • $.array[0] - array index
      Parameters:
      path - the XPath-style path expression
      Returns:
      the JsonNode at the path, or MissingNode if not found
    • getString

      public String getString(String path)
      Gets a string value at the path.
      Parameters:
      path - the path expression
      Returns:
      the string value, or null if not found
    • getString

      public String getString(String path, String defaultValue)
      Gets a string value at the path with default.
      Parameters:
      path - the path expression
      defaultValue - the default value if not found
      Returns:
      the string value, or defaultValue if not found
    • getInt

      public int getInt(String path, int defaultValue)
      Gets an integer value at the path.
      Parameters:
      path - the path expression
      defaultValue - the default value if not found
      Returns:
      the integer value, or defaultValue if not found
    • getLong

      public long getLong(String path, long defaultValue)
      Gets a long value at the path.
      Parameters:
      path - the path expression
      defaultValue - the default value if not found
      Returns:
      the long value, or defaultValue if not found
    • getDouble

      public double getDouble(String path, double defaultValue)
      Gets a double value at the path.
      Parameters:
      path - the path expression
      defaultValue - the default value if not found
      Returns:
      the double value, or defaultValue if not found
    • getBoolean

      public boolean getBoolean(String path, boolean defaultValue)
      Gets a boolean value at the path.
      Parameters:
      path - the path expression
      defaultValue - the default value if not found
      Returns:
      the boolean value, or defaultValue if not found
    • get

      public Optional<com.fasterxml.jackson.databind.JsonNode> get(String path)
      Gets an Optional value at the path.
      Parameters:
      path - the path expression
      Returns:
      Optional containing the JsonNode, or empty if not found
    • has

      public boolean has(String path)
      Checks if a value exists at the path.
      Parameters:
      path - the path expression
      Returns:
      true if a non-null value exists at the path
    • put

      public JsonState put(String path, Object value)
      Sets a value at the given path, creating intermediate nodes as needed.

      Path syntax is the same as select(String).

      Parameters:
      path - the path expression
      value - the value to set (String, Number, Boolean, or null)
      Returns:
      this JsonState for method chaining
    • remove

      public boolean remove(String path)
      Removes a value at the given path.
      Parameters:
      path - the path expression
      Returns:
      true if a value was removed
    • getRoot

      public com.fasterxml.jackson.databind.node.ObjectNode getRoot()
      Returns the root ObjectNode for direct manipulation.
      Returns:
      the root node
    • isEmpty

      public boolean isEmpty()
      Checks if the state is empty.
      Returns:
      true if no values are stored
    • size

      public int size()
      Returns the number of top-level keys.
      Returns:
      the number of keys
    • clear

      public void clear()
      Clears all values.
    • toString

      public String toString()
      Returns the JSON string representation.
      Overrides:
      toString in class Object
      Returns:
      JSON string
    • toPrettyString

      public String toPrettyString()
      Returns a pretty-printed JSON string.
      Returns:
      formatted JSON string
    • fieldNames

      public Iterator<String> fieldNames()
      Returns an iterator over the top-level field names.
      Returns:
      field name iterator
    • copy

      public JsonState copy()
      Creates a deep copy of this JsonState.
      Returns:
      a new JsonState with copied data