Skip to content

jsonicjs/yaml

Repository files navigation

@jsonic/yaml

A Jsonic plugin that parses YAML into plain JavaScript objects or Go values, using Jsonic's extensible grammar engine.

Available for both TypeScript/JavaScript (npm) and Go.

npm version license

Documentation

Full docs follow the Diataxis structure (tutorials / how-to guides / reference / explanation):

Features

  • 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 %TAG directives
  • Hex (0x), octal (0o), and binary (0b) integer literals
  • Tested against the official YAML Test Suite

Quick start

Node.js

npm install @jsonic/yaml jsonic
const { Jsonic } = require('jsonic')
const { Yaml } = require('@jsonic/yaml')

const j = Jsonic.make().use(Yaml)
j("name: Alice\nitems:\n  - one\n  - two\n")
// { name: 'Alice', items: ['one', 'two'] }

See doc/yaml-ts.md for the full guide.

Go

go get github.com/jsonicjs/yaml/go
import yaml "github.com/jsonicjs/yaml/go"

result, err := yaml.Parse("name: Alice\nitems:\n  - one\n  - two\n")
// map[name:Alice items:[one two]]

See doc/yaml-go.md for the full guide.

Development

A Makefile at the repo root drives both builds:

make           # embed grammar, build, and test both TS and Go
make test      # run both test suites
make test-ts   # Node.js tests only
make test-go   # Go tests only
make embed     # re-embed yaml-grammar.jsonic into src/yaml.ts and go/grammar.go
make reset     # clean install, rebuild, and re-test everything

The grammar definition lives in yaml-grammar.jsonic and is embedded into src/yaml.ts and go/grammar.go by embed-grammar.js. After editing the grammar file, run make embed to re-sync the source files.

License

MIT — Copyright (c) 2021 jsonicjs