Skip to content

Instantly share code, notes, and snippets.

@yaauie
Last active June 29, 2017 16:34
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 yaauie/5724664 to your computer and use it in GitHub Desktop.
Save yaauie/5724664 to your computer and use it in GitHub Desktop.
JSON-decode recursively until it isn't JSON anymore.
# encoding: utf-8
require 'json'
# For when someone puts JSON in your JSON
# (because MurderingRampage™)
class JasonVoorhees
def self.load(*args)
self.new(JSON).load(*args)
end
def initialize(coder = JSON)
@coder = coder
end
def load(thing)
case thing
when String
load(@coder.load(thing)) rescue thing
when Hash
thing.each_with_object({}) do |(key, value), memo|
memo[key] = load(value)
end
when Array
thing.map do |value|
load(value)
end
else
thing
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment