Skip to content

Instantly share code, notes, and snippets.

@andrei512
Last active December 19, 2015 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrei512/6007228 to your computer and use it in GitHub Desktop.
Save andrei512/6007228 to your computer and use it in GitHub Desktop.
converts JSONish structures from ruby to objective-c code
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