Puppet Function: stdlib::to_json_pretty
- Defined in:
-
lib/puppet/functions/stdlib/to_json_pretty.rb
- Function type:
- Ruby 4.x API
Summary
Convert data structure and output to pretty JSON
Overview
stdlib::to_json_pretty(Variant[Hash, Array] $data, Optional[Optional[Boolean]] $skip_undef, Optional[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) ⇒ Any
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
|
# File 'lib/puppet/functions/stdlib/to_json_pretty.rb', line 35
Puppet::Functions.create_function(:'stdlib::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 = opts.transform_keys(&:to_sym)
opts[:max_nesting] = false if opts[:max_nesting] == -1
end
data = data.compact if skip_undef && (data.is_a?(Array) || Hash)
JSON.pretty_generate(data, opts) << "\n"
end
end
|