Example: Size comparison between JSON and TOON.
()
| 236 | |
| 237 | |
| 238 | def example_comparison(): |
| 239 | """Example: Size comparison between JSON and TOON.""" |
| 240 | print("=== Size Comparison: JSON vs TOON ===") |
| 241 | |
| 242 | # Create a list of products |
| 243 | products = [ |
| 244 | Product(sku=f'PROD-{i:03d}', name=f'Product {i}', price=float(10 + i), stock=100 - i) |
| 245 | for i in range(1, 11) |
| 246 | ] |
| 247 | |
| 248 | # Convert to JSON |
| 249 | json_data = [p.model_dump() if hasattr(p, 'model_dump') else p.dict() for p in products] |
| 250 | json_str = json.dumps(json_data) |
| 251 | |
| 252 | # Convert to TOON |
| 253 | toon_str = encode_pydantic(products) |
| 254 | |
| 255 | print(f"Number of products: {len(products)}") |
| 256 | print(f"JSON size: {len(json_str)} bytes") |
| 257 | print(f"TOON size: {len(toon_str)} bytes") |
| 258 | print(f"Size reduction: {100 - (len(toon_str) / len(json_str) * 100):.1f}%") |
| 259 | print() |
| 260 | |
| 261 | print("JSON format (first 200 chars):") |
| 262 | print(json_str[:200] + "...") |
| 263 | print() |
| 264 | |
| 265 | print("TOON format:") |
| 266 | print(toon_str) |
| 267 | print() |
| 268 | |
| 269 | |
| 270 | if __name__ == '__main__': |
no test coverage detected
searching dependent graphs…