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 17 def initialize(writer, options = EMPTY_HASH) 18 @written = {} 19 @writer = writer 20 @options = options 21 end
Public Instance Methods
Tell the underlying writer to finish @api public
# File lib/puppet/pops/serialization/serializer.rb 25 def finish 26 @writer.finish 27 end
# File lib/puppet/pops/serialization/serializer.rb 95 def inspect 96 to_s 97 end
# File lib/puppet/pops/serialization/serializer.rb 77 def push_written(value) 78 @written[value.object_id] = @written.size 79 end
Write the start of an array. @param [Integer] size the size of the array @api private
# File lib/puppet/pops/serialization/serializer.rb 51 def start_array(size) 52 @writer.write(Extension::ArrayStart.new(size)) 53 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 58 def start_map(size) 59 @writer.write(Extension::MapStart.new(size)) 60 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 73 def start_object(attr_count) 74 @writer.write(Extension::ObjectStart.new(attr_count)) 75 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 66 def start_pcore_object(type_ref, attr_count) 67 @writer.write(Extension::PcoreObjectStart.new(type_ref, attr_count)) 68 end
Write the start of a sensitive object @api private
# File lib/puppet/pops/serialization/serializer.rb 83 def start_sensitive 84 @writer.write(Extension::SensitiveStart::INSTANCE) 85 end
# File lib/puppet/pops/serialization/serializer.rb 91 def to_s 92 "#{self.class.name} with #{@writer}" 93 end
# File lib/puppet/pops/serialization/serializer.rb 87 def type_by_reference? 88 @options[:type_by_reference] == true 89 end
Write an object @param [Object] value the object to write @api public
# File lib/puppet/pops/serialization/serializer.rb 32 def write(value) 33 case value 34 when Integer, Float, String, true, false, nil 35 @writer.write(value) 36 when :default 37 @writer.write(Extension::Default::INSTANCE) 38 else 39 index = @written[value.object_id] 40 if index.nil? 41 write_tabulated_first_time(value) 42 else 43 @writer.write(Extension::Tabulation.new(index)) 44 end 45 end 46 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 103 def write_tabulated_first_time(value) 104 case 105 when value.instance_of?(Symbol), 106 value.instance_of?(Regexp), 107 value.instance_of?(SemanticPuppet::Version), 108 value.instance_of?(SemanticPuppet::VersionRange), 109 value.instance_of?(Time::Timestamp), 110 value.instance_of?(Time::Timespan), 111 value.instance_of?(Types::PBinaryType::Binary), 112 value.is_a?(URI) 113 push_written(value) 114 @writer.write(value) 115 when value.instance_of?(Array) 116 push_written(value) 117 start_array(value.size) 118 value.each { |elem| write(elem) } 119 when value.instance_of?(Hash) 120 push_written(value) 121 start_map(value.size) 122 value.each_pair { |key, val| write(key); write(val) } 123 when value.instance_of?(Types::PSensitiveType::Sensitive) 124 start_sensitive 125 write(value.unwrap) 126 when value.instance_of?(Types::PTypeReferenceType) 127 push_written(value) 128 @writer.write(value) 129 when value.is_a?(Types::PuppetObject) 130 value._pcore_type.write(value, self) 131 else 132 impl_class = value.class 133 type = Loaders.implementation_registry.type_for_module(impl_class) 134 raise SerializationError, _("No Puppet Type found for %{klass}") % { klass: impl_class.name } unless type.is_a?(Types::PObjectType) 135 type.write(value, self) 136 end 137 end