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(*args) ⇒ String
(also: #to_s)
Convert the object to a JSON document.
Constructor Details
#initialize(object = nil) ⇒ Serializer
Instantiate a new serializer.
23 24 25 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 23 def initialize(object = nil) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns The object to serialize.
28 29 30 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 28 def object @object end |
Class Method Details
.load(json) ⇒ Object
Load an object from a JSON document.
15 16 17 18 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 15 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.
37 38 39 40 41 42 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 37 def load(_hash) raise( NotImplementedError, 'You must implement a concrete version in a subclass' ) end |
#to_h ⇒ Hash
Convert the object to a Hash.
49 50 51 52 53 54 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 49 def to_h raise( NotImplementedError, 'You must implement a concrete version in a subclass' ) end |
#to_json(*args) ⇒ String Also known as: to_s
Convert the object to a JSON document.
59 60 61 |
# File 'lib/benchmark/memory/held_results/serializer.rb', line 59 def to_json(*args) JSON.generate(to_h, *args) end |