PythonValue API
PythonValue wraps a Python evaluation result and provides typed access methods. Returned by eval() and batchEval().
Type Conversion Methods
| Method | Returns | Python Type | Description |
|---|---|---|---|
asInt() |
int |
int |
Convert to Java int |
asLong() |
long |
int |
Convert to Java long |
asDouble() |
double |
float / int |
Convert to Java double |
asBoolean() |
boolean |
bool |
Convert to Java boolean |
asString() |
String |
str |
Convert to Java String |
asList() |
List<Object> |
list |
Convert to raw list |
asList(Class<T>) |
List<T> |
list |
Convert to typed list |
asMap() |
Map<String, Object> |
dict |
Convert to raw map |
asMap(Class<K>, Class<V>) |
Map<K, V> |
dict |
Convert to typed map |
asBytes() |
byte[] |
bytes |
Convert to byte array |
toJson() |
String |
any | JSON string representation |
toJson(boolean) |
String |
any | JSON with optional pretty-print |
raw() |
Object |
any | Get raw Java object |
isNull() |
boolean |
None / null |
Check if value is null |
asDoubleMap() |
Map<String, Double> |
dict |
Number values as Double |
asStringMap() |
Map<String, String> |
dict |
String values |
asIntMap() |
Map<String, Integer> |
dict |
Integer values |
asLongMap() |
Map<String, Long> |
dict |
Long values |
Usage Examples
PythonValue val = py.eval("42");
int i = val.asInt(); // 42
long l = val.asLong(); // 42L
double d = val.asDouble(); // 42.0
PythonValue listVal = py.eval("[1, 2, 3]");
List<Integer> list = listVal.asList(Integer.class); // [1, 2, 3]
PythonValue dictVal = py.eval("{'a': 1, 'b': 2}");
Map<String, Integer> map = dictVal.asMap(String.class, Integer.class);
PythonValue bytesVal = py.eval("b'hello'");
byte[] bytes = bytesVal.asBytes();
// JSON serialization
String json = py.eval("{'key': [1, 2, 3]}").toJson();
Notes
asList()without type parameter returnsList<Object>— use the typed variant when possibleasMap()without type parameters returnsMap<String, Object>- Convenience methods
asDoubleMap(),asStringMap(),asIntMap(),asLongMap()avoid manual type casting toJson()serializes the Python value to JSON for logging or transmissiontoJson(true)enables pretty-printed output