tanszek:oktatas:techcomm:yaml
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tanszek:oktatas:techcomm:yaml [2025/11/03 20:34] – created knehez | tanszek:oktatas:techcomm:yaml [2025/11/17 20:35] (current) – [Summary] knehez | ||
|---|---|---|---|
| Line 60: | Line 60: | ||
| ===== Data Types ===== | ===== Data Types ===== | ||
| - | * Strings, numbers, booleans, lists, and mappings (dictionaries) | ||
| - | * Multi-line strings are supported using `|` (literal) or `>` (folded): | ||
| + | YAML supports a range of basic and complex data types. | ||
| + | Values can be written in **implicit** or **explicit** form — YAML automatically detects the type from context, but types can also be specified manually using tags (e.g., `!!str`, `!!int`). | ||
| + | |||
| + | ==== 1. Scalars ==== | ||
| + | Scalars are single values such as strings, numbers, or booleans. | ||
| + | |||
| + | ^ Type | Example | Notes | | ||
| + | | String | `name: " | ||
| + | | Integer | `age: 25` | No quotes needed; negative values allowed. | | ||
| + | | Float | `price: 19.99` | Decimal notation or scientific form (`1.2e+3`) supported. | | ||
| + | | Boolean | `enabled: true` or `enabled: no` | `true/ | ||
| + | | Null | `value: null` or `value: ~` | Both mean “no value”. | | ||
| + | | Date/Time | `created: 2025-11-03` | ISO 8601 format is recommended. | | ||
| + | |||
| + | Explicit typing (less common but useful for validation): | ||
| <code yaml> | <code yaml> | ||
| - | description: | | + | id: !!int " |
| - | This text | + | flag: !!bool " |
| - | spans multiple | + | pi: !!float "3.14159" |
| - | lines. | + | text: !!str 1234 # forced as string, not number |
| </ | </ | ||
| + | |||
| + | ==== 2. Strings ==== | ||
| + | YAML offers flexible ways to define strings: | ||
| + | |||
| + | * **Plain style:** `title: Hello World` | ||
| + | |||
| + | * **Single-quoted: | ||
| + | (backslashes are preserved literally) | ||
| + | |||
| + | * **Double-quoted: | ||
| + | (supports escape sequences like `\n`, `\t`) | ||
| + | |||
| + | * **Multi-line literal (`|`):** preserves line breaks | ||
| + | <code yaml> | ||
| + | description: | ||
| + | This is line one. | ||
| + | This is line two. | ||
| + | </ | ||
| + | |||
| + | * **Folded block (`>`):** joins lines into a single paragraph | ||
| + | <code yaml> | ||
| + | note: > | ||
| + | This sentence | ||
| + | continues on the next line. | ||
| + | </ | ||
| + | |||
| + | ==== 3. Collections ==== | ||
| + | YAML supports two structured types: **sequences (lists)** and **mappings (dictionaries)**. | ||
| + | |||
| + | * **Sequences: | ||
| + | <code yaml> | ||
| + | colors: | ||
| + | - red | ||
| + | - green | ||
| + | - blue | ||
| + | </ | ||
| + | |||
| + | * **Mappings: | ||
| + | <code yaml> | ||
| + | person: | ||
| + | name: Bob | ||
| + | age: 30 | ||
| + | city: London | ||
| + | </ | ||
| + | |||
| + | * **Inline form:** lists and dictionaries can also be written on one line | ||
| + | <code yaml> | ||
| + | colors: [red, green, blue] | ||
| + | person: {name: Bob, age: 30} | ||
| + | </ | ||
| + | |||
| + | ==== 4. Nested Structures ==== | ||
| + | Lists and mappings can be combined to represent complex hierarchical data: | ||
| + | <code yaml> | ||
| + | students: | ||
| + | - name: Anna | ||
| + | grades: [A, B, A] | ||
| + | - name: Mark | ||
| + | grades: | ||
| + | - B | ||
| + | - C | ||
| + | - A | ||
| + | </ | ||
| + | |||
| + | ==== 5. Aliases and Anchors ==== | ||
| + | YAML allows referencing the same data in multiple places using **anchors (&)** and **aliases (*)**. | ||
| + | |||
| + | <code yaml> | ||
| + | defaults: &base | ||
| + | host: localhost | ||
| + | port: 8080 | ||
| + | |||
| + | development: | ||
| + | <<: *base | ||
| + | debug: true | ||
| + | |||
| + | release: | ||
| + | <<: *base | ||
| + | debug: false | ||
| + | </ | ||
| + | |||
| + | This feature reduces duplication and keeps configuration files consistent. | ||
| + | |||
| + | ==== 6. Summary ==== | ||
| + | * YAML automatically infers most types but supports explicit typing. | ||
| + | * Scalars, sequences, and mappings cover all standard data models. | ||
| + | * Multi-line and folded strings improve readability. | ||
| + | * Anchors and aliases allow reuse of data blocks. | ||
| + | |||
| ===== Validation and Schema ===== | ===== Validation and Schema ===== | ||
| Line 90: | Line 192: | ||
| </ | </ | ||
| - | ===== Educational Demo Idea ===== | + | |
| - | Show the same configuration both in JSON and YAML, and ask: | + | |
| - | * Which one is easier to read? | + | |
| - | * What are the risks of using indentation as syntax? | + | |
| - | * How does the structure represent a **syntax tree**? | + | |
| ===== Summary ===== | ===== Summary ===== | ||
| Line 100: | Line 198: | ||
| * It was created to bridge the gap between human readability and machine processing. | * It was created to bridge the gap between human readability and machine processing. | ||
| * It plays a central role in modern **DevOps**, **configuration management**, | * It plays a central role in modern **DevOps**, **configuration management**, | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== JSON & YAML Exercises ===== | ||
| + | |||
| + | ==== 1. Identify the format & fix errors ==== | ||
| + | |||
| + | **Task: | ||
| + | Determine whether the snippet is in JSON or YAML format. If invalid, fix it. | ||
| + | |||
| + | < | ||
| + | name: ChatGPT | ||
| + | skills: [" | ||
| + | version: " | ||
| + | </ | ||
| + | |||
| + | **Solution: | ||
| + | This is valid YAML. | ||
| + | |||
| + | |||
| + | ==== 2. Convert JSON → YAML ==== | ||
| + | |||
| + | **Task:** | ||
| + | |||
| + | <code json> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | **Solution (YAML):** | ||
| + | |||
| + | <code yaml> | ||
| + | user: John | ||
| + | role: student | ||
| + | active: true | ||
| + | points: 128 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 3. Convert YAML → JSON ==== | ||
| + | |||
| + | **Task:** | ||
| + | |||
| + | <code yaml> | ||
| + | server: | ||
| + | host: localhost | ||
| + | port: 8080 | ||
| + | logging: true | ||
| + | </ | ||
| + | |||
| + | **Solution (JSON):** | ||
| + | |||
| + | <code json> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 4. Extend the YAML configuration ==== | ||
| + | |||
| + | **Task: | ||
| + | Add an ' | ||
| + | |||
| + | <code yaml> | ||
| + | webserver: | ||
| + | host: 192.168.1.20 | ||
| + | port: 3000 | ||
| + | </ | ||
| + | |||
| + | **Solution: | ||
| + | |||
| + | <code yaml> | ||
| + | webserver: | ||
| + | host: 192.168.1.20 | ||
| + | port: 3000 | ||
| + | ssl: true | ||
| + | admins: | ||
| + | - alice | ||
| + | - bob | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 5. Valid or invalid JSON? Fix it. ==== | ||
| + | |||
| + | **Task:** | ||
| + | |||
| + | <code json> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | **Solution: | ||
| + | Errors: misplaced colon, trailing comma. | ||
| + | |||
| + | Correct version: | ||
| + | |||
| + | <code json> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 6. Fix the indentation errors (YAML) ==== | ||
| + | |||
| + | **Task:** | ||
| + | |||
| + | <code yaml> | ||
| + | database: | ||
| + | name: testdb | ||
| + | port: 5432 | ||
| + | host: localhost | ||
| + | </ | ||
| + | |||
| + | **Solution: | ||
| + | |||
| + | <code yaml> | ||
| + | database: | ||
| + | name: testdb | ||
| + | port: 5432 | ||
| + | host: localhost | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 7. Create the JSON structure ==== | ||
| + | |||
| + | **Task: | ||
| + | Create this structure in JSON: | ||
| + | |||
| + | * application | ||
| + | * name: " | ||
| + | * database: | ||
| + | * host: " | ||
| + | * port: 3306 | ||
| + | * users: [" | ||
| + | |||
| + | **Solution: | ||
| + | |||
| + | <code json> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 8. Rewrite " | ||
| + | |||
| + | **Task:** | ||
| + | |||
| + | <code yaml> | ||
| + | product: | ||
| + | name: hoodie | ||
| + | sizes: "S, M, L, XL" | ||
| + | </ | ||
| + | |||
| + | **Solution: | ||
| + | |||
| + | <code yaml> | ||
| + | product: | ||
| + | name: hoodie | ||
| + | sizes: | ||
| + | - S | ||
| + | - M | ||
| + | - L | ||
| + | - XL | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 9. Create your own configuration ==== | ||
| + | |||
| + | **Task: | ||
| + | Create a configuration (YAML or JSON) including: | ||
| + | |||
| + | * server settings | ||
| + | * 3 users | ||
| + | * roles | ||
| + | * features list (3 items) | ||
| + | |||
| + | **Solution (YAML example):** | ||
| + | |||
| + | <code yaml> | ||
| + | app: | ||
| + | server: | ||
| + | host: 0.0.0.0 | ||
| + | port: 5000 | ||
| + | |||
| + | users: | ||
| + | - name: anna | ||
| + | role: admin | ||
| + | - name: bela | ||
| + | role: editor | ||
| + | - name: koris | ||
| + | role: read-only | ||
| + | |||
| + | features: | ||
| + | - analytics | ||
| + | - backup | ||
| + | - notifications | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 10. List differences between JSON and YAML ==== | ||
| + | |||
| + | **Task: | ||
| + | List 5 differences. | ||
| + | |||
| + | **Solution: | ||
| + | |||
| + | * YAML uses indentation instead of curly braces. | ||
| + | * YAML supports comments (#), JSON does not. | ||
| + | * JSON has stricter syntax; YAML is more flexible. | ||
| + | * YAML supports advanced features (anchors, aliases). | ||
| + | * JSON keys must be strings; YAML keys do not require quotes. | ||
| + | |||
| + | |||
| + | ==== 11. Industrial config conversion ==== | ||
| + | |||
| + | **Task:** Convert to YAML and add `backup: true`. | ||
| + | |||
| + | <code json> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | {" | ||
| + | {" | ||
| + | ], | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | **Solution: | ||
| + | |||
| + | <code yaml> | ||
| + | plant: Factory_A | ||
| + | machines: | ||
| + | - id: 1 | ||
| + | type: CNC | ||
| + | status: online | ||
| + | - id: 2 | ||
| + | type: Laser | ||
| + | status: offline | ||
| + | schedule: | ||
| + | shift: night | ||
| + | workers: 12 | ||
| + | backup: true | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 12. Convert string booleans to real booleans ==== | ||
| + | |||
| + | **Task:** | ||
| + | |||
| + | <code yaml> | ||
| + | config: | ||
| + | verbose: " | ||
| + | auto_restart: | ||
| + | safe_mode: " | ||
| + | </ | ||
| + | |||
| + | **Solution: | ||
| + | |||
| + | <code yaml> | ||
| + | config: | ||
| + | verbose: true | ||
| + | auto_restart: | ||
| + | safe_mode: false | ||
| + | </ | ||
| + | |||
tanszek/oktatas/techcomm/yaml.1762202069.txt.gz · Last modified: 2025/11/03 20:34 by knehez
