|
| 1 | +// W++ JSON Library Demo |
| 2 | +// Demonstrates all JSON functionality |
| 3 | + |
| 4 | +import { parse, stringify, pretty, validate, isValid, get, getString, getInt, merge } from "json" |
| 5 | + |
| 6 | +func main() { |
| 7 | + print("=== W++ JSON Library Demo ===") |
| 8 | + print("") |
| 9 | + |
| 10 | + // ======================================== |
| 11 | + // 1. JSON Parsing and Validation |
| 12 | + // ======================================== |
| 13 | + print("--- JSON Parsing & Validation ---") |
| 14 | + |
| 15 | + let valid_json = "{\"name\":\"Alice\",\"age\":30,\"city\":\"NYC\"}" |
| 16 | + let invalid_json = "{name: Alice}" // Missing quotes |
| 17 | + |
| 18 | + print("Valid JSON:", valid_json) |
| 19 | + print("Is valid?", isValid(valid_json)) |
| 20 | + |
| 21 | + print("Invalid JSON:", invalid_json) |
| 22 | + print("Is valid?", isValid(invalid_json)) |
| 23 | + print("") |
| 24 | + |
| 25 | + // ======================================== |
| 26 | + // 2. JSON Stringify and Pretty Print |
| 27 | + // ======================================== |
| 28 | + print("--- JSON Stringify & Pretty Print ---") |
| 29 | + |
| 30 | + let user_json = "{\"name\":\"Bob\",\"age\":25,\"hobbies\":[\"coding\",\"gaming\"]}" |
| 31 | + |
| 32 | + print("Minified:") |
| 33 | + print(stringify(user_json)) |
| 34 | + print("") |
| 35 | + |
| 36 | + print("Pretty (2 spaces):") |
| 37 | + print(pretty(user_json, 2)) |
| 38 | + print("") |
| 39 | + |
| 40 | + print("Pretty (4 spaces):") |
| 41 | + print(pretty(user_json, 4)) |
| 42 | + print("") |
| 43 | + |
| 44 | + // ======================================== |
| 45 | + // 3. JSON Query Operations |
| 46 | + // ======================================== |
| 47 | + print("--- JSON Query Operations ---") |
| 48 | + |
| 49 | + let person = "{\"user\":{\"name\":\"Charlie\",\"email\":\"charlie@example.com\"},\"score\":100}" |
| 50 | + |
| 51 | + print("Full JSON:", person) |
| 52 | + print("") |
| 53 | + |
| 54 | + print("Get nested value (user.name):") |
| 55 | + print(get(person, "user.name")) |
| 56 | + print("") |
| 57 | + |
| 58 | + print("Get string value (direct):") |
| 59 | + let simple = "{\"title\":\"Hello World\",\"count\":42}" |
| 60 | + print("Title:", getString(simple, "title")) |
| 61 | + print("Count:", getInt(simple, "count")) |
| 62 | + print("") |
| 63 | + |
| 64 | + // ======================================== |
| 65 | + // 4. JSON Merge |
| 66 | + // ======================================== |
| 67 | + print("--- JSON Merge ---") |
| 68 | + |
| 69 | + let obj1 = "{\"a\":1,\"b\":2}" |
| 70 | + let obj2 = "{\"b\":3,\"c\":4}" |
| 71 | + |
| 72 | + print("Object 1:", obj1) |
| 73 | + print("Object 2:", obj2) |
| 74 | + print("Merged:", pretty(merge(obj1, obj2), 2)) |
| 75 | + print("") |
| 76 | + |
| 77 | + // ======================================== |
| 78 | + // 5. Complex Nested JSON |
| 79 | + // ======================================== |
| 80 | + print("--- Complex Nested JSON ---") |
| 81 | + |
| 82 | + let complex = "{\"users\":[{\"id\":1,\"name\":\"Alice\"},{\"id\":2,\"name\":\"Bob\"}],\"metadata\":{\"version\":\"1.0\",\"count\":2}}" |
| 83 | + |
| 84 | + print("Complex JSON (pretty):") |
| 85 | + print(pretty(complex, 2)) |
| 86 | + print("") |
| 87 | + |
| 88 | + print("Get metadata.version:") |
| 89 | + print(get(complex, "metadata.version")) |
| 90 | + print("") |
| 91 | + |
| 92 | + // ======================================== |
| 93 | + // 6. Error Handling |
| 94 | + // ======================================== |
| 95 | + print("--- Error Handling ---") |
| 96 | + |
| 97 | + let malformed = "{broken json" |
| 98 | + print("Malformed JSON:", malformed) |
| 99 | + print("Validation result:", validate(malformed)) |
| 100 | + print("") |
| 101 | + |
| 102 | + print("=== Demo Complete ===") |
| 103 | +} |
0 commit comments