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

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

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

#binary_encode_any_high_ascii

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)



30
31
32
33
34
35
36
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 30

def dump_yaml(object)
  # Psych produces UTF-8 encoded output; we'd rather
  # have 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)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ndr_support/yaml/serialization_migration.rb', line 12

def load_yaml(string, coerce_invalid_chars = false)
  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)

  object = Psych.load(string)

  # 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