summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-05-06 14:32:25 -0700
committerJames Turnbull <james@lovedthanlost.net>2009-06-06 19:53:35 +1000
commit7f322b315a7890f1b3cee035d023f09814cc6075 (patch)
tree014201a062017a3a13f4c3e6c547f8d0a309f1d8 /lib
parent7666597edc335055275569f5626a897453402d5b (diff)
downloadpuppet-7f322b315a7890f1b3cee035d023f09814cc6075.tar.gz
puppet-7f322b315a7890f1b3cee035d023f09814cc6075.tar.xz
puppet-7f322b315a7890f1b3cee035d023f09814cc6075.zip
Adding a JSON format
Also making some log messages more informative.
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/network/format.rb2
-rw-r--r--lib/puppet/network/formats.rb33
2 files changed, 34 insertions, 1 deletions
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb
index 5ed120ba6..dbbb9a842 100644
--- a/lib/puppet/network/format.rb
+++ b/lib/puppet/network/format.rb
@@ -117,7 +117,7 @@ class Puppet::Network::Format
return true if has_method
- Puppet.debug "Format %s not supported for %s; %s" % [name, klass, message]
+ Puppet.debug "Format %s not supported for %s; %s" % [self.name, klass, message]
return false
end
end
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb
index 51354b03d..3a19b0bba 100644
--- a/lib/puppet/network/formats.rb
+++ b/lib/puppet/network/formats.rb
@@ -88,3 +88,36 @@ Puppet::Network::FormatHandler.create(:raw, :mime => "application/x-raw", :weigh
true
end
end
+
+Puppet::Network::FormatHandler.create(:json, :mime => "text/json", :weight => 10, :required_methods => [:render_method, :intern_method]) do
+ confine :true => Puppet.features.json?
+
+ def intern(klass, text)
+ data_to_instance(klass, JSON.parse(text))
+ end
+
+ def intern_multiple(klass, text)
+ JSON.parse(text).collect do |data|
+ data_to_instance(klass, data)
+ end
+ end
+
+ # JSON monkey-patches Array, so this works.
+ def render_multiple(instances)
+ instances.to_json
+ end
+
+ # If they pass class information, we want to ignore it. By default,
+ # we'll include class information but we won't rely on it - we don't
+ # want class names to be required because we then can't change our
+ # internal class names, which is bad.
+ def data_to_instance(klass, data)
+ if data.is_a?(Hash) and d = data['data']
+ data = d
+ end
+ if data.is_a?(klass)
+ return data
+ end
+ klass.from_json(data)
+ end
+end