Last active
June 29, 2017 16:34
-
-
Save yaauie/5724664 to your computer and use it in GitHub Desktop.
JSON-decode recursively until it isn't JSON anymore.
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
# 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