Module: M3u8::Serializable
- Included in:
- BitrateItem, ByteRange, ContentSteeringItem, DateRangeItem, DefineItem, DiscontinuityItem, GapItem, KeyItem, MapItem, MediaItem, PartInfItem, PartItem, PlaybackStart, Playlist, PlaylistItem, PreloadHintItem, RenditionReportItem, SegmentItem, ServerControlItem, SessionDataItem, SessionKeyItem, SkipItem, TimeItem
- Defined in:
- lib/m3u8/serializable.rb
Overview
Serializable provides a Hash representation of an item by converting its instance variables into JSON-compatible values.
Class Method Summary collapse
-
.serialize(value) ⇒ Object
Recursively convert a value into a JSON-compatible form.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Convert the item into a JSON-ready Hash (alias of #to_h).
-
#to_h ⇒ Hash<Symbol, Object>
Convert the item into a Hash of its attributes.
-
#to_json(*args) ⇒ String
Render the item as a JSON string.
Class Method Details
.serialize(value) ⇒ Object
Recursively convert a value into a JSON-compatible form.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/m3u8/serializable.rb', line 32 def self.serialize(value) case value when TimeItem then serialize(value.time) when Time then value.iso8601 when BigDecimal then value.to_f when Serializable then value.to_h when Hash then value.transform_values { |item| serialize(item) } else value end end |
Instance Method Details
#as_json ⇒ Hash
Convert the item into a JSON-ready Hash (alias of #to_h).
18 19 20 |
# File 'lib/m3u8/serializable.rb', line 18 def as_json(*) to_h end |
#to_h ⇒ Hash<Symbol, Object>
Convert the item into a Hash of its attributes.
9 10 11 12 13 14 |
# File 'lib/m3u8/serializable.rb', line 9 def to_h instance_variables.each_with_object({}) do |ivar, result| key = ivar.to_s.delete_prefix('@').to_sym result[key] = Serializable.serialize(instance_variable_get(ivar)) end end |
#to_json(*args) ⇒ String
Render the item as a JSON string.
24 25 26 27 |
# File 'lib/m3u8/serializable.rb', line 24 def to_json(*args) require 'json' to_h.to_json(*args) end |