Created
June 22, 2022 13:22
-
-
Save kevindayton/0387dfedf04a92d7010d0b8f04677d6b to your computer and use it in GitHub Desktop.
JsonWithRootFormat (ARes customer encoder/decoder)
This file contains 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
module ActiveResource | |
module Formats | |
module JsonWithRootFormat | |
extend self | |
def extension | |
"json" | |
end | |
def mime_type | |
"application/json" | |
end | |
def encode(hash, options={}) | |
hash.to_json(options) | |
end | |
def decode(json) | |
decoded = ActiveSupport::JSON.decode(json) | |
if json['data'] | |
decoded = decoded['data'].map do | d | | |
d.transform_keys do |k| | |
k.to_s.underscore | |
end | |
end | |
else | |
decoded = decoded.transform_keys do |k| | |
k.to_s.underscore | |
end | |
end | |
decoded | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment