Last active
December 19, 2015 19:38
-
-
Save andrei512/6007228 to your computer and use it in GitHub Desktop.
converts JSONish structures from ruby to objective-c code
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
require 'json' | |
def tabspaces indent_level | |
return " " * indent_level | |
end | |
def to_objc object, indent_level = 0, ignore_first = false | |
first_indent = ignore_first ? "" : tabspaces(indent_level) | |
indent = tabspaces indent_level | |
if object.is_a? Array | |
return "#{first_indent}@[\n" + | |
object.map { |item| | |
to_objc(item, indent_level + 1) | |
}.join(",\n") + | |
"\n" + | |
"#{indent}]" | |
elsif object.is_a? Hash | |
return "#{first_indent}@{\n" + | |
object.keys.map { |key| | |
"#{tabspaces(indent_level + 1)}#{to_objc(key)} : #{to_objc(object[key], indent_level + 1, true)}" | |
}.join(",\n") + | |
"\n" + | |
"#{indent}}" | |
elsif object.is_a? String | |
return "#{first_indent}@\"#{object}\"" | |
elsif object == nil | |
return "#{first_indent}[NSNull null]" | |
else | |
return "#{first_indent}@(#{object})" | |
end | |
end | |
unless $stdin.tty? | |
input = $stdin.read | |
data = JSON.parse(input) | |
puts to_objc(data) | |
else | |
puts "no input." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment