支持转储和加载的纯 Javascript YAML 库?

发布于 2024-07-11 17:15:25 字数 274 浏览 8 评论 0 原文

YAML (又名 YAML)?

如果这个曾经存在过,那么它一定已经被抹去了,因为最新的搜索结果一无所获。 看起来有很多实现仅从 Javascript 转储到 YAML 输出,但很难找到同时支持转储和加载的实现。

有没有人在做这样的事情……或者是需求太低了。

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 implementations that dump from Javascript to YAML output only, but having trouble finding an implementation that supports both dump and load.

Is anyone working on such a thing ... or is the demand simply far too low for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

〗斷ホ乔殘χμё〖 2024-07-18 17:15:25

只是在寻找相同的东西,这是一个基本的 基于 Javascript 的 YAML 解析器 编写的Tj Holowaychukrefactormycode.com。 我在此处复制它以确保它不会丢失,似乎 yaml.org 上的 JsYaml 链接已损坏一段时间。 还没有测试过。

;(function(){
  YAML = {
    valueOf: function(token) {
      return eval('(' + 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") 
          continue
        else if (key.exec(token) && tokens[0] == "\n")
          stack[RegExp.$1] = this.parse(tokens)
        else if (key.exec(token))
          stack[RegExp.$1] = this.valueOf(tokens.shift())
        else if (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 comment
  list: ['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

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) {
      return eval('(' + 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") 
          continue
        else if (key.exec(token) && tokens[0] == "\n")
          stack[RegExp.$1] = this.parse(tokens)
        else if (key.exec(token))
          stack[RegExp.$1] = this.valueOf(tokens.shift())
        else if (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 comment
  list: ['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
姐不稀罕 2024-07-18 17:15:25

可能是较新版本的 js-yaml:

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

Possibly newer version of js-yaml here:

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

若相惜即相离 2024-07-18 17:15:25

我更新这个问题以提供我自己研究的另一个解决方案:https://github.com/jeremyfa/yaml。 支持

它是 Symfony YAML 组件 (YAML 1.2) 的纯 JavaScript 端口,并且 装载和倾倒。 希望这可以帮助。

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.

复古式 2024-07-18 17:15:25

我不确定在哪里可以找到支持转储但不加载的“大量实现” - 就 JSON 是 YAML 1.2 的适当子集而言,我猜可能会有很多,但该子集使得YAML 不太人性化,尤其是对于复杂的数据结构。 我发现的大多数链接都是 JS-YAML 的 github 分支,这些分支依赖于 node.js 和/或仅提供解析器。

Jeremy Faivre 在 bitbucket 上的 yaml.js 在独立的 javascript 中实现了 YAML 的转储和加载(我从相关 stackoverflow 问题的答案中找到了它。 它没有得到积极维护,但似乎对于中等复杂的 YAML 和/或 javascript 对象工作得很好。

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.

不醒的梦 2024-07-18 17:15:25

yaml-javascript 假装既是转储器又是解析器。 从未尝试过。

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

云淡风轻 2024-07-18 17:15:25

还有这个 javascript-yaml-parser/< /a>

看到这个问题:JavaScript YAML Parser,Diogo Costa 说:
可用的 javascript YAML 解析器都无法满足我的需求,因此我开发了自己的解析器:它可以在此处使用: http://code.google.com/p/javascript-yaml-parser/

There's also this javascript-yaml-parser/

See this question: JavaScript YAML Parser, Diogo Costa says:
None of the javascript YAML parsers available satisfied my needs so I developed my own: It is available here: http://code.google.com/p/javascript-yaml-parser/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文