Skip to content

Commit 7dc508f

Browse files
committed
Merge branch 'feature/pydantic-to-toon'
2 parents 2b813d1 + 7e9c3db commit 7dc508f

File tree

9 files changed

+1154
-3
lines changed

9 files changed

+1154
-3
lines changed

.gitignore

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,56 @@ __pycache__
88
*.pyz
99
*.pywz
1010
*.pyzw
11-
*.pyzwz
11+
*.pyzwz
12+
13+
# Python
14+
__pycache__/
15+
*.py[cod]
16+
*$py.class
17+
*.so
18+
.Python
19+
build/
20+
develop-eggs/
21+
dist/
22+
downloads/
23+
eggs/
24+
.eggs/
25+
lib/
26+
lib64/
27+
parts/
28+
sdist/
29+
var/
30+
wheels/
31+
*.egg-info/
32+
.installed.cfg
33+
*.egg
34+
35+
# Testing
36+
.coverage
37+
.pytest_cache
38+
.tox
39+
htmlcov/
40+
41+
# Virtual environments
42+
.venv
43+
venv/
44+
ENV/
45+
env/
46+
47+
# IDEs
48+
.vscode
49+
.idea
50+
*.swp
51+
*.swo
52+
*~
53+
54+
# OS
55+
.DS_Store
56+
Thumbs.db
57+
58+
# Environment variables
59+
.env
60+
.env.local
61+
.env.development.local
62+
.env.test.local
63+
.env.production.local

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ For development:
4040
pip install toonify[dev]
4141
```
4242

43+
With Pydantic support:
44+
```bash
45+
pip install toonify[pydantic]
46+
```
47+
4348
## Quick Start
4449

4550
### Python API
@@ -83,6 +88,48 @@ cat data.json | toon -e > data.toon
8388
toon data.json --stats
8489
```
8590

91+
### Pydantic Integration
92+
93+
TOON supports direct conversion from Pydantic models:
94+
95+
```python
96+
from pydantic import BaseModel
97+
from toon import encode_pydantic, decode_to_pydantic
98+
99+
# Define Pydantic models
100+
class User(BaseModel):
101+
id: int
102+
name: str
103+
email: str
104+
105+
# Encode Pydantic models to TOON
106+
users = [
107+
User(id=1, name='Alice', email='[email protected]'),
108+
User(id=2, name='Bob', email='[email protected]')
109+
]
110+
111+
toon = encode_pydantic(users)
112+
print(toon)
113+
# Output:
114+
# [2]{id,name,email}:
115+
116+
117+
118+
# Decode TOON back to Pydantic models
119+
decoded_users = decode_to_pydantic(toon, User)
120+
assert all(isinstance(u, User) for u in decoded_users)
121+
```
122+
123+
**Features:**
124+
- ✅ Direct conversion from Pydantic models (v1 and v2)
125+
- ✅ Support for nested models
126+
- ✅ Exclude unset, None, or default values
127+
- ✅ Field aliases support
128+
- ✅ Full validation on decode
129+
- ✅ Round-trip conversion
130+
131+
See [examples/pydantic_usage.py](examples/pydantic_usage.py) for more examples.
132+
86133
## TOON Format Specification
87134

88135
### Basic Syntax
@@ -184,6 +231,57 @@ data = decode(toon_string, {
184231
})
185232
```
186233

234+
### `encode_pydantic(model, options=None, exclude_unset=False, exclude_none=False, exclude_defaults=False, by_alias=False)`
235+
236+
Convert Pydantic model(s) to TOON string.
237+
238+
**Parameters:**
239+
- `model`: Pydantic model instance or list of model instances
240+
- `options`: Same as `encode()` function
241+
- `exclude_unset`: If True, exclude fields that were not explicitly set
242+
- `exclude_none`: If True, exclude fields with None values
243+
- `exclude_defaults`: If True, exclude fields with default values
244+
- `by_alias`: If True, use field aliases instead of field names
245+
246+
**Example:**
247+
```python
248+
from pydantic import BaseModel
249+
from toon import encode_pydantic
250+
251+
class User(BaseModel):
252+
id: int
253+
name: str
254+
email: str | None = None
255+
256+
user = User(id=1, name='Alice')
257+
toon = encode_pydantic(user, exclude_none=True)
258+
```
259+
260+
### `decode_to_pydantic(toon_string, model_class, options=None)`
261+
262+
Decode TOON string to Pydantic model(s).
263+
264+
**Parameters:**
265+
- `toon_string`: TOON formatted string
266+
- `model_class`: Pydantic model class to instantiate
267+
- `options`: Same as `decode()` function
268+
269+
**Returns:**
270+
- Pydantic model instance or list of instances (depending on input)
271+
272+
**Example:**
273+
```python
274+
from pydantic import BaseModel
275+
from toon import decode_to_pydantic
276+
277+
class User(BaseModel):
278+
id: int
279+
name: str
280+
281+
toon = "id: 1\nname: Alice"
282+
user = decode_to_pydantic(toon, User)
283+
```
284+
187285
## CLI Usage
188286

189287
```

assets/README.ko.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ pip install toonify
4040
pip install toonify[dev]
4141
```
4242

43+
Pydantic 지원:
44+
```bash
45+
pip install toonify[pydantic]
46+
```
47+
4348
## 빠른 시작
4449

4550
### Python API
@@ -83,6 +88,48 @@ cat data.json | toon -e > data.toon
8388
toon data.json --stats
8489
```
8590

91+
### Pydantic 통합
92+
93+
TOON은 Pydantic 모델에서 직접 변환을 지원합니다:
94+
95+
```python
96+
from pydantic import BaseModel
97+
from toon import encode_pydantic, decode_to_pydantic
98+
99+
# Pydantic 모델 정의
100+
class User(BaseModel):
101+
id: int
102+
name: str
103+
email: str
104+
105+
# Pydantic 모델을 TOON으로 인코딩
106+
users = [
107+
User(id=1, name='Alice', email='[email protected]'),
108+
User(id=2, name='Bob', email='[email protected]')
109+
]
110+
111+
toon = encode_pydantic(users)
112+
print(toon)
113+
# 출력:
114+
# [2]{id,name,email}:
115+
116+
117+
118+
# TOON을 다시 Pydantic 모델로 디코딩
119+
decoded_users = decode_to_pydantic(toon, User)
120+
assert all(isinstance(u, User) for u in decoded_users)
121+
```
122+
123+
**기능:**
124+
- ✅ Pydantic 모델에서 직접 변환 (v1 및 v2)
125+
- ✅ 중첩된 모델 지원
126+
- ✅ 설정되지 않은 값, None 또는 기본값 제외
127+
- ✅ 필드 별칭 지원
128+
- ✅ 디코딩 시 전체 검증
129+
- ✅ 왕복 변환
130+
131+
자세한 예제는 [examples/pydantic_usage.py](../examples/pydantic_usage.py)를 참조하세요.
132+
86133
## TOON 형식 사양
87134

88135
### 기본 구문
@@ -184,6 +231,57 @@ data = decode(toon_string, {
184231
})
185232
```
186233

234+
### `encode_pydantic(model, options=None, exclude_unset=False, exclude_none=False, exclude_defaults=False, by_alias=False)`
235+
236+
Pydantic 모델을 TOON 문자열로 변환합니다.
237+
238+
**매개변수:**
239+
- `model`: Pydantic 모델 인스턴스 또는 모델 인스턴스 리스트
240+
- `options`: `encode()` 함수와 동일
241+
- `exclude_unset`: True인 경우 명시적으로 설정되지 않은 필드 제외
242+
- `exclude_none`: True인 경우 None 값을 가진 필드 제외
243+
- `exclude_defaults`: True인 경우 기본값을 가진 필드 제외
244+
- `by_alias`: True인 경우 필드 이름 대신 필드 별칭 사용
245+
246+
**예제:**
247+
```python
248+
from pydantic import BaseModel
249+
from toon import encode_pydantic
250+
251+
class User(BaseModel):
252+
id: int
253+
name: str
254+
email: str | None = None
255+
256+
user = User(id=1, name='Alice')
257+
toon = encode_pydantic(user, exclude_none=True)
258+
```
259+
260+
### `decode_to_pydantic(toon_string, model_class, options=None)`
261+
262+
TOON 문자열을 Pydantic 모델로 디코딩합니다.
263+
264+
**매개변수:**
265+
- `toon_string`: TOON 형식 문자열
266+
- `model_class`: 인스턴스화할 Pydantic 모델 클래스
267+
- `options`: `decode()` 함수와 동일
268+
269+
**반환값:**
270+
- Pydantic 모델 인스턴스 또는 인스턴스 리스트 (입력에 따라 다름)
271+
272+
**예제:**
273+
```python
274+
from pydantic import BaseModel
275+
from toon import decode_to_pydantic
276+
277+
class User(BaseModel):
278+
id: int
279+
name: str
280+
281+
toon = "id: 1\nname: Alice"
282+
user = decode_to_pydantic(toon, User)
283+
```
284+
187285
## CLI 사용법
188286

189287
```

0 commit comments

Comments
 (0)