Skip to content

Commit

Permalink
Add as_json method to resource so it properly serializes
Browse files Browse the repository at this point in the history
to json when activesupport is included
  • Loading branch information
mmzyk committed Mar 6, 2012
1 parent dd7b3c1 commit fcbf713
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions chef/lib/chef/resource.rb
Expand Up @@ -346,8 +346,10 @@ def inspect
end << ">"
end

# Serialize this object as a hash
def to_json(*a)
# as_json does most of the to_json heavy lifted. It exists here in case activesupport
# is loaded. activesupport will call as_json and skip over to_json. This ensure
# json is encoded as expected
def as_json(*a)
safe_ivars = instance_variables.map { |ivar| ivar.to_sym } - FORBIDDEN_IVARS
instance_vars = Hash.new
safe_ivars.each do |iv|
Expand All @@ -357,6 +359,11 @@ def to_json(*a)
'json_class' => self.class.name,
'instance_vars' => instance_vars
}
end

# Serialize this object as a hash
def to_json(*a)
results = as_json
results.to_json(*a)
end

Expand Down

0 comments on commit fcbf713

Please sign in to comment.