Module: NdrSupport::YAML::SerializationMigration
- Includes:
- UTF8Encoding
- Defined in:
- lib/ndr_support/yaml/serialization_migration.rb
Overview
Lightweight wrapper around YAML serialization, to provide any necessary support for YAML engines and string encodings.
Constant Summary collapse
- YAML_SAFE_CLASSES =
Classes we routinely allow to be included in our YAML serialisations, automatically accepted by load_yaml
[Date, DateTime, Time, Symbol].freeze
Constants included from UTF8Encoding
UTF8Encoding::AUTO_ENCODINGS, UTF8Encoding::BINARY, UTF8Encoding::REPLACEMENT_SCHEME, UTF8Encoding::UTF8
Constants included from UTF8Encoding::ControlCharacters
UTF8Encoding::ControlCharacters::CONTROL_CHARACTERS
Instance Method Summary collapse
-
#dump_yaml(object) ⇒ Object
Wrapper around: YAML.dump(object).
-
#load_yaml(string, coerce_invalid_chars = false) ⇒ Object
Wrapper around: YAML.load(string).
- #utf8_storage ⇒ Object
-
#utf8_storage=(utf8_storage) ⇒ Object
Allow emitted YAML to contain UTF-8 characters Defaults to true.
- #yaml_safe_classes ⇒ Object
-
#yaml_safe_classes=(yaml_safe_classes) ⇒ Object
Set list of YAML safe classes, or :unsafe to use unsafe load.
Methods included from UTF8Encoding
#coerce_utf8, #coerce_utf8!, #ensure_utf8, #ensure_utf8!
Methods included from UTF8Encoding::ObjectSupport
#ensure_utf8_array!, #ensure_utf8_hash!, #ensure_utf8_object!
Methods included from UTF8Encoding::ForceBinary
Methods included from UTF8Encoding::ControlCharacters
#escape_control_chars, #escape_control_chars!, #escape_control_chars_in_array!, #escape_control_chars_in_hash!, #escape_control_chars_in_object!
Instance Method Details
#dump_yaml(object) ⇒ Object
Wrapper around: YAML.dump(object)
62 63 64 65 66 67 68 69 70 |
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 62 def dump_yaml(object) return Psych.dump(object) if utf8_storage # Psych produces UTF-8 encoded output; historically we # preferred YAML that can be safely stored in stores with # other encodings. If #load_yaml is used, the binary # encoding of the object will be reversed on load. Psych.dump binary_encode_any_high_ascii(object) end |
#load_yaml(string, coerce_invalid_chars = false) ⇒ Object
Wrapper around: YAML.load(string)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 37 def load_yaml(string, coerce_invalid_chars = false) # rubocop:disable Style/OptionalBooleanParameter fix_encoding!(string, coerce_invalid_chars) # Achieve same behaviour using `syck` and `psych`: handle_special_characters!(string, coerce_invalid_chars) fix_encoding!(string, coerce_invalid_chars) # TODO: Bump NdrSupport major version, and switch to safe_load by default object = if yaml_safe_classes == :unsafe raise(SecurityError, 'Unsafe YAML no longer supported') unless Psych::VERSION.start_with?('3.') Psych.load(string) else Psych.safe_load(string, permitted_classes: yaml_safe_classes, aliases: true) end # Ensure that any string related to the object # we've loaded is also valid UTF-8. ensure_utf8_object!(object) # We escape all non-printing control chars: escape_control_chars_in_object!(object) end |
#utf8_storage ⇒ Object
30 31 32 33 34 |
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 30 def utf8_storage return @utf8_storage if @utf8_storage == false true # New ndr_support default for versions >= 6, previously false end |
#utf8_storage=(utf8_storage) ⇒ Object
Allow emitted YAML to contain UTF-8 characters Defaults to true. (Defaulted to false in ndr_support versions < 6)
26 27 28 |
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 26 def utf8_storage=(utf8_storage) @utf8_storage = utf8_storage end |
#yaml_safe_classes ⇒ Object
20 21 22 |
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 20 def yaml_safe_classes @yaml_safe_classes || YAML_SAFE_CLASSES end |
#yaml_safe_classes=(yaml_safe_classes) ⇒ Object
Set list of YAML safe classes, or :unsafe to use unsafe load
16 17 18 |
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 16 def yaml_safe_classes=(yaml_safe_classes) @yaml_safe_classes = yaml_safe_classes end |