Skip to content

jsonicjs/yaml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@jsonic/yaml

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.

npm version license

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

Install

Node.js

npm install @jsonic/yaml jsonic

jsonic (v2+) is a peer dependency.

Go

go get github.com/jsonicjs/yaml/go

Usage

TypeScript / JavaScript

const { 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 } }

Go

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)

API

TypeScript / JavaScript

Yaml — A Jsonic plugin function. Pass it to jsonic.use():

const jsonic = Jsonic.make().use(Yaml)
const data = jsonic(yamlString)

Go

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).

Testing

Node.js

npm install
npm run build
npm test

Run a subset of tests by pattern:

npm run test-some --pattern="4MUZ"

Go

cd go
go test ./...

The test suite validates against the official YAML Test Suite data files included in test/yaml-test-suite/.

License

MIT — Copyright (c) 2021 jsonicjs

About

YAML

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors