Module: SolidObjects::Serialization
- Defined in:
- lib/solid_objects/serialization.rb,
sig/generated/lib/solid_objects/serialization.rbs
Defined Under Namespace
Classes: Dumped
Constant Summary collapse
- MAX_NESTING =
100
Class Method Summary collapse
- .deep_copy(value) ⇒ Object
- .dump(value, max_bytes: nil) ⇒ Object
- .dump_with_byte_size(value, max_bytes: nil) ⇒ Dumped
- .load(value) ⇒ Object
- .readonly_copy(value) ⇒ Object
Class Method Details
.deep_copy(value) ⇒ Object
39 40 41 42 43 |
# File 'lib/solid_objects/serialization.rb', line 39 def deep_copy(value) JSON.parse(JSON.generate(normalize(value), max_nesting: MAX_NESTING)) rescue JSON::GeneratorError, JSON::ParserError, EncodingError => error raise InvalidPayload, error. end |
.dump(value, max_bytes: nil) ⇒ Object
11 12 13 14 15 |
# File 'lib/solid_objects/serialization.rb', line 11 def dump(value, max_bytes: nil) return normalize(value) unless max_bytes dump_with_byte_size(value, max_bytes:).value end |
.dump_with_byte_size(value, max_bytes: nil) ⇒ Dumped
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/solid_objects/serialization.rb', line 18 def dump_with_byte_size(value, max_bytes: nil) normalized = normalize(value) byte_size = JSON.generate(normalized, max_nesting: MAX_NESTING).bytesize if max_bytes && byte_size > max_bytes raise PayloadTooLarge, "serialized value exceeds #{max_bytes} bytes" end Dumped.new(value: normalized, byte_size:) rescue JSON::GeneratorError, EncodingError => error raise InvalidPayload, error. end |
.load(value) ⇒ Object
32 33 34 35 36 |
# File 'lib/solid_objects/serialization.rb', line 32 def load(value) return nil if value.nil? normalize(value) end |
.readonly_copy(value) ⇒ Object
46 47 48 |
# File 'lib/solid_objects/serialization.rb', line 46 def readonly_copy(value) freeze_recursively(deep_copy(value)) end |