中文 | English
Serialization
Wingman supports multiple data serialization formats for configuration files, data exchange, persistence, and other scenarios.
Supported Formats
| Format | Module | Strengths | Typical use cases |
|---|---|---|---|
| JSON | json | Lightweight, readable, widely supported | API data exchange, configuration files |
| YAML | (planned) | More readable, supports comments | Complex configuration, human-edited files |
| INI | ini | Simple, good compatibility, supports escapes and merging | Simple configuration, legacy application configuration |
Format Comparison
JSON
Pros:
- Standardized format supported by virtually every language
- Fast parsing
- Well suited to machine processing and API interaction
- Rich type system (strings, numbers, booleans, arrays, objects)
Cons:
- No support for comments
- Verbose syntax (quotes, commas)
- Not suitable for hand-editing large configurations
Example:
python
from wingman import json
# 解析 JSON
config_str = '{"host": "localhost", "port": 8080, "debug": true}'
config = json.decode(config_str)
# 编码 JSON(模块函数只接受位置参数)
output = json.encode(config, 2)YAML
Pros:
- More readable, well suited to hand editing
- Supports comments
- More concise syntax
- Supports anchors and aliases (reuse)
Cons:
- Slower parsing
- More complex format (indentation-sensitive)
- Different implementations may have compatibility issues
Example (planned):
python
from wingman import yaml
# 解析 YAML
config_str = '''
# 服务器配置
host: localhost
port: 8080
debug: true
# 数据库配置
database:
host: db.example.com
port: 5432
'''
config = yaml.decode(config_str)
# 编码 YAML
output = yaml.encode(config)INI
Pros:
- Extremely simple
- Broad compatibility (the traditional format for Windows applications)
- Suited to flat configuration
Cons:
- No support for nested structures
- Limited types (mostly strings)
- Not suitable for complex data
Example:
python
from wingman import ini
# 解析 INI
config_str = '''
[Server]
host=localhost
port=8080
[Database]
host=db.example.com
port=5432
'''
config = ini.decode(config_str)
# 编码 INI
output = ini.encode(config)
# 读取配置
host = ini.get(config, "Server", "host")
port = ini.get(config, "Server", "port")
# 修改配置(返回新数据对象,须接收)
config = ini.set(config, "Server", "host", "192.168.1.1")
# 保存配置
with open("config.ini", "w") as f:
f.write(ini.encode(config))lua
local wingman = require("wingman")
-- 解析 INI
local config_str = [[
[Server]
host=localhost
port=8080
]]
local config = wingman.ini.decode(config_str)
-- 读取配置
local host = wingman.ini.get(config, "Server", "host")
local port = wingman.ini.get(config, "Server", "port")
-- 修改配置(返回新数据对象,须接收)
config = wingman.ini.set(config, "Server", "host", "192.168.1.1")Selection Guide
Use JSON
Suitable scenarios:
- API requests/responses
- Data exchange with external systems
- Configuration files that require a standard format
- Configuration generated by automation tooling
Example:
json
{
"api_endpoint": "https://api.example.com",
"timeout": 30000,
"retry_count": 3
}Use YAML (planned)
Suitable scenarios:
- Complex configuration that needs hand editing
- Configuration files that need explanatory comments
- Deeply nested configuration
Example:
yaml
# 服务器配置
server:
host: localhost
port: 8080
# 开发模式启用
debug: true
# 日志配置
logging:
level: info # 日志级别
file: app.log # 日志文件Use INI
Suitable scenarios:
- Simple application configuration
- Compatibility with traditional Windows applications
- Flat key-value configuration
Example:
ini
[Server]
host=localhost
port=8080
[Logging]
level=info
file=app.logModule Details
- json module documentation - Full JSON API
- ini module documentation - Full INI API
- yaml module (planned)
Format Conversion
Converting data between formats:
python
from wingman import json
# JSON → YAML(需要 yaml 模块)
json_data = json.decode('{"key": "value"}')
# yaml_str = yaml.encode(json_data)
# YAML → JSON(需要 yaml 模块)
# yaml_data = yaml.decode(yaml_str)
# json_str = json.encode(yaml_data)Best Practices
Naming Conventions
- JSON files:
.jsonextension - YAML files:
.yamlor.ymlextension - INI files:
.iniextension
Coding Style
JSON:
- Use 2 or 4 space indentation
- Use snake_case or camelCase key names consistently
YAML:
- Use 2-space indentation (the YAML standard)
- Keep indentation consistent
INI:
- Organize configuration with
[Section] - Separate key-value pairs with
=
Performance Considerations
- JSON parses fastest; prefer it for high-performance scenarios
- YAML suits configuration loading, not high-frequency data exchange
- Use INI only for simple scenarios; JSON/YAML is recommended for complex configuration
Caveats
Security
- Guard against injection attacks when parsing untrusted data
- The JSON parser does not execute code by default, which is relatively safe
- Some YAML features may carry security risks (once enabled)
Encoding
- JSON/YAML use UTF-8 encoding by default
- INI file encoding may depend on the system; specifying UTF-8 explicitly is recommended
Compatibility
- JSON is a standard format with no cross-platform issues
- Different YAML parsers may differ in subtle ways
- INI formats are not standardized; stick to simple syntax
Large file handling
- Streaming parsing is recommended for large JSON/YAML files (future support)
- Avoid loading very large files into memory all at once