Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active April 16, 2021 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/3e4adb0a3eb092f0ec449ddd05a031c1 to your computer and use it in GitHub Desktop.
Save JoshCheek/3e4adb0a3eb092f0ec449ddd05a031c1 to your computer and use it in GitHub Desktop.
Testing GH JSON / JSON5 / YAML highlighting

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"
}
{
"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"
}
{
// 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",
}
{
# 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",
}
source 'https://rubygems.org'
gem 'json5'
GEM
remote: https://rubygems.org/
specs:
json5 (0.0.1)
PLATFORMS
arm64-darwin-20
DEPENDENCIES
json5
BUNDLED WITH
2.2.14
require 'json'
require 'json5'
require 'yaml'
json = JSON.parse File.read 'example.json'
json5 = JSON5.parse File.read 'example.json5'
yaml = YAML.safe_load File.read 'example.yml'
if json == json5 && json == yaml
puts "All examples are equivalent:"
else
$stderr.puts "ERROR: EXAMPLES ARE NOT EQUIVALENT!"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment