yaml

YAML subset parser and emitter with nested mappings, sequences and syntax errors.

v1.0.0Data formatsbundledimport "yaml";
Overview Installation Documentation Examples

Overview

Practical YAML subset parser and emitter for HolyPHP with zero dependencies. Supports nested mappings, block sequences, inline arrays and mappings, quoted and unquoted scalars, booleans, numbers, nulls, comments, and clear line-numbered syntax and indentation errors.

bundled — works offline config parse emit

Quick start

import "yaml";

$config = yaml_parse("
# Application config
name: holy-api
port: 8080
debug: false
tags: [web, api, \"v1\"]
database:
  host: localhost
  port: 5432
");

println($config["name"]);            // holy-api
println($config["database"]["port"]); // 5432

$yaml = yaml_stringify($config);
println($yaml);

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install yaml

This adds the library to hphp.yaml, installs it into .hphp/packages/yaml/ and pins the exact version in hphp.lock:

require:
  yaml: "^1.0"

Use it

import "yaml";

yaml is bundled with the compiler, so import "yaml"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install yaml          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install yaml
Packageyaml
Version1.0.0 (follows the compiler)
Entry fileyaml.hphp
Sourcepackages/lib/yaml ↗

Documentation

API Reference

yaml_parse(string $input)

Parses a YAML string into HolyPHP values (array, string, int, float, bool, or null).

Supported YAML features:

Parameters:

Returns: Parsed HolyPHP value (array, string, int, float, bool, or null).

---

yaml_stringify($value, int $indent = 2): string

Serializes a HolyPHP value (array, string, int, float, bool, or null) into a formatted YAML string. Automatically quotes strings that contain special characters or look like booleans, numbers, or null.

Parameters:

Returns: YAML-formatted string.

Error Handling

Invalid YAML or invalid options throw an Exception prefixed with yaml: and a line number when parsing:

Examples

01Quick start

Bundled with the compiler — import "yaml"; works offline in any file, or add it to a project with hphp install yaml.

import "yaml";

$config = yaml_parse("
# Application config
name: holy-api
port: 8080
debug: false
tags: [web, api, \"v1\"]
database:
  host: localhost
  port: 5432
");

println($config["name"]);            // holy-api
println($config["database"]["port"]); // 5432

$yaml = yaml_stringify($config);
println($yaml);
Try it
hphp install yaml
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_yaml.hphp.