Files
NebulaShell/store/@{FutureOSS}/json-codec
qwen.ai[bot] 97ced1b5e6 Title: Implement minimal core framework with PL injection and update build config
Key features implemented:
- Updated package metadata and dependencies in PKG-INFO, setup files
- Added main.py entry point for backward compatibility with README launch method
- Enhanced CLI with config options, system info command, and proper signal handling
- Implemented minimal PluginManager loading only plugin-loader core plugin
- Refactored PluginLoader to follow minimal core design, removed sandbox/isolation complexity
- Updated auto-dependency plugin with safer PL injection mechanism and disabled pl_injection
- Removed legacy plugin files (firewall, frp_proxy, ftp_server, multi_lang_deploy, ops_toolbox, security_gateway) as functionality moved to core plugin system
- Improved gitignore with comprehensive ignore patterns

The changes implement a minimal core framework design where only the plugin-loader is directly loaded by the core, with all other plugins managed through the PL injection mechanism, significantly simplifying the architecture.
2026-04-25 10:47:26 +00:00
..
2026-04-17 23:15:15 +08:00
2026-04-17 23:15:15 +08:00

json-codec JSON 编解码器

提供插件间 JSON 数据的编码、解码和验证功能。

功能

  • JSON 编码: Python 对象 → JSON 字符串
  • JSON 解码: JSON 字符串 → Python 对象
  • Schema 验证: 验证 JSON 数据结构
  • 自定义类型: 支持注册自定义类型编解码器

基本使用

codec = plugin_mgr.get("json-codec")

# 编码
data = {"name": "test", "count": 42}
json_str = codec.encode(data)
# '{"name": "test", "count": 42}'

# 编码(格式化)
json_pretty = codec.encode(data, pretty=True)
# '{\n  "name": "test",\n  "count": 42\n}'

# 解码
parsed = codec.decode(json_str)
# {"name": "test", "count": 42}

HTTP 响应处理

# 在 http-api 插件中使用
router.get("/api/users", lambda req: Response(
    status=200,
    headers={"Content-Type": "application/json"},
    body=codec.encode({"users": [...]})
))

Schema 验证

# 注册 schema
codec.register_schema("user", {
    "type": "object",
    "required": ["name", "email"],
    "properties": {
        "name": {"type": "string"},
        "email": {"type": "string"},
        "age": {"type": "number"}
    }
})

# 验证数据
user_data = {"name": "test", "email": "test@example.com"}
is_valid = codec.validate(user_data, "user")

自定义类型

from datetime import datetime

# 注册自定义编码器
codec.serializer.register_encoder(datetime, lambda dt: dt.isoformat())

# 使用
data = {"created_at": datetime.now()}
json_str = codec.encode(data)

错误处理

from oss.plugin.types import JsonCodecError

try:
    result = codec.decode("invalid json")
except JsonCodecError as e:
    print(f"解码失败: {e}")