Package com.scivicslab.pojoactor.core
Class JsonState
java.lang.Object
com.scivicslab.pojoactor.core.JsonState
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
$.keyorkey- 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 -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all values.copy()Creates a deep copy of this JsonState.Returns an iterator over the top-level field names.Optional<com.fasterxml.jackson.databind.JsonNode> Gets an Optional value at the path.booleangetBoolean(String path, boolean defaultValue) Gets a boolean value at the path.doubleGets a double value at the path.intGets an integer value at the path.longGets a long value at the path.com.fasterxml.jackson.databind.node.ObjectNodegetRoot()Returns the root ObjectNode for direct manipulation.Gets a string value at the path.Gets a string value at the path with default.booleanChecks if a value exists at the path.booleanisEmpty()Checks if the state is empty.Sets a value at the given path, creating intermediate nodes as needed.booleanRemoves a value at the given path.com.fasterxml.jackson.databind.JsonNodeSelects a JsonNode at the given path.intsize()Returns the number of top-level keys.Returns a pretty-printed JSON string.toString()Returns the JSON string representation.
-
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
Selects a JsonNode at the given path.Path syntax:
$.keyorkey- 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
Gets a string value at the path.- Parameters:
path- the path expression- Returns:
- the string value, or null if not found
-
getString
Gets a string value at the path with default.- Parameters:
path- the path expressiondefaultValue- the default value if not found- Returns:
- the string value, or defaultValue if not found
-
getInt
Gets an integer value at the path.- Parameters:
path- the path expressiondefaultValue- the default value if not found- Returns:
- the integer value, or defaultValue if not found
-
getLong
Gets a long value at the path.- Parameters:
path- the path expressiondefaultValue- the default value if not found- Returns:
- the long value, or defaultValue if not found
-
getDouble
Gets a double value at the path.- Parameters:
path- the path expressiondefaultValue- the default value if not found- Returns:
- the double value, or defaultValue if not found
-
getBoolean
Gets a boolean value at the path.- Parameters:
path- the path expressiondefaultValue- the default value if not found- Returns:
- the boolean value, or defaultValue if not found
-
get
Gets an Optional value at the path.- Parameters:
path- the path expression- Returns:
- Optional containing the JsonNode, or empty if not found
-
has
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
Sets a value at the given path, creating intermediate nodes as needed.Path syntax is the same as
select(String).- Parameters:
path- the path expressionvalue- the value to set (String, Number, Boolean, or null)- Returns:
- this JsonState for method chaining
-
remove
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
Returns the JSON string representation. -
toPrettyString
Returns a pretty-printed JSON string.- Returns:
- formatted JSON string
-
fieldNames
Returns an iterator over the top-level field names.- Returns:
- field name iterator
-
copy
Creates a deep copy of this JsonState.- Returns:
- a new JsonState with copied data
-