• Stars
    star
    875
  • Rank 50,171 (Top 2 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.

Build status

⚠️ Development of this library has slowed-down

I am still using yaml.js in production for some projects, it works fine in all the situations I needed it. That said, I am not actively working with raw javascript environments (mostly using haxe now, if you are curious), thus I don't have much bandwidth to actively provide support to the posted issues asking for new features or bugfixes that don't affect my own use cases of the library. If this situation is an issue for you, I suggest you use js-yaml which is a great and pretty feature-complete yaml parser and dumper for javascript. Pull Requests are still welcome, as long as they don't break the current set of unit tests!

Thanks 🙏

yaml.js

Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.

Mainly inspired from Symfony Yaml Component.

How to use

Import yaml.js in your html page:

<script type="text/javascript" src="yaml.js"></script>

Parse yaml string:

nativeObject = YAML.parse(yamlString);

Dump native object into yaml string:

yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */[, spaces /* @integer number of spaces to use for indentation */] ]);

Load yaml file:

nativeObject = YAML.load('file.yml');

Load yaml file:

YAML.load('file.yml', function(result)
{
    nativeObject = result;
});

Use with node.js

Install module:

npm install yamljs

Use it:

YAML = require('yamljs');

// parse YAML string
nativeObject = YAML.parse(yamlString);

// Generate YAML
yamlString = YAML.stringify(nativeObject, 4);

// Load yaml file using YAML.load
nativeObject = YAML.load('myfile.yml');

Command line tools

You can enable the command line tools by installing yamljs as a global module:

npm install -g yamljs

Then, two cli commands should become available: yaml2json and json2yaml. They let you convert YAML to JSON and JSON to YAML very easily.

yaml2json

usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input

Positional arguments:
  input                 YAML file or directory containing YAML files.

Optional arguments:
  -h, --help            Show this help message and exit.
  -v, --version         Show program's version number and exit.
  -p, --pretty          Output pretty (indented) JSON.
  -i INDENTATION, --indentation INDENTATION
                        Number of space characters used to indent code (use 
                        with --pretty, default: 2).
  -s, --save            Save output inside JSON file(s) with the same name.
  -r, --recursive       If the input is a directory, also find YAML files in 
                        sub-directories recursively.
  -w, --watch           Watch for changes.

json2yaml

usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input

Positional arguments:
  input                 JSON file or directory containing JSON files.

Optional arguments:
  -h, --help            Show this help message and exit.
  -v, --version         Show program's version number and exit.
  -d DEPTH, --depth DEPTH
                        Set minimum level of depth before generating inline 
                        YAML (default: 2).
  -i INDENTATION, --indentation INDENTATION
                        Number of space characters used to indent code 
                        (default: 2).
  -s, --save            Save output inside YML file(s) with the same name.
  -r, --recursive       If the input is a directory, also find JSON files in 
                        sub-directories recursively.
  -w, --watch           Watch for changes.

examples

# Convert YAML to JSON and output resulting JSON on the console
yaml2json myfile.yml

# Store output inside a JSON file
yaml2json myfile.yml > output.json

# Output "pretty" (indented) JSON
yaml2json myfile.yml --pretty

# Save the output inside a file called myfile.json
yaml2json myfile.yml --pretty --save

# Watch a full directory and convert any YAML file into its JSON equivalent
yaml2json mydirectory --pretty --save --recursive

# Convert JSON to YAML and store output inside a YAML file
json2yaml myfile.json > output.yml

# Output YAML that will be inlined only after 8 levels of indentation
json2yaml myfile.json --depth 8

# Save the output inside a file called myfile.json with 4 spaces for each indentation
json2yaml myfile.json --indentation 4

# Watch a full directory and convert any JSON file into its YAML equivalent
json2yaml mydirectory --pretty --save --recursive

Credits / Thanks

  • Symfony Yaml Component which was the reference implementation initially.
  • minj who did fix various bugs and ported a first batch of unit tests in an earlier version of yamljs.

