@@ -40,6 +40,11 @@ pip install toonify
4040pip 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
8388toon 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 = ' alice@example.com' ),
108+ User(id = 2 , name = ' Bob' , email = ' bob@example.com' )
109+ ]
110+
111+ toon = encode_pydantic(users)
112+ print (toon)
113+ # 출력:
114+ # [2]{id,name,email}:
115+ # 1,Alice,alice@example.com
116+ # 2,Bob,bob@example.com
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\n name: Alice"
282+ user = decode_to_pydantic(toon, User)
283+ ```
284+
187285## CLI 사용법
188286
189287```
0 commit comments