Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/active_resource/formats/json_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def encode(resource, options = nil)
resource.to_json(options)
end

def decode(json)
def decode(json, remove_root = true)
return nil if json.nil?
Formats.remove_root(ActiveSupport::JSON.decode(json))
hash = ActiveSupport::JSON.decode(json)
remove_root ? Formats.remove_root(hash) : hash
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/active_resource/formats/xml_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def encode(resource, options = {})
resource.to_xml(options)
end

def decode(xml)
Formats.remove_root(Hash.from_xml(xml))
def decode(xml, remove_root = true)
hash = Hash.from_xml(xml)
remove_root ? Formats.remove_root(hash) : hash
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/active_resource/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def from_hash(messages, save_cache = false)

# Grabs errors from a json response.
def from_json(json, save_cache = false)
decoded = ActiveSupport::JSON.decode(json) || {} rescue {}
decoded = Formats[:json].decode(json, false) || {} rescue {}
errors = decoded["errors"] || {}
from_hash errors, save_cache
end

# Grabs errors from an XML response.
def from_xml(xml, save_cache = false)
array = Array.wrap(Hash.from_xml(xml)["errors"]["error"]) rescue []
array = Array.wrap(Formats[:xml].decode(xml, false)["errors"]["error"]) rescue []
from_array array, save_cache
end
end
Expand Down