Created
January 24, 2012 23:10
-
-
Save pete/1673382 to your computer and use it in GitHub Desktop.
JSON pretty-printer. Not sophisticated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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