Example: Decoding TOON back to Pydantic models.
()
| 175 | |
| 176 | |
| 177 | def example_decoding(): |
| 178 | """Example: Decoding TOON back to Pydantic models.""" |
| 179 | print("=== Decoding TOON to Pydantic ===") |
| 180 | |
| 181 | # TOON string representing a list of users |
| 182 | toon = """[3]{id,name,email,age,active}: |
| 183 | 1,Alice Smith,alice@example.com,30,true |
| 184 | 2,Bob Johnson,bob@example.com,35,true |
| 185 | 3,Charlie Brown,charlie@example.com,28,false""" |
| 186 | |
| 187 | print("TOON input:") |
| 188 | print(toon) |
| 189 | print() |
| 190 | |
| 191 | # Decode to list of User objects |
| 192 | users = decode_to_pydantic(toon, User) |
| 193 | |
| 194 | print("Decoded Pydantic models:") |
| 195 | for user in users: |
| 196 | print(f" User(id={user.id}, name='{user.name}', age={user.age}, active={user.active})") |
| 197 | print() |
| 198 | |
| 199 | |
| 200 | def example_roundtrip(): |
no test coverage detected
searching dependent graphs…