Documentation
¶
Overview ¶
Package utils provides utility functions for the agno framework utils 包为 agno 框架提供工具函数
JSON Serialization / JSON 序列化 ¶
This package provides custom JSON serialization utilities to handle types that are not serializable by default, particularly in WebSocket and API contexts.
此包提供自定义 JSON 序列化工具,以处理默认情况下不可序列化的类型, 特别是在 WebSocket 和 API 上下文中。
Supported Types / 支持的类型 ¶
time.Time and *time.Time -> ISO 8601 format (RFC3339)
fmt.Stringer interface -> String() method output
Nested maps and slices (recursive conversion)
Primitive types (string, int, float, bool, nil)
time.Time 和 *time.Time -> ISO 8601 格式 (RFC3339)
fmt.Stringer 接口 -> String() 方法输出
嵌套的 map 和 slice(递归转换)
基本类型(string、int、float、bool、nil)
Example Usage / 使用示例 ¶
// Simple serialization / 简单序列化
data := map[string]interface{}{
"timestamp": time.Now(),
"status": "active",
}
jsonStr, err := ToJSONString(data)
// Complex nested structures / 复杂嵌套结构
workflow := map[string]interface{}{
"id": "workflow-123",
"steps": []interface{}{
map[string]interface{}{
"name": "step-1",
"createdAt": time.Now(),
},
},
}
jsonBytes, err := ToJSON(workflow)
// Panic on error (for critical paths) / 出错时 panic(用于关键路径)
jsonStr := MustToJSONString(data)
Performance / 性能 ¶
Benchmark results on Apple M3:
- ToJSON: ~600ns/op, 760B/op, 15 allocs/op
- ConvertValue: ~180ns/op, 392B/op, 5 allocs/op
Apple M3 上的基准测试结果:
- ToJSON: ~600ns/op,760B/op,15 次内存分配
- ConvertValue: ~180ns/op,392B/op,5 次内存分配
Index ¶
- func JSONPretty(v interface{}) string
- func MarshalJSON(v interface{}) ([]byte, error)
- func MustToJSONString(v interface{}) string
- func Retry(attempts int, delay time.Duration, fn func() error) error
- func ToJSON(v interface{}) ([]byte, error)
- func ToJSONString(v interface{}) (string, error)
- type JSONSerializer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSONPretty ¶
func JSONPretty(v interface{}) string
JSONPretty returns pretty-printed JSON or empty string on error
func MarshalJSON ¶
MarshalJSON provides custom JSON serialization for objects not serializable by default MarshalJSON 为默认情况下不可序列化的对象提供自定义 JSON 序列化
Handles: - time.Time objects -> ISO 8601 format strings - Custom types implementing Stringer interface -> string representation
处理: - time.Time 对象 -> ISO 8601 格式字符串 - 实现了 Stringer 接口的自定义类型 -> 字符串表示
func MustToJSONString ¶
func MustToJSONString(v interface{}) string
MustToJSONString converts any value to JSON string, panics on error.
This function is useful in critical code paths where serialization failure indicates a programming error rather than a runtime condition.
Panics if the value cannot be serialized (e.g., contains channels or functions).
Example:
// Use when you're certain the data is serializable
// 当你确定数据可序列化时使用
jsonStr := MustToJSONString(map[string]string{
"status": "active",
})
MustToJSONString 将任意值转换为 JSON 字符串,出错时 panic。
此函数在关键代码路径中很有用,其中序列化失败表示编程错误而不是运行时条件。
如果值无法序列化(例如包含 channel 或函数),则 panic。
func ToJSON ¶
ToJSON converts any value to JSON bytes with custom serialization.
This function recursively processes the input value, converting special types (time.Time, fmt.Stringer) to JSON-serializable equivalents before marshaling.
Returns an error if the value contains types that cannot be serialized (e.g., channels, functions).
Example:
data := map[string]interface{}{
"timestamp": time.Now(),
"count": 42,
}
jsonBytes, err := ToJSON(data)
if err != nil {
log.Fatal(err)
}
ToJSON 使用自定义序列化将任意值转换为 JSON 字节。
此函数递归处理输入值,在 marshal 之前将特殊类型(time.Time、fmt.Stringer) 转换为 JSON 可序列化的等价物。
如果值包含无法序列化的类型(例如 channel、函数),则返回错误。
Example ¶
ExampleToJSON demonstrates basic usage of ToJSON ExampleToJSON 演示 ToJSON 的基本用法
testTime := time.Date(2025, 10, 4, 12, 30, 45, 0, time.UTC)
data := map[string]interface{}{
"event": "user_login",
"timestamp": testTime,
"user_id": "12345",
}
jsonBytes, err := ToJSON(data)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println(string(jsonBytes))
Output: {"event":"user_login","timestamp":"2025-10-04T12:30:45Z","user_id":"12345"}
func ToJSONString ¶
ToJSONString converts any value to JSON string with custom serialization.
This is a convenience wrapper around ToJSON that returns a string instead of bytes. Returns an error if the value cannot be serialized.
Example:
jsonStr, err := ToJSONString(map[string]interface{}{
"name": "agent-1",
"createdAt": time.Now(),
})
ToJSONString 使用自定义序列化将任意值转换为 JSON 字符串。
这是 ToJSON 的便捷包装器,返回字符串而不是字节。 如果值无法序列化,则返回错误。
Example ¶
ExampleToJSONString demonstrates ToJSONString usage ExampleToJSONString 演示 ToJSONString 的用法
testTime := time.Date(2025, 10, 4, 12, 30, 45, 0, time.UTC)
data := map[string]interface{}{
"timestamp": testTime,
}
jsonStr, err := ToJSONString(data)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println(jsonStr)
Output: {"timestamp":"2025-10-04T12:30:45Z"}
Types ¶
type JSONSerializer ¶
type JSONSerializer struct{}
JSONSerializer is a custom JSON serializer for handling special types JSONSerializer 是一个自定义 JSON 序列化器,用于处理特殊类型