More Repositories

1

node-redis-dump

Dump redis database into redis commands or json with command line or node.js
JavaScript
110
star
2

node-ts2hx

Typescript to Haxe transpiler written in JavaScript
JavaScript
98
star
3

interpret

Turn haxe classes into scriptable/interpretable/hot-reloadable code using hscript
Haxe
67
star
4

bind

Bind Objective-C/Swift and Java/JNI code to Haxe without writing glue code manually.
Haxe
53
star
5

spine-hx

Spine runtime for Haxe automatically converted from the official Java/libgdx runtime
Haxe
53
star
6

node-exec-sync

IMPORTANT: This repository is no longer maintained.
CoffeeScript
48
star
7

docpad-plugin-minicms

Add admin features to docpad
JavaScript
45
star
8

openfl-sockjs

Cross-platform SockJS client library for realtime remoting between OpenFL apps and SockJS websocket server.
Haxe
20
star
9

vscode-haxe-blocks

An extension to display function block decorations on haxe code
Haxe
10
star
10

arcade

An arcade-oriented physics engine for Haxe
Haxe
8
star
11

fuzzaldrin

Fuzzy filtering and string scoring (ported to haxe from https://github.com/atom/fuzzaldrin)
Haxe
7
star
12

polyline

An utility to create polylines written in Haxe (ported from https://github.com/mattdesl/extrude-polyline)
Haxe
7
star
13

tracker

A set of tools to manage events, observable and serializable properties, autorunable functions and data models in general.
Haxe
7
star
14

haxe-xmlgrammar

Patch main haxe grammar to get xml syntax highlighting in strings
Haxe
7
star
15

hx2objc

Macros to automatically generate bindings from Haxe/HXCPP to Objective-C/Swift compatible code.
Haxe
6
star
16

phpore

Online RPG Editor written in php. Personal project from 2005. WARNING: this is old-fashion code. I was a teenager when I wrote this engine. Author website: http://www.jeremyfa.com
PHP
5
star
17

linc_soloud

Soloud bindings for Haxe C++ target
C
4
star
18

node-ts2json

Get JSON SyntaxTree from TypeScript files
JavaScript
4
star
19

flixel-spine-hx

HaxeFlixel + Spine example using spine-hx
Haxe
3
star
20

libturbojpeg-ios

Shell
3
star
21

node-cocos2d-coffee-autocomplete

Provide autocompletion for cocos2d-x/html5 projects written in CoffeeScript
CoffeeScript
3
star
22

atom-lime

atom.io support for haxe lime/openfl build tool
JavaScript
2
star
23

generate

An utility to generate haxe code, intended for code completion, from json-ish data.
Haxe
2
star
24

dmConfigOverriderPlugin

PHP
2
star
25

ld49

My submission for Ludum Dare (LDJAM / LD49)
Haxe
2
star
26

vscode-tasks-chooser

Allow to choose tasks and change default build command from status bar
Haxe
2
star
27

autorun

Utilities to create statically typed events, observable objects and autorun callbacks in Haxe language
Haxe
2
star
28

hxvue

Haxe js externs for Vue (http://vuejs.org)
Haxe
2
star
29

ceramic-demo

The project used to showcase some ceramic features in [Discover ceramic](https://jeremyfa.com/what-is-ceramic-engine/) article
Haxe
2
star
30

ld46

Ludum Dare LDJAM 46
Haxe
1
star
31

ludumdare-32

My participation in Ludum Dare #32
Haxe
1
star
32

node-miniproxy

Run a proxy that routes your different projects using their package.json files
CoffeeScript
1
star
33

node-energenie

Remotely control power sockets from the Raspberry Pi (wrapper around energenie python package)
JavaScript
1
star
34

lib-hxcpp-runtime

HXCPP runtime wrapper to use Haxe libraries from standard iOS/Android projects
Haxe
1
star
35

dmAkismetPlugin

Akismet port for Diem CMF (Symfony)
1
star