JsonCodec
public interface JsonCodec
源码Source
io/agentscope/core/util/JsonCodec.java:47–134定义 JSON 编解码器契约的接口。位于 io.agentscope.core.util 包。核心成员包括 toJson、toPrettyJson、fromJson、convertValue。
Interface for JSON serialization and deserialization operations.
This interface provides a unified abstraction for JSON processing, allowing users to replace the default Jackson-based implementation with custom implementations if needed.
Usage:
`// Get the codec instance
JsonCodec codec = JsonUtils.getJsonCodec();
// Serialize object to JSON
String json = codec.toJson(myObject);
// Deserialize JSON to object
MyClass obj = codec.fromJson(json, MyClass.class);
// Deserialize with generic type
List<MyClass> list = codec.fromJson(json, new TypeReference<List<MyClass>>() {});
`
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public String toJson(Object obj) | 设置到 JSON。 Serialize an object to JSON string. |
public String toPrettyJson(Object obj) | 设置该参数。 Serialize an object to pretty-printed JSON string. |
public T T fromJson(String json, Class<T> type) | 执行来自 JSON相关操作。 Deserialize a JSON string to an object of the specified type. |
public T T fromJson(String json, TypeReference<T> typeRef) | 执行来自 JSON相关操作。 Deserialize a JSON string to an object with generic type. |
public T T convertValue(Object from, Class<T> toType) | 转换值。 Convert an object to another type. |
public T T convertValue(Object from, TypeReference<T> toTypeRef) | 转换值。 Convert an object to another generic type. |
public Object convertValue(Object from, Type toType) | 转换值。 Convert an object to another type using Java reflection Type. |
方法详情Method Detail
toJson
public String toJson(Object obj)
设置到 JSON。
Serialize an object to JSON string.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
obj | Object | obj(类型 `Object`)。the object to serialize |
String返回字符串结果。JSON string representation
JsonException可能抛出 `JsonException`。if serialization fails
源码Source
io/agentscope/core/util/JsonCodec.java:56toPrettyJson
public String toPrettyJson(Object obj)
设置该参数。
Serialize an object to pretty-printed JSON string.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
obj | Object | obj(类型 `Object`)。the object to serialize |
String返回字符串结果。pretty-printed JSON string representation
JsonException可能抛出 `JsonException`。if serialization fails
源码Source
io/agentscope/core/util/JsonCodec.java:65fromJson
public T T fromJson(String json, Class<T> type)
执行来自 JSON相关操作。
Deserialize a JSON string to an object of the specified type.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
json | String | JSON(类型 `String`)。the JSON string to deserialize |
type | Class<T> | 目标类类型。the target class type |
T返回T。deserialized object
JsonException可能抛出 `JsonException`。if deserialization fails
源码Source
io/agentscope/core/util/JsonCodec.java:76fromJson
public T T fromJson(String json, TypeReference<T> typeRef)
执行来自 JSON相关操作。
Deserialize a JSON string to an object with generic type.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
json | String | JSON(类型 `String`)。the JSON string to deserialize |
typeRef | TypeReference<T> | 类型引用用于泛型类型。the type reference for generic types |
T返回T。deserialized object
JsonException可能抛出 `JsonException`。if deserialization fails
源码Source
io/agentscope/core/util/JsonCodec.java:89convertValue
public T T convertValue(Object from, Class<T> toType)
转换值。
Convert an object to another type.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
from | Object | 来源对象。the source object |
toType | Class<T> | 目标类类型。the target class type |
T返回T。converted object
JsonException可能抛出 `JsonException`。if conversion fails
源码Source
io/agentscope/core/util/JsonCodec.java:102convertValue
public T T convertValue(Object from, TypeReference<T> toTypeRef)
转换值。
Convert an object to another generic type.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
from | Object | 来源对象。the source object |
toTypeRef | TypeReference<T> | 类型引用用于泛型类型。the type reference for generic types |
T返回T。converted object
JsonException可能抛出 `JsonException`。if conversion fails
源码Source
io/agentscope/core/util/JsonCodec.java:113convertValue
public Object convertValue(Object from, Type toType)
转换值。
Convert an object to another type using Java reflection Type.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
from | Object | 来源对象。the source object |
toType | Type | 到类型(类型 `Type`)。the target type (can be Class, ParameterizedType, etc.) |
Object返回对象。converted object
JsonException可能抛出 `JsonException`。if conversion fails
源码Source
io/agentscope/core/util/JsonCodec.java:133