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