bx-yaml
bx-yaml: YAML Serialization
Installation
install-bx-module bx-yaml
# CommandBox
box install boxlang-yaml
BIFs
| BIF | Description |
|---|---|
yamlSerialize( content, [filepath], [charset] ) |
Serialize a BoxLang value to a YAML string (or file) |
yamlDeserialize( content ) |
Parse a YAML string into a BoxLang value |
yamlDeserializeFile( filepath, [charset] ) |
Parse a YAML file into a BoxLang value |
Basic Usage
// Serialize a struct to YAML
yaml = yamlSerialize( { name: "Luis", age: 21, city: "Orlando" } )
// Serialize nested structures
yaml = yamlSerialize({
name : "Luis",
age : 21,
address : { street: "1234 Main St", city: "Orlando", state: "FL" },
tags : [ "admin", "developer" ]
})
// Serialize to a file
yamlSerialize( appConfig, "/app/config/settings.yml" )
// Deserialize YAML string back to BoxLang value
data = yamlDeserialize( yaml )
println( data.name ) // Luis
println( data.address.city ) // Orlando
// Deserialize from a file
config = yamlDeserializeFile( "/app/config/settings.yml" )
Config File Pattern
// Read environment config
env = server.system.environment.APP_ENV ?: "development"
config = yamlDeserializeFile( "/app/config/#env#.yml" )
// Access nested config values
dbHost = config.database.host
dbPort = config.database.port
Serializing BoxLang Classes
By default, a class with serializable = true (or no annotation) is serialized using its properties:
class serializable=true {
property name="username" type="string"
property name="email" type="string"
property name="password" type="string" yamlExclude=true // excluded from YAML
}
user = new User().setUsername( "alice" ).setEmail( "alice@example.com" )
yaml = yamlSerialize( user )
// username and email are included; password is excluded
Custom toYAML() Method
class {
function init( name, age, city ) {
variables.name = name
variables.age = age
variables.city = city
}
// Only expose name and city (not age)
function toYAML() {
return { name: variables.name, city: variables.city }
}
}
person = new Person( "Luis", 21, "Orlando" )
yaml = yamlSerialize( person )
// Calls toYAML() — only name and city appear in output
Property-Level Exclusion Options
yamlExclude=trueon a property — exclude this property from YAML serializationserializable=falseon the class — prevents the whole class from being serializedyamlExcludelist on the class — list of property names to exclude
Common Pitfalls
- ✅ Use
yamlDeserializeFile()for reading config files rather thanfileRead()+yamlDeserialize() - ❌ YAML is whitespace-sensitive — don't manually construct YAML strings; always use
yamlSerialize() - ✅ Use the
yamlExcludeannotation to prevent sensitive fields (passwords, tokens) from being serialized
More from ortus-boxlang/skills
boxlang-functional-programming
Use this skill when working with BoxLang lambdas, closures, arrow functions, higher-order functions, functional array/struct pipelines (map, filter, reduce, flatMap, groupBy, etc.), destructuring, or spread syntax.
10boxlang-code-reviewer
Use this skill when reviewing BoxLang code for quality, correctness, security vulnerabilities, performance issues, style violations, or when providing structured code review feedback following BoxLang best practices and security guidelines.
9boxlang-best-practices
Use this skill when writing, reviewing, or improving BoxLang code to ensure it follows community best practices for naming, structure, scoping, error handling, performance, and maintainability.
9boxlang-classes-and-oop
Use this skill when writing BoxLang classes, components, interfaces, inheritance hierarchies, annotations, properties, constructors, or applying object-oriented design patterns in BoxLang.
9boxlang-web-development
Use this skill when building BoxLang web applications: Application.bx lifecycle, request/response handling, sessions, forms, REST APIs, HTTP clients, routing, CSRF protection, Server-Sent Events, or configuring CommandBox/MiniServer.
8boxlang-configuration
Use this skill when configuring BoxLang runtime settings via boxlang.json, setting environment variables for config overrides, configuring datasources, caches, executors, modules, logging, security, or schedulers — or when helping someone understand the BoxLang configuration system.
8