Showing that YAML can do everything JSON5 can do. And if you're in Ruby, then you get to avoid a gem dependency, too, since YAML ships with the stdlib. The example from the JSON5 website comes out exactly the same when parsed by YAML, except that the comment character needs to be changed.
This is a self-generated document, expand this to see the code.
# This document lives at https://gist.github.com/JoshCheek/3e4adb0a3eb092f0ec449ddd05a031c1
#! ruby -x README.md
File.write __FILE__, [
File.read(__FILE__)[/\A.*?__END__\n/m] << # preamble (everything upto the __END__)
"\x3c/code>\x3c/pre>\x3c/details>", # hide (details) & prettyify (code/pre) this generation code
`ruby test.rb`.tap { exit 1 unless $?.success? }, # test they're equivalent, embed output on success
*Dir['example.*'].map { |f| "**#{f}**\n```#{f[/(?<=\.).*/]}\n#{File.read f}```" }, # embed examples
].join("\n\n")
__END__
All examples are equivalent:
example.yml
{
# comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}
example.json5
{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}
example.json
{
"unquoted": "and you can quote me on that",
"singleQuotes": "I can use \"double quotes\" here",
"lineBreaks": "Look, Mom! No \\n's!",
"hexadecimal": 912559,
"leadingDecimalPoint": 0.8675309, "andTrailing": 8675309,
"positiveSign": 1,
"trailingComma": "in objects", "andIn": ["arrays"],
"backwardsCompatible": "with JSON"
}