Created
April 5, 2013 10:30
-
-
Save xhan/5318278 to your computer and use it in GitHub Desktop.
simple script to convert between JSON and YAML
This file contains 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 | |
def usage | |
puts "USAGE: binary file.json/yaml [json/yaml] " | |
true | |
end | |
require 'yaml' | |
require 'json' | |
TYPE_JSON, TYPE_YAML = ['json','yaml'] | |
def main | |
file , type = ARGV | |
content = nil | |
usage and return unless file | |
# get type by file extension if not defined | |
type = file.split('.').last unless type | |
type.downcase! if type | |
unless type or [TYPE_JSON,TYPE_YAML ].include? type.downcase | |
usage and return | |
end | |
case type | |
when TYPE_YAML | |
content = YAML::load( File.open(file) ) #.to_json | |
content = JSON.pretty_generate( content ) | |
when TYPE_JSON | |
content = JSON::load( File.open(file) ).to_yaml | |
end | |
puts content | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment