Skip to content Skip to sidebar Skip to footer

Pure Javascript Yaml Library That Supports Both Dump And Load?

Does such a thing exist for YAML (aka YAML)? If this existed at one time, it must have been obliterated because the latest search turned up nada. It looks like there are plenty of

Solution 1:

Was just looking for the same, here's a basic Javascript-based YAML parser written by Tj Holowaychuk over at refactormycode.com. I'm duplicating it here to ensure it isn't lost, appears the JsYaml link on yaml.org has been broken a while. Haven't tested it yet.

;(function(){
  YAML = {
    valueOf: function(token) {
      returneval('(' + token + ')')
    },

    tokenize: function(str) {
      return str.match(/(---|true|false|null|#(.*)|\[(.*?)\]|\{(.*?)\}|[\w\-]+:|-(.+)|\d+\.\d+|\d+|\n+)/g)
    },

    strip: function(str) {
      return str.replace(/^\s*|\s*$/, '')
    },

    parse: function(tokens) {
      var token, list = /^-(.*)/, key = /^([\w\-]+):/, stack = {}
      while (token = tokens.shift())
        if (token[0] == '#' || token == '---' || token == "\n") 
          continueelseif (key.exec(token) && tokens[0] == "\n")
          stack[RegExp.$1] = this.parse(tokens)
        elseif (key.exec(token))
          stack[RegExp.$1] = this.valueOf(tokens.shift())
        elseif (list.exec(token))
          (stack.constructor == Array ?
            stack : (stack = [])).push(this.strip(RegExp.$1))
      return stack
    },

    eval: function(str) {
      return this.parse(this.tokenize(str))
    }
  }
})()

print(YAML.eval(readFile('config.yml')).toSource())




// config.yml

---
  # just a commentlist: ['foo', 'bar']
  hash: { foo: "bar", n: 1 }
  lib:
    - lib/cart.js
    - lib/cart.foo.js
  specs:
    - spec/cart.spec.js
    - spec/cart.foo.spec.js
    # - Commented out
  environments:
    all:
      options:
        failuresOnly: true
        verbose: false

Solution 2:

I update this question to give another solution that myself worked on: https://github.com/jeremyfa/yaml.js

It is a pure javascript port of Symfony YAML Component (YAML 1.2) and supports both loading and dumping. Hope this helps.

Solution 3:

Possibly newer version of js-yaml here:

http://github.com/visionmedia/js-yaml

Solution 4:

I'm not sure where the "plenty of implementations" that support dump but not load are to be found - to the extent that JSON is a proper subset of YAML 1.2, I guess there might be plenty of those, but that subset makes for YAML that is not particular human friendly, especially for complex data structures. Most of the links I have found are to github forks of JS-YAML that depend on node.js and/or only provide parsers.

Jeremy Faivre's yaml.js on bitbucket implements both dump and load of YAML in standalone javascript (I found it from an answer to a related stackoverflow question). It is not actively maintained, but seems to work fine for moderately complex YAML and/or javascript objects.

Solution 5:

yaml-javascript pretends to be both dumper and parser. Never tried.

Post a Comment for "Pure Javascript Yaml Library That Supports Both Dump And Load?"