Class: Puppet::Pops::Serialization::JSON::Unpacker

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

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Unpacker

Returns a new instance of Unpacker.



243
244
245
246
247
# File 'lib/puppet/pops/serialization/json.rb', line 243

def initialize(io)
  re_initialize(io)
  @type_registry = {}
  @nested = []
end

Instance Method Details

#re_initialize(io) ⇒ Object

Raises:



249
250
251
252
253
# File 'lib/puppet/pops/serialization/json.rb', line 249

def re_initialize(io)
  parsed = parse_io(io)
  raise SerializationError, _("JSON stream is not an array. It is a %{klass}") % { klass: io.class.name } unless parsed.is_a?(Array)
  @etor_stack = [parsed.each]
end

#readObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/puppet/pops/serialization/json.rb', line 255

def read
  obj = nil
  loop do
    raise SerializationError, _('Unexpected end of input') if @etor_stack.empty?
    etor = @etor_stack.last
    begin
      obj = etor.next
      break
    rescue StopIteration
      @etor_stack.pop
    end
  end
  if obj.is_a?(Array)
    ext_etor = obj.each
    @etor_stack << ext_etor
    ext_no = ext_etor.next
    ext_block = @type_registry[ext_no]
    raise SerializationError, _("Invalid input. %{ext_no} is not a valid extension number") % { ext_no: ext_no } if ext_block.nil?
    obj = ext_block.call(nil)
  end
  obj
end

#register_type(extension_number, &block) ⇒ Object



278
279
280
# File 'lib/puppet/pops/serialization/json.rb', line 278

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