The integer type in YAML represents whole numbers, allowing for various numerical bases and signs, making it a versatile scalar data type for configuration and data serialization.
Understanding YAML Integers
In YAML, an integer is a scalar value that signifies a whole number. Unlike some programming languages, YAML itself doesn't impose strict limits on the size of an integer, often allowing for arbitrary precision, though the actual limits may depend on the specific YAML parser and the programming language it's implemented in. YAML integers can be positive or negative.
Different Bases for Integers
YAML supports expressing integers in several common numerical bases, which enhances flexibility for different use cases, especially when dealing with low-level configurations or bitmasks.
-
Decimal Integers: These are the most common and represent base-10 numbers. They are written without any special prefixes.
- Examples:
count: 123
negative_value: -45
zero: 0
- Examples:
-
Octal Integers: Representing base-8 numbers, octal values are identified by a leading zero (
0
). This convention is important to remember as a number like010
is not ten, but eight.- Examples:
octal_number: 010
(equivalent to 8 in decimal)permissions: 0644
(a common representation for file permissions)
- Examples:
-
Hexadecimal Integers: These are base-16 numbers and are distinguished by the prefix
0x
. Hexadecimal numbers can include digits 0-9 and letters A-F (case-insensitive) to represent values 10-15.- Examples:
hex_value: 0xA
(equivalent to 10 in decimal)rgb_color: 0xFF00FF
(representing a magenta color)
- Examples:
Sign and Precision
YAML integers can be prefixed with a +
or -
sign. While +
is usually optional for positive numbers, -
is mandatory for negative values.
For example:
positive: +50
negative: -100
The YAML specification, particularly YAML 1.2, suggests that integers should support arbitrary precision. This means, theoretically, an integer can be as large as necessary. However, in practice, parsers written in languages like Java or Python will typically map these to their native integer types (e.g., long
in Java, int
in Python 3), which do have maximum limits.
Summary of Integer Types
The following table summarizes the different ways to represent integers in YAML:
Type | Prefix | Example | Decimal Equivalent | Description |
---|---|---|---|---|
Decimal | None | 100 |
100 | Standard base-10 numbers. |
Octal | 0 (zero) |
0144 |
100 | Base-8 numbers, indicated by a leading zero. |
Hexadecimal | 0x |
0x64 |
100 | Base-16 numbers, indicated by 0x . |
Practical Considerations
When working with YAML integers, especially with leading zeros, be mindful of the interpretation. A number like 08
would be considered invalid octal by most parsers because 8
is not a valid octal digit. Parsers typically treat such cases as errors or fall back to decimal parsing, depending on the implementation. For robust and clear YAML, explicitly using decimal representation is often preferred unless octal or hexadecimal is specifically required for clarity or compatibility with existing systems.
For more details on YAML's data types, refer to the official YAML 1.2 Specification.