Class: Puppet::Pops::Serialization::JSON::Packer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/serialization/json.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The JSON Packer. Modeled after the MessagePack::Packer

Instance Method Summary collapse

Constructor Details

#initialize(io, options = {}) ⇒ Packer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Packer.



66
67
68
69
70
71
72
73
74
# File 'lib/puppet/pops/serialization/json.rb', line 66

def initialize(io, options = {})
  @io = io
  @io << '['
  @type_registry = {}
  @nested = []
  @verbose = options[:verbose]
  @verbose = false if @verbose.nil?
  @indent = options[:indent] || 0
end

Instance Method Details

#clear_ioObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
83
84
85
86
87
# File 'lib/puppet/pops/serialization/json.rb', line 80

def clear_io
  # Truncate everything except leading '['
  if @io.is_a?(String)
    @io.slice!(1..-1)
  else
    @io.truncate(1)
  end
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


89
90
91
# File 'lib/puppet/pops/serialization/json.rb', line 89

def empty?
  @io.is_a?(String) ? io.length == 1 : @io.pos == 1
end

#flushObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet/pops/serialization/json.rb', line 93

def flush
  # Drop last comma unless just start marker present
  if @io.is_a?(String)
    @io.chop! unless @io.length == 1
    @io << ']'
  else
    pos = @io.pos
    @io.pos = pos - 1 unless pos == 1
    @io << ']'
    @io.flush
  end
end

#register_type(type, klass, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



76
77
78
# File 'lib/puppet/pops/serialization/json.rb', line 76

def register_type(type, klass, &block)
  @type_registry[klass] = [type, klass, block]
end

#to_aObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
# File 'lib/puppet/pops/serialization/json.rb', line 149

def to_a
  ::Puppet::Util::Json.load(io_string)
end

#to_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



153
154
155
156
157
158
159
# File 'lib/puppet/pops/serialization/json.rb', line 153

def to_json
  if @indent > 0
    ::Puppet::Util::Json.dump(to_a, { :pretty => true, :indent => ' ' * @indent })
  else
    io_string
  end
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



145
146
147
# File 'lib/puppet/pops/serialization/json.rb', line 145

def to_s
  to_json
end

#write(obj) ⇒ Object Also known as: pack

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/puppet/pops/serialization/json.rb', line 106

def write(obj)
  case obj
  when Array
    write_array_header(obj.size)
    obj.each { |x| write(x) }
  when Hash
    write_map_header(obj.size)
    obj.each_pair { |k, v| write(k); write(v) }
  when nil
    write_nil
  else
    write_scalar(obj)
  end
end

#write_array_header(n) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
125
126
127
128
129
# File 'lib/puppet/pops/serialization/json.rb', line 122

def write_array_header(n)
  if n < 1
    @io << '[]'
  else
    @io << '['
    @nested << [false, n]
  end
end

#write_map_header(n) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



131
132
133
134
135
136
137
138
# File 'lib/puppet/pops/serialization/json.rb', line 131

def write_map_header(n)
  if n < 1
    @io << '{}'
  else
    @io << '{'
    @nested << [true, n * 2]
  end
end

#write_nilObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



140
141
142
143
# File 'lib/puppet/pops/serialization/json.rb', line 140

def write_nil
  @io << 'null'
  write_delim
end

#write_pl(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Write a payload object. Not subject to extensions



162
163
164
# File 'lib/puppet/pops/serialization/json.rb', line 162

def write_pl(obj)
  @io << Puppet::Util::Json.dump(obj) << ','
end