ruby-json.git  about / heads / tags
Unnamed repository; edit this file 'description' to name the repository.
blob 3c53183962a0296b250a20872239c42776c70fd5 1096 bytes (raw)
$ git show HEAD:bin/prettify_json.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
#!/usr/bin/env ruby

require 'json'
require 'fileutils'
include FileUtils
require 'spruz/go'
include Spruz::GO

opts = go 'slhi:', args = ARGV.dup
if opts['h'] || opts['l'] && opts['s']
  puts <<EOT
Usage: #{File.basename($0)} [OPTION] [FILE]

If FILE is skipped, this scripts waits for input from STDIN. Otherwise
FILE is opened, read, and used as input for the prettifier.

OPTION can be
  -s     to output the shortest possible JSON (precludes -l)
  -l     to output a longer, better formatted JSON (precludes -s)
  -i EXT prettifies FILE in place, saving a backup to FILE.EXT
  -h     this help
EOT
  exit 0
end

json_opts = { :max_nesting => false, :create_additions => false }

document =
  if filename = args.first or filename == '-'
    File.read(filename)
  else
    STDIN.read
  end

json = JSON.parse document, json_opts

output = if opts['s']
  JSON.fast_generate json, json_opts
else # default is -l
  JSON.pretty_generate json, json_opts
end

if opts['i'] && filename
  cp filename, "#{filename}.#{opts['i']}"
  File.open(filename, 'w') { |f| f.puts output }
else
  puts output
end

git clone https://yhbt.net/ruby-json.git