YAML
YAML Ain't Markup Language, định dạng tuần tự hóa dữ liệu dễ đọc dựa trên thụt lề.
YAML (YAML Ain't Markup Language) là định dạng tuần tự hóa dữ liệu dễ đọc that uses indentation to express hierarchical structure. Originally an acronym for "Yet Another Markup Language," it was renamed to the current recursive acronym to emphasize it is not a markup language. Widely used for configuration files and data exchange, it is the de facto standard particularly in the DevOps domain.
YAML được thiết kế như tập cha của JSON, meaning all valid JSON is valid YAML. Features absent in JSON include comments (#), anchors and aliases (data reuse), multiple documents in one file (separated by ---), and block scalars (multi-line text representation). It is widely adopted for configuration files in Docker Compose, Kubernetes manifests, GitHub Actions workflows, Ansible playbooks, CloudFormation templates, and other DevOps tools. DevOps introduction books cover YAML usage.
Cú pháp YAML có một số cạm bẫy đáng chú ý. Tab characters are not allowed for indentation; only spaces are permitted. Implicit type conversion can cause unexpected behavior: yes, no, on, off are auto-converted to booleans, and 1.0 to a floating-point number. The "Norway problem" where country code NO converts to false is a famous example. Strings must be quoted to ensure they are treated as strings.
Chọn giữa YAML và JSON phụ thuộc vào trường hợp sử dụng. YAML suits human-readable configuration files, with comment support being a major advantage. JSON suits programmatic data exchange, with simpler parser implementation and faster processing. The common practice is JSON for API responses and YAML for CI/CD configuration.
Từ góc độ bảo mật, deserialization YAML cần thận trọng. Some YAML parsers can instantiate arbitrary objects, and loading YAML from untrusted sources with unsafe parsers can lead to remote code execution vulnerabilities. In Python's PyYAML, yaml.safe_load() should be used instead of yaml.load(). Infrastructure automation books are also helpful.
Đối với đếm ký tự, YAML có xu hướng ít ký tự hơn JSON for the same data due to fewer brackets and quotes. However, indentation spaces count toward character count, so deeply nested structures may result in more characters in YAML. When optimizing configuration file sizes, the relationship between data structure depth and YAML/JSON character counts should be considered.