Customize parsing with a custom Constructor
mainYou can modify how Yams interprets specific YAML tags by providing a custom Constructor. This is done by creating a new Constructor instance with a modified defaultScalarMap. This allows you to override default behaviors, such as restricting boolean parsing to only true and false literals.
import Yams
extension Constructor {
public static func withBoolAsTrueFalse() -> Constructor {
var map = defaultScalarMap
map[.bool] = Bool.constructUsingOnlyTrueAndFalse
return Constructor(map)
}
}
// Usage:
let yamlString = """
- true
- on
- off
- false
"""
if let array = try? Yams.load(yaml: yamlString, .default, .withBoolAsTrueFalse()) as? [Any] {
print(array)
}
// Prints: [true, "on", "off", false]