Skip to content

Instantly share code, notes, and snippets.

@pete
Created January 24, 2012 23:10
Show Gist options
  • Save pete/1673382 to your computer and use it in GitHub Desktop.
Save pete/1673382 to your computer and use it in GitHub Desktop.
JSON pretty-printer. Not sophisticated.
#! /usr/bin/env ruby
require 'json'
require 'pp'
mode = :ruby
ARGV.each_with_index { |arg, i|
next unless arg.start_with? ?-
ARGV.delete_at i
case arg
when '-j'
mode = :json
when '-r'
mode = :ruby
when '--'
break
else
$stderr.puts "Un-understood option: #{arg}"
break
end
}
content = ARGF.read
parsed =
begin
JSON.parse content
rescue JSON::ParserError => e
$stderr.puts e.inspect
mode = :nop
content
end
case mode
when :nop
print parsed
when :ruby
pp parsed
when :json
puts JSON.pretty_unparse(parsed)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment