User Tools

Site Tools


tanszek:oktatas:techcomm:yaml

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tanszek:oktatas:techcomm:yaml [2025/11/03 20:34] – created kneheztanszek:oktatas:techcomm:yaml [2025/11/03 20:39] (current) – [Data Types] 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: "Alice"` | Quotation marks are optional unless special characters are used. |
 +| 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/false`, `yes/no`, and `on/off` are equivalent. |
 +| 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 "42" 
-  This text +flag: !!bool "yes" 
-  spans multiple +pi: !!float "3.14159" 
-  lines.+text: !!str 1234   # forced as string, not number
 </code> </code>
 +
 +==== 2. Strings ====
 +YAML offers flexible ways to define strings:
 +
 +  * **Plain style:** `title: Hello World`
 +
 +  * **Single-quoted:** `path: 'C:\Users\Name'`  
 +    (backslashes are preserved literally)
 +
 +  * **Double-quoted:** `message: "Line1\nLine2"`  
 +    (supports escape sequences like `\n`, `\t`)
 +
 +  * **Multi-line literal (`|`):** preserves line breaks  
 +    <code yaml>
 +    description: |
 +      This is line one.
 +      This is line two.
 +    </code>
 +
 +  * **Folded block (`>`):** joins lines into a single paragraph  
 +    <code yaml>
 +    note: >
 +      This sentence
 +      continues on the next line.
 +    </code>
 +
 +==== 3. Collections ====
 +YAML supports two structured types: **sequences (lists)** and **mappings (dictionaries)**.
 +
 +  * **Sequences:** ordered lists of elements, marked with `-`
 +    <code yaml>
 +    colors:
 +      - red
 +      - green
 +      - blue
 +    </code>
 +
 +  * **Mappings:** unordered key–value pairs
 +    <code yaml>
 +    person:
 +      name: Bob
 +      age: 30
 +      city: London
 +    </code>
 +
 +  * **Inline form:** lists and dictionaries can also be written on one line  
 +    <code yaml>
 +    colors: [red, green, blue]
 +    person: {name: Bob, age: 30}
 +    </code>
 +
 +==== 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
 +</code>
 +
 +==== 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
 +</code>
 +
 +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 =====
tanszek/oktatas/techcomm/yaml.txt · Last modified: 2025/11/03 20:39 by knehez