AS AgentScopeAPI 手册 / API Reference

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
objObjectobj(类型 `Object`)。the object to serialize
返回Returns String
返回字符串结果。JSON string representation
抛出Throws
  • JsonException 可能抛出 `JsonException`。if serialization fails
源码Source io/agentscope/core/util/JsonCodec.java:56

toPrettyJson

public String toPrettyJson(Object obj)

设置该参数。

Serialize an object to pretty-printed JSON string.

参数Parameters
名称Name类型Type说明Description
objObjectobj(类型 `Object`)。the object to serialize
返回Returns String
返回字符串结果。pretty-printed JSON string representation
抛出Throws
  • JsonException 可能抛出 `JsonException`。if serialization fails
源码Source io/agentscope/core/util/JsonCodec.java:65

fromJson

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
jsonStringJSON(类型 `String`)。the JSON string to deserialize
typeClass<T>目标类类型。the target class type
返回Returns T
返回T。deserialized object
抛出Throws
  • JsonException 可能抛出 `JsonException`。if deserialization fails
源码Source io/agentscope/core/util/JsonCodec.java:76

fromJson

public T T fromJson(String json, TypeReference<T> typeRef)

执行来自 JSON相关操作。

Deserialize a JSON string to an object with generic type.

参数Parameters
名称Name类型Type说明Description
jsonStringJSON(类型 `String`)。the JSON string to deserialize
typeRefTypeReference<T>类型引用用于泛型类型。the type reference for generic types
返回Returns T
返回T。deserialized object
抛出Throws
  • JsonException 可能抛出 `JsonException`。if deserialization fails
源码Source io/agentscope/core/util/JsonCodec.java:89

convertValue

public T T convertValue(Object from, Class<T> toType)

转换值。

Convert an object to another type.

参数Parameters
名称Name类型Type说明Description
fromObject来源对象。the source object
toTypeClass<T>目标类类型。the target class type
返回Returns T
返回T。converted object
抛出Throws
源码Source io/agentscope/core/util/JsonCodec.java:102

convertValue

public T T convertValue(Object from, TypeReference<T> toTypeRef)

转换值。

Convert an object to another generic type.

参数Parameters
名称Name类型Type说明Description
fromObject来源对象。the source object
toTypeRefTypeReference<T>类型引用用于泛型类型。the type reference for generic types
返回Returns T
返回T。converted object
抛出Throws
源码Source io/agentscope/core/util/JsonCodec.java:113

convertValue

public Object convertValue(Object from, Type toType)

转换值。

Convert an object to another type using Java reflection Type.

参数Parameters
名称Name类型Type说明Description
fromObject来源对象。the source object
toTypeType到类型(类型 `Type`)。the target type (can be Class, ParameterizedType, etc.)
返回Returns Object
返回对象。converted object
抛出Throws
源码Source io/agentscope/core/util/JsonCodec.java:133
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.