jeremyfa / yaml.js Goto Github PK
View Code? Open in Web Editor NEWStandalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.
License: MIT License
Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.
License: MIT License
http://mistakes.io/#fce34f14a4d465938bec
Yes and No should parse to true
and false
- this is mentioned in the YAML spec (though in vague terms) and supported by most other YAML parsers.
Certain YAML libraries like SnakeYAML can generate documents that feature double quoted line breaks (see example 7.5) in order to wrap long double-quoted scalars. Here's an example of such a document:
--- "I've made this string so long that SnakeYAML will wrap it! Mwarhar. My long strings\
\ shall dominate the world!"
Ruby 2.2.1's YAML parser (which wraps libyaml) and JS-YAML can convert these documents back to this string as expected:
"I've made this string so long that SnakeYAML will wrap it! Mwarhar. My long strings shall dominate the world!"
However, yaml.js claims that this is invalid syntax, throwing a ParseException
with the following details:
message: "Unable to parse.",
parsedLine: 1,
snippet: " \ shall dominate the world!""
This bug may impact interoperability with all systems that generate double-quoted scalars.
The command line tools seem to demand input be in the form of a file. By unix convention, if "-" is passed in, the program ought attempt to use stdin, rather than a file. yamljs merely attempts to open a "-" file.
This issue relates to a problem being experienced by some DocPad users (see docpad/docpad#321).
yamljs parses date-like properties and converts them to javascript Date objects using new Date(string)
:
https://github.com/jeremyfa/yaml.js/blob/master/src/yaml/YamlInline.js#L429-430
This is a useful behavior, but it is affected by the underlying inconsistent behavior of Javascript itself. Concider these three properties:
date: 2013-10-14
dateTime: 2013-10-14 00:00:00
dateTime2: 2013-10-13 19:00:00
The problem with new Date(string)
is that it is inconsistent in what timezone it assumes. For strings that are just a date (e.g. date
), it returns a date that represents midnight UTC. For strings that include a date and a time (e.g. dateTime
and dateTime2
), it returns a date that represents the specified time in the local timezone.
This behavior is odd, but it is what it is. It becomes troublesome in the context of yaml.js because it is no longer possible to tell from the resulting JSON what was intended by the author of the YAML.
Concider the metadata above run through yamls.js on a browser running in the central timezone (US). You get these results:
date: Sun Oct 13 2013 19:00:00 GMT-0500 (Central Daylight Time)
dateTime: Mon Oct 14 2013 00:00:00 GMT-0500 (Central Daylight Time)
dateTime2: Sun Oct 13 2013 19:00:00 GMT-0500 (Central Daylight Time)
The challenge is that you can not distinguish between date
and dateTime2
and it's highly likely that the author of the YAML did not intend them to be the same date. I would suggest that most authors probably would have expected that date
and dateTime
were the two that would end up being the same value.
As you can see in docpad/docpad#321, some DocPad users have bumped into this. The DocPad code can't workaround it at the moment, because by the time the DocPad code sees the parsed YAML, it is not possible to tell if the author typed just a lone date.
Are you open to changes to yamljs to address this? I can think of a couple possible options:
Thoughts?
Have a quick question, I expect output to be like this:
---
- include: RedHat.yml
when: ansible_os_family == 'RedHat'
but I get output like this:
---
-
include: RedHat.yml
when: 'ansible_os_family == ''RedHat'''
Is it possible?
First, everything under "when" is encapsulated in quotes. Can I get rid of this quote?
Second, the line break between "-" and "include".
Hi there Jeremy,
I tried to use your yaml parser in vert.x. However it is not supported out of the box as your library is completely wrapped in a closure and depends on the existence of browser.window or node.js module exports... Unfornuately Vertx does not have either of those... so I had to hack into it a couple of extra lines.
https://gist.github.com/3652677
Kind Regards,
Daryl Teo
bower.json has not specified an updated version since 0.2.0. This seems to be having some adverse effects on bower install.
SyntaxError: Cannot declare a parameter named 'e' as it shadows the name of a strict mode function.
This is the fragment that triggers the error (function e(e,t,n){
.
For some reason not described in ECMA-262, Safari decides that function name and function argument should not be the same in strict mode.
file "yamljs/bin/yaml.min.js" is Empty
when i installed through bower.
i assume bower is not updated
As title, here's my error message.
c:\Users\user\Documents\GitHub\others\train\node_modules\yamljs\bin\yaml.js:1589
value = value.split("\r\n").join("\n").split("\r").join("\n");
^
TypeError: Object has no method 'split'
at Object.YamlParser.cleanup (c:\Users\user\Documents\GitHub\others\train\no
de_modules\yamljs\bin\yaml.js:1589:17)
at Object.YamlParser.parse (c:\Users\user\Documents\GitHub\others\train\node
_modules\yamljs\bin\yaml.js:980:21)
at Object.Yaml.parse (c:\Users\user\Documents\GitHub\others\train\node_modul
es\yamljs\bin\yaml.js:240:15)
at c:\Users\user\Documents\GitHub\others\train\node_modules\yamljs\bin\yaml.
js:215:24
at c:\Users\user\Documents\GitHub\others\train\node_modules\yamljs\bin\yaml.
js:309:22
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
Hello
I have following JSON
"services" : { "memcached" : { "class" : "Memcached", "calls" : [ [ "addServers", ["%memcache_hosts%"] ] ] }, "doctrine.cache.memcached" : { "class" : "Doctrine\\Common\\Cache\\MemcachedCache", "calls" : [ [ "setMemcached", ["@memcached"] ] ] }, "sync_cache": { "class" : "Doctrine\\Common\\Cache\\MemcachedCache", "calls" : [ [ "setMemcached", [ "@memcached" ] ], [ "setNamespace", [ 'a' ] ] ] } }
And it give me that yaml output
services: memcached: class: Memcached calls: - [addServers, ['%memcache_hosts%']] doctrine.cache.memcached: class: Doctrine\Common\Cache\MemcachedCache calls: - [setMemcached, ['@memcached']] sync_cache: class: Doctrine\Common\Cache\MemcachedCache calls: - [setMemcached, ['@memcached']] - [setNamespace, [a]]
I need the - [setNamespace, [a]] to look like this - [setNamespace, ["a"]]. How can i do this ?
loading yaml.js results in a deprecation warnings in most recent browser version.
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
To fix this, xhr open() should be called using the async=true option
Interesting issue in v0.2.1 (which is current, and the one on NPM - perhaps NPM should have the master/stable branch?)
---
key: { val1: "string", val2: { v2k1: "v2k1" } }
produces
{"key":{"val1":"string","val2":"string"}}
One is empty
Hi,
In my code I try to have an indent output to be appended to another yaml structure (from template)
I read in api docs
# @param [Object] input The JavaScript value
# @param [Integer] inline The level where you switch to inline YAML
# @param [Integer] indent The level of indentation (used internally)
...
So I would expext that yamljs.stringify({ Test: { hello: "world" } }, 2, 2)
would put Test
with an indentation of 2. It is instead always put at the beginning of the line
Is it my misunderstanding or is it a bug ?
Thank you
https://raw.githubusercontent.com/companieshouse/api-enumerations/master/constants.yml
The following:
yaml2json constants.yml > constants.json
resulted in the error:
Unable to parse.
The yaml in the file has a single indent. Once un-indented it parsed fine. I'm not sure if this is the fault of the yaml file not meeting yaml specs.
Thanks for the useful module.
$ npm i yamljs
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
/private/tmp
└─┬ [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
└─┬ [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
└── [email protected]
Getting Malformed inline YAML string ('Enter desired Kontagent subdomain).
for
de:
kontagent_integration_request:
! 'Enter desired Kontagent subdomain: ':
en-US:
/admin/curated_channels:
! 'Search channel:':
however its valid on http://www.yamllint.com/
YAMLLint.com says this is valid:
---
foo: "bar"
baz:
- "qux"
- "quxx"
corge: null
grault: 1
garply: true
waldo: "false"
fred: "undefined"
emptyArray: []
emptyObject: {}
emptyString: ""
yamljs says
{ message: "Unable to parse."
, parsedFile: null
, parsedLine: 2
, rawMessage: "Unable to parse."
, snippet: " foo: \"bar\""
}
I don't know which is telling the truth.
if a comment is included within an indented structure, but is not indented to the same level as the surrounding structure, "Unable to parse" is the result:
If I remove the 2nd comment below, the yaml is parsed correctly.
---
Version: 3
parts:
a: [ 1,2,3]
b: [4,5,6]
# this comment is ok
c: ["a","b"]
# this comment is not ok
d: [3,4,2]
Given the following JSON object:
{
"emptyArray": [],
"fullArray": [1, 2, 3],
"emptyObject": {},
"fullObject": {
"key": "value"
},
"integer": 7,
"string": "hello World!"
}
if I use YAML.stringify(data, 1, 4);
I get the following output:
emptyArray: []
fullArray: [1, 2, 3]
emptyObject: {}
fullObject: {key: value}
integer: 7
string: 'hello World!'
This is the expected result and allows me to use YAML.parse()
to switch back to a JSON object and get the same JSON back just fine. However, if I use YAML.stringify(data, Infinity, 4);
to expand the YAML as far as it can (which I need for my project since I have a deeply nested JSON object that I am trying to edit in YAML) I get the following:
emptyArray: []
fullArray:
- 1
- 2
- 3
emptyObject:
fullObject:
key: value
integer: 7
string: 'hello World!'
This causes a problem when parsing the YAML back to JSON since the emptyObject
now parses back to null
instead of an emptyObject {}
:
{
"emptyArray": [],
"fullArray": [
1,
2,
3
],
"emptyObject": null,
"fullObject": {
"key": "value"
},
"integer": 7,
"string": "hello World!"
}
While I realize that the Infinity
keyword is used in the command to say that all JSON objects should be expanded, the resulting YAML is not equivalent when expanding an empty JSON object. The expected YAML output would be as follows:
emptyArray: []
fullArray:
- 1
- 2
- 3
emptyObject: {}
fullObject:
key: value
integer: 7
string: 'hello World!'
I just ran yaml2json -s -r -w -o json yaml
. I expected it to transform all of the yaml files in yaml
into JSON files in json
. However, instead the files were created in json/yaml
.
In other words: yaml/foo.yaml -> json/foo.json
expected, yaml/foo.yaml -> json/yaml/foo.json
actual.
YamlRunningUnderNode
is true
whenever exports
and module.expors
are present, but using pakmanager
, browserify
, and other CommonJS packaging systems for the browser those are also present.
I'd suggest sniffing process.versions
or something more node specific.
P.S. The projects I'm working on
You should move coffeeify
, jasmine-node
and benchmark
to devDeps?
Some data or content formats use YAML as a header. Some examples:
It would be very good to let yamljs auto-detect YAML blocks.
Thanks!
Hi,
I'm trying to serialize an object with a certain depth but the result is that yaml is renderer only until the 2nd level of depth while the deeper one are returned as js objects
Does this work as expected?
Example code
var obj = {
a: {
b: {
c: true
}
},
b: {
a: {
c: false
}
}
};
console.warn(yamljs.stringify(obj));
expected result
a:
b:
c: true
b:
a:
c: false
actual result
a:
b: {c: true}
b:
a: {c: false}
I'm not sure if this is a matter of YAML spec, or just a difference between other parsers, but yamljs appears to require extra leading spaces for nested mappings.
---
test:
- a:
- b:
- one
yaml2json returns the following:
Unable to parse in "it.yaml" at line 5 (near " - one").
Compared to pyyaml which parses ok.
The required spacing appears to depend on the level of nesting. For instance this works (further indented -a)
---
test:
- a:
- b:
- one
But this returns "Unable to parse".
---
test:
- a:
- b:
- c:
- one
this yml
options:
- list: foo
url: bar
tasks:
- Task: 5559
Title: New task
Description: |
Added comment....
Multiple lines
TaskStatusValue: Not Started
parses on http://yaml-online-parser.appspot.com/
but crashes out using yamljs with the error
TypeError: vaule.indexOf is not a function
When parsing a multiline string containing backslashes, they are replaced by "Undefined".
#!/usr/bin/env coffee
yaml = require 'yamljs'
fs = require 'fs'
testStr = "string 1\nstring 2 with \\ inside"
console.log testStr
console.log yaml.stringify(testStr)
string 1
string 2 with \ inside
"string 1\nstring 2 with undefined inside"
Below is some hacked-together code to run this parser against the tests. 59 tests pass, 45 fail because the library parses but gets the wrong result, and 11 fail because the library throws an error.
That's a pretty dire failure rate. By contrast, yaml-js
, which seems to have half this library's popularity in terms of downloads per month, passes all the tests.
I'm not very familiar with YAML; am I missing anything in my analysis here? Is there any reason for this library to continue existing given the extent to which it fails to meet spec (and yaml-js
does)? If not, would it be reasonable to pull it from GitHub and npm so that traffic can go to yaml-js
instead?
var https = require('https'),
YAML = require('yamljs');
function runTests (tests) {
for (key in tests) {
var testSuite = tests[key];
for (key in testSuite) {
var test = testSuite[key];
try {
if (JSON.stringify(YAML.parse(test['yaml'])) != JSON.stringify(test['result'])) {
console.log('FAIL', test);
}
else {
console.log('PASS', test);
}
}
catch (e) {
console.log('EXCEPTION', test);
}
}
}
}
var request = require("request")
var url = "https://raw.githubusercontent.com/connec/yaml-spec/master/spec.json";
request({
url: url,
json: true
}, function (error, response, body) {
runTests(body);
})
Covert from json to yaml where json key is empty string and vice verse.
e.g.
{
"": "test"
}
When I try to convert to yaml I get this result:
: test
When I try to convert converted yaml back to json I get this:
{
"test"
}
According to yamllint.com (and to the spec as far as I understand it) this is valid YAML:
foo
bar: |
baz
And this also:
- foo
bar
yamljs stumbles on both with parse errors. Also on combinations of both (which is how I noticed):
- really very long text for a map key where one could reasonably expect
a line-break:
- some more stuff
- and yet more stuff which is also very long-winded and requires a
line-break
Tested with yamljs 0.2.2
This fails in yaml.js, but is valid YAML.
- sequence1:
- long_string_in_unindented_sequence: |
line1
line2
- name: something
it should yield this:
[
{
"sequence1": [
{
"long_string_in_unindented_sequence": "line1\nline2\n"
},
{
"name": "something"
}
]
}
]
I'm implementing a build system where I start with a single "master" YAML file, and then with various gulp tasks, other versions of the file are created. There is some meta-information in the "master" YAML file that indicates, based on which gulp task is used, sections to either leave in or omit.
So, I'm using yaml.js to load the master YAML, then use JS to parse through the structure, identify sections that should be added or omitted, and then using stringify
to create the resulting output. The issue is, I have some very long strings in the master YAML file that end up being output onto a single line in the resulting YAML. Unfortunately, this causes certain text editors to hang (cough Atom cough), but more importantly it makes whatever that string basically unmaintainable (because it cannot easily be edited).
I have used the YAML >+
operator for multi-line strings in the master YAML, and an option to force stringify
to do the same and use the line breaks that are included in the string to make the line breaks in the block string. Apologies if something like this is already included. Thanks for the great tool!
Hi guys, I have just installed and used yamljs
for my project. I also use Webpack for my project. When compiling, everything works just fine but there were a few warnings.
WARNING in ./~/yamljs/lib/Yaml.js
require.extensions is not supported by webpack. Use a loader instead.
WARNING in ./~/yamljs/lib/Yaml.js
Critical dependencies:
78:43-50 require function is used in a way in which dependencies cannot be statically extracted
@ ./~/yamljs/lib/Yaml.js 78:43-50
Why did this happen? Is there a way to ignore or fix this?
Here is my Webpack configuration:
const webpack = require('webpack');
const path = require('path');
const nodeENV = process.env.NODE_ENV; // can either be 'development' or 'production'
const isProd = nodeENV === 'production';
let config = {
entry: [
'./app/index.js'
],
output: {
path: path.join(__dirname, 'app', 'build'),
filename: 'bundle.js'
},
node: {
__filename: true,
__dirname: true,
"child_process": "empty"
},
target: 'node',
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' },
{ test: /\.json$/, loader: 'json-loader' }
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': { 'NODE_ENV' : JSON.stringify(nodeENV) }
})
]
};
module.exports = config;
Thanks in advance! 😃
If you call YAML.parse(myObject);
passing it an object it blows up.
TypeError: undefined is not a function
at Parser.cleanup (/Users//code/tag-validation-lambda/node_modules/yamljs/lib/Parser.js:532:15)
at Parser.parse (/Users//code/tag-validation-lambda/node_modules/yamljs/lib/Parser.js:65:23)
at Function.Yaml.parse (/Users//code/tag-validation-lambda/node_modules/yamljs/lib/Yaml.js:20:25)
at repl:1:6
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
I am just proposing that maybe this could be idempotent and simply return any thing that is already an object instead of a getting a stacktrace.
When parsing yaml with new line characters, eg:
https://github.com/openshift-qe/v3-testfiles/blob/master/image-streams/image-source.yaml#L80
https://github.com/openshift-qe/v3-testfiles/blob/master/image-streams/image-source.yaml#L124-L125
I get back ParseException with message "You cannot define a sequence item when in a mapping", even if its a valid yaml format due to http://www.yamllint.com/
Hi,
Consider the next string:
"--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess unit_id: - - 3 role: - disabled - regular user_id: - - 14 id: - - 42"
Or structured:
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
unit_id:
-
- 2
role:
- disabled
- regular
user_id:
-
- 11
id:
-
- 32
While YAML.load in Ruby successfully parses it to JSON, yamljs fails somwhere.
It should generate null attributes where an empty line is found.
when you call .stringify
on an object that has a Date property this happens (from coffescript repl, I've checked it into node as well)
coffee> YAML = require 'yamljs'
...
coffee> obj = date: new Date(), ary: [1,2,3], foo: 'bar'
{ date: Sun Jun 05 2016 00:11:59 GMT+0200 (CEST),
ary: [ 1, 2 ],
foo: 'bar' }
coffee> YAML.stringify obj
'date:\n 2016-06-04T22:11:59.576Zary:\n - 1\n - 2\nfoo: bar\n'
coffee>
so as you can see there's an extra \n
after date
and it's missing the newline after the date string
The version of glob
used by this library uses a vulnerable version of minimatch
as per this advisory: https://nodesecurity.io/advisories/118
I can't install a package that relies on this because of it, would be great if you could help. Thanks :)
The following...
y = require('yamljs');
console.log(y.parse(y.stringify({a:[]})));
...results in { a: null }
.
Obviously this is not right and it is causing errors in our app.
Since I don't work with node or coffeescript, I do not know much about all the loaders out there but the one you use here is certainly of doubtful quality when it comes to non-node contexts where require
is not available...
One such context I deal with is Firefox addons. It's useful to isolate script scopes completely when including them in Firefox to prevent global scope pollution. What is more, window object may not even be available here, and even if it is, it's probably a XUL window.
Long story short, { module: { exports: {} } }
is the minimal global object but your loader does not handle it at all.
Even if you do set YAML on this
and module.exports
on lines 1859+ they are not actually exported anywhere: both this
and module.exports
are references to l.exports
from var l=n[o]
.
Sure n[o].exports
is a return value for s
but it's not actually used:
for (var o = 0; o < r.length; o++)
s(r[o]);
return s
Having read all this minimized gibberish 'I can't even` is the only thing that comes to my mind.
The loader in js-yaml.js works just fine but there are other problems with that lib that prevents me from using it in an add-on.
foo: &foo
x: 3
bar1:
<<: *foo
y: 3
bar2: {
<<: *foo,
y: 3 }
It also works on this parser.
> yamljs.parse(`...`)
{ foo: { x: 3 },
bar1: { x: 3, y: 3 },
bar2: { '<<': '*foo', y: 3 } }
{ foo: { x: 3 },
bar1: { x: 3, y: 3 },
bar2: { x: 3, y: 3 } }
It's not simple trying to extend from Error. I have observed that errors from this module lack prototype
and stack
(which is very important for debugging). I've done some research and from jashkenas/coffeescript#2956 it seems that Coffeescript does not intend to fix that, though there's a suggestion there, maybe we can try that?
class MyError extends Error then constructor: -> super
Hi,
Is is possible to modify the YAML scope like in Mustache.js to make YAML variable accessible from even outside the browser and when i'm not using Node.js.
Example in Mustache.js:
https://github.com/janl/mustache.js/blob/master/mustache.js#L8
https://github.com/janl/mustache.js/blob/master/mustache.js#L14
Right now i'm modifying this line to get it work but it's ugly.
https://github.com/jeremyfa/yaml.js/blob/develop/dist/yaml.legacy.js#L390
with
// Handle all cases
this.YAML = YAML;
Hint : Maybe this file and recompile via Cakefile ?
Line 112 in 4408ae2
Could you help me ? Thanks
Regards
yaml.parse(
`2016-01-01 12:34:56.789: foo
2016-01-02 12:34:56.789: bar
`)
and
yaml.parse(
`? 2016-01-01 12:34:56.789
: foo
? 2016-01-02 12:34:56.789
: bar
`)
yield
{
'Fri Jan 01 2016 12:34:56 GMT+0000 (UTC)': 'foo',
'Sat Jan 02 2016 12:34:56 GMT+0000 (UTC)': 'bar'
}
but should yield
new Map([
[new Date('2016-01-01T12:34:56.789Z'): 'foo'],
[new Date('2016-01-02T12:34:56.789Z'): 'bar']
])
… or at least
{
'2016-01-01T12:34:56.789Z': 'foo',
'2016-01-01T12:34:56.789Z': 'bar'
}
or
{
'2016-01-01 12:34:56.789': 'foo',
'2016-01-01 12:34:56.789': 'bar'
}
Does this library support reading the YAML as a stream? I think that was one of the hypothetical advantages of YAML's design.
In the 1.2 yaml spec here, http://www.yaml.org/spec/1.2/spec.html , there is an example of mapping of mappings as follows (example 2.6):
Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
hr: 63,
avg: 0.288
}
When I try to parse this, I get an error:
Indentation problem
at line 5, the "}"
The file only contains the copyright header. The minified code is missing.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.