A Jsonic plugin that adds YAML parsing support. Parse YAML documents into plain JavaScript objects (or Go values) using Jsonic's extensible grammar engine.
Available for both TypeScript/JavaScript (npm) and Go.
- Block mappings and sequences (indentation-based)
- Flow collections (
{a: 1, b: 2},[1, 2, 3]) - Quoted scalars (single and double), including multiline
- Block scalars (literal
|and folded>) with chomping indicators - Anchors (
&name) and aliases (*name), including merge keys (<<) - Multi-document streams (
---/...) - YAML value keywords (
true/false/yes/no/on/off,null/~,.inf,.nan) - Comments (
#) - Tags and
%TAGdirectives - Hex (
0x), octal (0o), and binary (0b) integer literals - Tested against the official YAML Test Suite
npm install @jsonic/yaml jsonicjsonic (v2+) is a peer dependency.
go get github.com/jsonicjs/yaml/goconst { Jsonic } = require('jsonic')
const { Yaml } = require('@jsonic/yaml')
const jsonic = Jsonic.make().use(Yaml)
const result = jsonic(`
name: Alice
items:
- one
- two
flags:
debug: true
`)
// { name: 'Alice', items: ['one', 'two'], flags: { debug: true } }package main
import (
"fmt"
yaml "github.com/jsonicjs/yaml/go"
)
func main() {
result, err := yaml.Parse(`
name: Alice
items:
- one
- two
flags:
debug: true
`)
if err != nil {
panic(err)
}
fmt.Println(result)
// map[flags:map[debug:true] items:[one two] name:Alice]
}You can also use the lower-level API to get a configured Jsonic instance:
j := yaml.MakeJsonic()
result, err := j.Parse(src)Yaml — A Jsonic plugin function. Pass it to jsonic.use():
const jsonic = Jsonic.make().use(Yaml)
const data = jsonic(yamlString)yaml.Parse(src string) (any, error) — Parse a YAML string directly.
yaml.MakeJsonic(opts ...YamlOptions) *jsonic.Jsonic — Create a reusable Jsonic instance configured for YAML.
yaml.Yaml — The raw Jsonic plugin function, for use with j.Use(yaml.Yaml, nil).
npm install
npm run build
npm testRun a subset of tests by pattern:
npm run test-some --pattern="4MUZ"cd go
go test ./...The test suite validates against the official
YAML Test Suite data files
included in test/yaml-test-suite/.
MIT — Copyright (c) 2021 jsonicjs