Class: Benchmark::Memory::HeldResults::Serializer
- Inherits:
-
Object
- Object
- Benchmark::Memory::HeldResults::Serializer
- Defined in:
- lib/benchmark/memory/held_results/serializer.rb
Overview
Serialize objects for holding between runs.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
The object to serialize.
Class Method Summary collapse
-
.load(json) ⇒ Object
Load an object from a JSON document.
Instance Method Summary collapse
-
#initialize(object = nil) ⇒ Serializer
constructor
Instantiate a new serializer.
-
#load(_hash) ⇒ Object
Convert a JSON document into an object.
-
#to_h ⇒ Hash
Convert the object to a Hash.
-
#to_json ⇒ String
(also: #to_s)
Convert the object to a JSON document.
Constructor Details
#initialize(object = nil) ⇒ Serializer
Instantiate a new serializer.
21 22 23 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 21 def initialize(object = nil) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns The object to serialize.
26 27 28 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 26 def object @object end |
Class Method Details
.load(json) ⇒ Object
Load an object from a JSON document.
13 14 15 16 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 13 def self.load(json) json = JSON.parse(json) if json.is_a?(String) new.load(json).object end |
Instance Method Details
#load(_hash) ⇒ Object
Convert a JSON document into an object.
35 36 37 38 39 40 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 35 def load(_hash) fail( NotImplementedError, "You must implement a concrete version in a subclass" ) end |
#to_h ⇒ Hash
Convert the object to a Hash.
47 48 49 50 51 52 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 47 def to_h fail( NotImplementedError, "You must implement a concrete version in a subclass" ) end |
#to_json ⇒ String Also known as: to_s
Convert the object to a JSON document.
57 58 59 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 57 def to_json JSON.generate(to_h) end |