Sha256: b9e4c3d309877b2c678996b0f1c4b60533b072b5292931e2f216b8f8c077dc0d

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

#!/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

Version data entries

7 entries across 7 versions & 5 rubygems

Version Path
json_pure-1.5.5 bin/prettify_json.rb
json-1.5.5 bin/prettify_json.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/json-1.5.4/bin/prettify_json.rb
json-maglev--1.5.4 bin/prettify_json.rb
json-utils-1.6.0 bin/prettify_json.rb
json_pure-1.5.4 bin/prettify_json.rb
json-1.5.4 bin/prettify_json.rb