35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'vendor_modules/stdlib/lib/puppet/functions/to_json_pretty.rb', line 35
Puppet::Functions.create_function(:to_json_pretty) do
dispatch :to_json_pretty do
param 'Variant[Hash, Array]', :data
optional_param 'Optional[Boolean]', :skip_undef
optional_param 'Struct[{
indent => Optional[String],
space => Optional[String],
space_before => Optional[String],
object_nl => Optional[String],
array_nl => Optional[String],
allow_nan => Optional[Boolean],
max_nesting => Optional[Integer[-1,default]],
}]', :opts
end
def to_json_pretty(data, skip_undef = false, opts = nil)
if opts
opts = Hash[opts.map { |k, v| [k.to_sym, v] }]
if opts[:max_nesting] == -1
opts[:max_nesting] = false
end
end
if skip_undef
if data.is_a? Array
data = data.reject { |value| value.nil? }
elsif data.is_a? Hash
data = data.reject { |_, value| value.nil? }
end
end
::JSON.pretty_generate(data, opts) << "\n"
end
end
|