1 module Puppet::Parser::Functions
2 newfunction(:loadjson, :type => :rvalue, :arity => -2, :doc => <<-'ENDHEREDOC') do |args|
3 Load a JSON file containing an array, string, or hash, and return the data
4 in the corresponding native data type.
5 The second parameter is the default value. It will be returned if the file
6 was not found or could not be parsed.
10 $myhash = loadjson('/etc/puppet/data/myhash.json')
11 $myhash = loadjson('no-file.json', {'default' => 'value'})
14 raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1
16 if File.exists?(args[0])
18 content = File.read(args[0])
19 PSON::load(content) || args[1]
28 warning("Can't load '#{args[0]}' File does not exist!")