class Puppet::Pops::Serialization::Serializer
The serializer is capable of writing, arrays, maps, and complex objects using an underlying protocol writer. It takes care of tabulating and disassembling complex objects. @api public
Attributes
Provides access to the writer. @api private
Public Class Methods
@param writer [AbstractWriter] the writer that is used for writing primitive values @param options [{String, Object}] serialization options @option options [Boolean] :type_by_reference `true` if Object types are serialized by name only. @api public
# File lib/puppet/pops/serialization/serializer.rb 18 def initialize(writer, options = EMPTY_HASH) 19 @written = {} 20 @writer = writer 21 @options = options 22 end
Public Instance Methods
Tell the underlying writer to finish @api public
# File lib/puppet/pops/serialization/serializer.rb 26 def finish 27 @writer.finish 28 end
# File lib/puppet/pops/serialization/serializer.rb 96 def inspect 97 to_s 98 end
# File lib/puppet/pops/serialization/serializer.rb 78 def push_written(value) 79 @written[value.object_id] = @written.size 80 end
Write the start of an array. @param [Integer] size the size of the array @api private
# File lib/puppet/pops/serialization/serializer.rb 52 def start_array(size) 53 @writer.write(Extension::ArrayStart.new(size)) 54 end
Write the start of a map (hash). @param [Integer] size the number of entries in the map @api private
# File lib/puppet/pops/serialization/serializer.rb 59 def start_map(size) 60 @writer.write(Extension::MapStart.new(size)) 61 end
Write the start of a complex object @param [Integer] attr_count the number of attributes in the object @api private
# File lib/puppet/pops/serialization/serializer.rb 74 def start_object(attr_count) 75 @writer.write(Extension::ObjectStart.new(attr_count)) 76 end
Write the start of a complex pcore object @param [String] type_ref the name of the type @param [Integer] attr_count the number of attributes in the object @api private
# File lib/puppet/pops/serialization/serializer.rb 67 def start_pcore_object(type_ref, attr_count) 68 @writer.write(Extension::PcoreObjectStart.new(type_ref, attr_count)) 69 end
Write the start of a sensitive object @api private
# File lib/puppet/pops/serialization/serializer.rb 84 def start_sensitive 85 @writer.write(Extension::SensitiveStart::INSTANCE) 86 end
# File lib/puppet/pops/serialization/serializer.rb 92 def to_s 93 "#{self.class.name} with #{@writer}" 94 end
# File lib/puppet/pops/serialization/serializer.rb 88 def type_by_reference? 89 @options[:type_by_reference] == true 90 end
Write an object @param [Object] value the object to write @api public
# File lib/puppet/pops/serialization/serializer.rb 33 def write(value) 34 case value 35 when Integer, Float, String, true, false, nil 36 @writer.write(value) 37 when :default 38 @writer.write(Extension::Default::INSTANCE) 39 else 40 index = @written[value.object_id] 41 if index.nil? 42 write_tabulated_first_time(value) 43 else 44 @writer.write(Extension::Tabulation.new(index)) 45 end 46 end 47 end
First time write of a tabulated object. This means that the object is written and then remembered. Subsequent writes of the same object will yield a write of a tabulation index instead. @param [Object] value the value to write @api private
# File lib/puppet/pops/serialization/serializer.rb 104 def write_tabulated_first_time(value) 105 case 106 when value.instance_of?(Symbol), 107 value.instance_of?(Regexp), 108 value.instance_of?(SemanticPuppet::Version), 109 value.instance_of?(SemanticPuppet::VersionRange), 110 value.instance_of?(Time::Timestamp), 111 value.instance_of?(Time::Timespan), 112 value.instance_of?(Types::PBinaryType::Binary), 113 value.is_a?(URI) 114 push_written(value) 115 @writer.write(value) 116 when value.instance_of?(Array) 117 push_written(value) 118 start_array(value.size) 119 value.each { |elem| write(elem) } 120 when value.instance_of?(Hash) 121 push_written(value) 122 start_map(value.size) 123 value.each_pair { |key, val| write(key); write(val) } 124 when value.instance_of?(Types::PSensitiveType::Sensitive) 125 start_sensitive 126 write(value.unwrap) 127 when value.instance_of?(Types::PTypeReferenceType) 128 push_written(value) 129 @writer.write(value) 130 when value.is_a?(Types::PuppetObject) 131 value._pcore_type.write(value, self) 132 else 133 impl_class = value.class 134 type = Loaders.implementation_registry.type_for_module(impl_class) 135 raise SerializationError, _("No Puppet Type found for %{klass}") % { klass: impl_class.name } unless type.is_a?(Types::PObjectType) 136 type.write(value, self) 137 end 138 end