JsonUtils
public final class JsonUtils
io/agentscope/core/util/JsonUtils.java:49–152JSON Utils,为框架提供通用类型与基础能力。位于 io.agentscope.core.util 包。核心成员包括 getJsonCodec、setJsonCodec、resetToDefault、isValidJsonObject、resolveToolCallArgsJson。
Utility class for accessing the global JsonCodec instance.
This class provides a centralized way to access JSON serialization/deserialization
functionality throughout the framework. By default, it uses JacksonJsonCodec,
but users can replace it with a custom implementation.
Usage:
`// Basic usage
JsonUtils.getJsonCodec().toJson(obj);
JsonUtils.getJsonCodec().fromJson(json, MyClass.class);
// Cache reference for frequent calls
JsonCodec codec = JsonUtils.getJsonCodec();
codec.toJson(obj1);
codec.toJson(obj2);
// Replace with custom implementation
JsonUtils.setJsonCodec(new MyCustomJsonCodec());
`
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public static JsonCodec getJsonCodec()static | 获取JSON 编解码器。 Get the global JsonCodec instance. |
public static void setJsonCodec(JsonCodec jsonCodec)static | 设置JSON 编解码器。 Set a custom JsonCodec implementation. |
public static void resetToDefault()static | 重置到默认。 Reset to the default JacksonJsonCodec implementation. |
public static boolean isValidJsonObject(String str)static | 判断是否有效 JSON 对象。 Check whether the given string is a valid JSON object (i.e. |
public static String resolveToolCallArgsJson(ToolUseBlock toolUse)static | 解析工具调用参数 JSON。 Resolve the arguments JSON string from a `ToolUseBlock`, ensuring the
result is always a valid JSON object. |
方法详情Method Detail
getJsonCodec
public static JsonCodec getJsonCodec()
获取JSON 编解码器。
Get the global JsonCodec instance.
JsonCodecio/agentscope/core/util/JsonUtils.java:64–66setJsonCodec
public static void setJsonCodec(JsonCodec jsonCodec)
设置JSON 编解码器。
Set a custom JsonCodec implementation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
jsonCodec | JsonCodec | JSON 编解码器(类型 `JsonCodec`)。the custom JsonCodec implementation |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if jsonCodec is null
io/agentscope/core/util/JsonUtils.java:77–82resetToDefault
public static void resetToDefault()
重置到默认。
Reset to the default JacksonJsonCodec implementation.
io/agentscope/core/util/JsonUtils.java:89–91isValidJsonObject
public static boolean isValidJsonObject(String str)
判断是否有效 JSON 对象。
Check whether the given string is a valid JSON object (i.e.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
str | String | str(类型 `String`)。the string to validate |
booleanio/agentscope/core/util/JsonUtils.java:103–114resolveToolCallArgsJson
public static String resolveToolCallArgsJson(ToolUseBlock toolUse)
解析工具调用参数 JSON。
Resolve the arguments JSON string from a ToolUseBlock, ensuring the result is always a valid JSON object.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolUse | ToolUseBlock | 工具使用块。the tool use block |
Stringio/agentscope/core/util/JsonUtils.java:133–151