@@ -15,6 +15,10 @@ class MetasploitDataModels::Base64Serializer
1515
1616 # The default for {#default}
1717 DEFAULT = { }
18+
19+ # The default for {#coerce}
20+ COERCE_DEFAULT = false
21+
1822 # Deserializers for {#load}
1923 # 1. Base64 decoding and then unmarshalling the value.
2024 # 2. Parsing the value as YAML.
@@ -47,14 +51,33 @@ def default
4751 end
4852
4953 attr_writer :default
54+ attr_writer :coerce
55+
56+ # Recursively coerce the object that has been passed in, keeping primitive types as their original type,
57+ # while changing objects that cannot be serialized into a string representation of the object data.
58+ def coerce_object ( value )
59+ case value
60+ when Hash
61+ value . transform_values { |v | coerce_object ( v ) }
62+ when Array
63+ value . map { |v | coerce_object ( v ) }
64+ when File , IO
65+ value . inspect
66+ when String , Integer , Float , TrueClass , FalseClass , NilClass , Symbol
67+ value
68+ else
69+ value . to_s
70+ end
71+ end
5072
5173 # Serializes the value by marshalling the value and then base64 encodes the marshaled value.
5274 #
5375 # @param value [Object] value to serialize
5476 # @return [String]
5577 def dump ( value )
5678 # Always store data back in the Marshal format
57- marshalled = Marshal . dump ( value )
79+ to_serialize = @coerce ? coerce_object ( value ) : value
80+ marshalled = Marshal . dump ( to_serialize )
5881 base64_encoded = [ marshalled ] . pack ( 'm' )
5982
6083 base64_encoded
@@ -63,9 +86,10 @@ def dump(value)
6386 # @param attributes [Hash] attributes
6487 # @option attributes [Object] :default ({}) Value to use for {#default}.
6588 def initialize ( attributes = { } )
66- attributes . assert_valid_keys ( :default )
89+ attributes . assert_valid_keys ( :default , :coerce )
6790
6891 @default = attributes . fetch ( :default , DEFAULT )
92+ @coerce = attributes . fetch ( :coerce , COERCE_DEFAULT )
6993 end
7094
7195 # Deserializes the value by either
0 commit comments