|
| 1 | +# ZON Format Examples |
| 2 | + |
| 3 | +This document provides detailed examples of ZON v8.0 encoding across different data types and structures. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Basic Examples](#basic-examples) |
| 8 | +- [Format Comparison](#format-comparison) |
| 9 | +- [Symbol Reference](#symbol-reference) |
| 10 | +- [Advanced Examples](#advanced-examples) |
| 11 | + |
| 12 | +## Basic Examples |
| 13 | + |
| 14 | +### Simple List |
| 15 | + |
| 16 | +**JSON**: |
| 17 | +```json |
| 18 | +[ |
| 19 | + {"id": 1, "name": "Alice"}, |
| 20 | + {"id": 2, "name": "Bob"} |
| 21 | +] |
| 22 | +``` |
| 23 | + |
| 24 | +**ZON**: |
| 25 | +``` |
| 26 | +@data(2):id,name |
| 27 | +1,Alice |
| 28 | +_,Bob |
| 29 | +``` |
| 30 | + |
| 31 | +- `@data(2)` → table with 2 rows |
| 32 | +- `_` → auto-increment (2 = 1 + 1) |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +### Nested Object |
| 37 | + |
| 38 | +**JSON**: |
| 39 | +```json |
| 40 | +{ |
| 41 | + "config": { |
| 42 | + "host": "localhost", |
| 43 | + "port": 5432 |
| 44 | + }, |
| 45 | + "users": [ |
| 46 | + {"id": 1, "name": "test"} |
| 47 | + ] |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +**ZON**: |
| 52 | +``` |
| 53 | +config.host:localhost |
| 54 | +config.port:5432 |
| 55 | +
|
| 56 | +@users(1):id,name |
| 57 | +1,test |
| 58 | +``` |
| 59 | + |
| 60 | +- Dot notation flattens nesting |
| 61 | +- Metadata separated from table with blank line |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### Arrays in Metadata |
| 66 | + |
| 67 | +**JSON**: |
| 68 | +```json |
| 69 | +{ |
| 70 | + "tags": ["python", "data", "format"], |
| 71 | + "version": "1.0" |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +**ZON**: |
| 76 | +``` |
| 77 | +tags:[python,data,format] |
| 78 | +version:1.0 |
| 79 | +``` |
| 80 | + |
| 81 | +- No quotes in arrays unless needed |
| 82 | +- Compact `[item,item,item]` syntax |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Format Comparison |
| 87 | + |
| 88 | +### Example: User Records |
| 89 | + |
| 90 | +**JSON (201 bytes)**: |
| 91 | +```json |
| 92 | +[ |
| 93 | + {"id":1,"name":"Alice","age":30,"active":true,"city":"NYC"}, |
| 94 | + {"id":2,"name":"Bob","age":25,"active":false,"city":"LA"}, |
| 95 | + {"id":3,"name":"Charlie","age":35,"active":true,"city":"NYC"} |
| 96 | +] |
| 97 | +``` |
| 98 | + |
| 99 | +**TOON (formatted, ~180 bytes)**: |
| 100 | +``` |
| 101 | +users[3]{id,name,age,active,city} |
| 102 | +1,Alice,30,true,"NYC" |
| 103 | +2,Bob,25,false,"LA" |
| 104 | +3,Charlie,35,true,"NYC" |
| 105 | +``` |
| 106 | + |
| 107 | +**ZON (106 bytes - 47% smaller than JSON)**: |
| 108 | +``` |
| 109 | +@data(3):active,age,city,id,name |
| 110 | +T,30,NYC,1,Alice |
| 111 | +F,25,LA,_,Bob |
| 112 | +T,35,^,_,Charlie |
| 113 | +``` |
| 114 | + |
| 115 | +**Why ZON wins**: |
| 116 | +- ✅ No quotes on simple strings |
| 117 | +- ✅ `T`/`F` instead of `true`/`false` (3 bytes saved per boolean) |
| 118 | +- ✅ `_` for auto-increment IDs |
| 119 | +- ✅ `^` for repeated values (NYC) |
| 120 | +- ✅ No spaces after delimiters |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Symbol Reference |
| 125 | + |
| 126 | +### Metadata Symbols |
| 127 | + |
| 128 | +| Symbol | Meaning | Example | |
| 129 | +|--------|---------|---------| |
| 130 | +| `:` | Key-value separator | `name:Alice` | |
| 131 | +| `.` | Nested object delimiter | `user.name:Alice` | |
| 132 | +| `[]` | Array | `tags:[a,b,c]` | |
| 133 | +| `,` | Array/table delimiter | No quotes around spaces | |
| 134 | + |
| 135 | +### Table Symbols |
| 136 | + |
| 137 | +| Symbol | Meaning | Example | |
| 138 | +|--------|---------|---------| |
| 139 | +| `@` | Table marker | `@users(10):id,name` | |
| 140 | +| `()` | Row count | `@data(5)` | |
| 141 | +| `:` | Header separator | `@table:col1,col2` | |
| 142 | + |
| 143 | +### Compression Tokens |
| 144 | + |
| 145 | +| Token | Meaning | Example | Output | |
| 146 | +|-------|---------|---------|--------| |
| 147 | +| `_` | Auto-increment | `1,_,_` | `1,2,3` | |
| 148 | +| `^` | Repeat previous | `red,^,blue` | `red,red,blue` | |
| 149 | +| `T` | Boolean true | `T` | `true` | |
| 150 | +| `F` | Boolean false | `F` | `false` | |
| 151 | +| `null` | Null value | `null` | `null` | |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +## Advanced Examples |
| 156 | + |
| 157 | +### Complex Nested Data |
| 158 | + |
| 159 | +**Input**: |
| 160 | +```json |
| 161 | +{ |
| 162 | + "company": "Acme Inc", |
| 163 | + "employees": [ |
| 164 | + { |
| 165 | + "id": 1, |
| 166 | + "name": "John Doe", |
| 167 | + "department": "Engineering", |
| 168 | + "skills": ["Python", "Go"], |
| 169 | + "active": true |
| 170 | + }, |
| 171 | + { |
| 172 | + "id": 2, |
| 173 | + "name": "Jane Smith", |
| 174 | + "department": "Engineering", |
| 175 | + "skills": ["Java", "Kotlin"], |
| 176 | + "active": true |
| 177 | + } |
| 178 | + ] |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +**ZON Output**: |
| 183 | +``` |
| 184 | +company:Acme Inc |
| 185 | +
|
| 186 | +@employees(2):active,department,id,name,skills |
| 187 | +T,Engineering,1,John Doe,[Python,Go] |
| 188 | +^,^,_,Jane Smith,[Java,Kotlin] |
| 189 | +``` |
| 190 | + |
| 191 | +**Symbols explained**: |
| 192 | +- `company:Acme Inc` → metadata (no quotes needed) |
| 193 | +- `@employees(2)` → The table name and row count |
| 194 | +- `T` → Boolean true |
| 195 | +- `^` → Repeat "Engineering" and "T" |
| 196 | +- `_` → Auto-increment ID (2 = 1 + 1) |
| 197 | +- `[Python,Go]` → Inline array (no quotes) |
| 198 | + |
| 199 | +--- |
| 200 | + |
| 201 | +### Repetitive Data |
| 202 | + |
| 203 | +**Input**: Weather data with repeated locations |
| 204 | + |
| 205 | +```json |
| 206 | +[ |
| 207 | + {"date": "2024-01-01", "city": "NYC", "temp": 32}, |
| 208 | + {"date": "2024-01-02", "city": "NYC", "temp": 35}, |
| 209 | + {"date": "2024-01-03", "city": "NYC", "temp": 30}, |
| 210 | + {"date": "2024-01-04", "city": "LA", "temp": 68} |
| 211 | +] |
| 212 | +``` |
| 213 | + |
| 214 | +**ZON**: |
| 215 | +``` |
| 216 | +@data(4):city,date,temp |
| 217 | +NYC,2024-01-01,32 |
| 218 | +^,2024-01-02,35 |
| 219 | +^,2024-01-03,30 |
| 220 | +LA,2024-01-04,68 |
| 221 | +``` |
| 222 | + |
| 223 | +- `^` compresses 3 repeated "NYC" values |
| 224 | +- Saves ~12 bytes on city names alone |
| 225 | + |
| 226 | +--- |
| 227 | + |
| 228 | +### Mixed Data Types |
| 229 | + |
| 230 | +**Input**: |
| 231 | +```json |
| 232 | +{ |
| 233 | + "user": "alice", |
| 234 | + "score": 95.5, |
| 235 | + "verified": true, |
| 236 | + "tags": ["premium", "early-adopter"], |
| 237 | + "metadata": null |
| 238 | +} |
| 239 | +``` |
| 240 | + |
| 241 | +**ZON**: |
| 242 | +``` |
| 243 | +metadata:null |
| 244 | +score:95.5 |
| 245 | +tags:[premium,early-adopter] |
| 246 | +user:alice |
| 247 | +verified:T |
| 248 | +``` |
| 249 | + |
| 250 | +- All types preserved correctly |
| 251 | +- Alphabetical sorting for consistency |
| 252 | +- No unnecessary quotes |
| 253 | + |
| 254 | +--- |
| 255 | + |
| 256 | +## When to Quote Strings |
| 257 | + |
| 258 | +ZON uses **minimal quoting** - quotes are only added when the value contains: |
| 259 | +- Comma `,` (the delimiter) |
| 260 | +- Newline, tab, or control characters |
| 261 | +- Brackets `[]` that could be confused with arrays |
| 262 | +- Quote character `"` |
| 263 | + |
| 264 | +### Examples: |
| 265 | + |
| 266 | +| Value | ZON Output | Quoted? | Reason | |
| 267 | +|-------|------------|---------|--------| |
| 268 | +| `Alice` | `Alice` | No | Simple alphanumeric | |
| 269 | +| `New York` | `New York` | No | **Spaces are fine!** | |
| 270 | +| `Hello: World` | `Hello: World` | No | **Colons are fine!** | |
| 271 | +| `data,value` | `"data,value"` | Yes | Contains comma | |
| 272 | +| `[test]` | `"[test]"` | Yes | Contains brackets | |
| 273 | +| `say "hi"` | `"say \"hi\""` | Yes | Contains quotes | |
| 274 | + |
| 275 | +This is a major improvement over v7.0 which quoted anything with spaces or colons! |
| 276 | + |
| 277 | +--- |
| 278 | + |
| 279 | +## Performance Tips |
| 280 | + |
| 281 | +1. **Use sequential IDs**: Let `_` token handle auto-increment |
| 282 | +2. **Group similar data**: Repetition compression works best on sorted data |
| 283 | +3. **Flatten when possible**: Nested objects become dot-notation metadata |
| 284 | +4. **Avoid deep nesting in tables**: Tables should be flat records |
| 285 | + |
| 286 | +--- |
| 287 | + |
| 288 | +## See Also |
| 289 | + |
| 290 | +- [README.md](README.md) - Full documentation |
| 291 | +- [benchmarks/encoded_samples/](benchmarks/encoded_samples/) - Real-world examples |
0 commit comments