Skip to content

Instantly share code, notes, and snippets.

@yaauie

yaauie/jason-voorhees.rb

Last active Jun 29, 2017
Embed
What would you like to do?
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