Skip to content

Convenience Type Casting Methods

Mihai A. 🇷🇴🇩🇪🇬🇧🇫🇷 edited this page May 5, 2024 · 3 revisions

Convenience Type-Casting Methods

As per the YAML spec, a YAML object/document can hold mappings, sequences or scalars (strings). eo-yaml also offers convenience type-casting methods that wrap the calls to .string(...) methods so you can cast your plain string scalar on the fly.

---
integer: 123
float: 3.54
double: 2.05
long: 32165498
localDate: 2007-12-03
localDateTime: 2007-12-03T10:15:30
boolean: true
...
final YamlMapping map = Yaml.createYamlInput("casting.yml").readYamlMapping();
int value = map.integer("integer");
float flValue = map.floatNumber("float");
double dbValue = map.doubleNumber("double");
long lnValue = map.longNumber("long");
java.time.LocalDate date = map.date("localDate");
java.time.LocalDateTime dateTime = map.dateTime("localDateTime");
boolean boolValue = map.bool("boolean");

The same type-casting methods are available for YamlSequence as well, of course.