Last active
December 19, 2015 14:49
-
-
Save nanki/5972111 to your computer and use it in GitHub Desktop.
convert JSON to Property List.
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 | |
# -*- coding: UTF-8 -*-; | |
require 'base64' | |
require 'pathname' | |
require 'english' | |
require 'json' | |
require 'CFPropertyList' | |
Encoding.default_internal = "UTF-8" | |
Encoding.default_external = "UTF-8" | |
dict = JSON.load ARGF | |
def visit_value(obj, &block) | |
case obj | |
when Hash | |
obj.each do |k, v| | |
obj[k] = visit_value(v, &block) | |
end | |
when Array | |
obj.map! do |v| | |
visit_value(v, &block) | |
end | |
else | |
yield obj | |
end | |
end | |
visit_value(dict) do |value| | |
case value | |
when /\Adata:;base64,/ | |
CFPropertyList::Blob.new Base64.decode64 $POSTMATCH | |
when /\Afile:\/\// | |
open(Pathname(ARGF.filename).dirname + $POSTMATCH, "rb") | |
else | |
value | |
end | |
end | |
plist = CFPropertyList::List.new | |
plist.value = CFPropertyList::guess dict | |
data = plist.to_str CFPropertyList::List::FORMAT_XML | |
print data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment