Class: Restate::DryStructSerde
- Inherits:
-
Object
- Object
- Restate::DryStructSerde
- Defined in:
- lib/restate/serde.rb
Overview
Serde for Dry::Struct types. Deserializes JSON into struct instances, serializes structs to JSON.
Instance Attribute Summary collapse
-
#struct_class ⇒ Object
readonly
Returns the value of attribute struct_class.
Instance Method Summary collapse
-
#deserialize(buf) ⇒ Object
Deserialize JSON bytes into a Dry::Struct instance.
-
#initialize(struct_class) ⇒ DryStructSerde
constructor
Create a DryStructSerde for the given Dry::Struct class.
-
#json_schema ⇒ Object
Return the JSON Schema derived from the Dry::Struct definition.
-
#serialize(obj) ⇒ Object
Serialize a Dry::Struct (or hash-like object) to JSON bytes.
Constructor Details
#initialize(struct_class) ⇒ DryStructSerde
Create a DryStructSerde for the given Dry::Struct class.
181 182 183 |
# File 'lib/restate/serde.rb', line 181 def initialize(struct_class) @struct_class = struct_class end |
Instance Attribute Details
#struct_class ⇒ Object (readonly)
Returns the value of attribute struct_class.
178 179 180 |
# File 'lib/restate/serde.rb', line 178 def struct_class @struct_class end |
Instance Method Details
#deserialize(buf) ⇒ Object
Deserialize JSON bytes into a Dry::Struct instance.
194 195 196 197 198 199 |
# File 'lib/restate/serde.rb', line 194 def deserialize(buf) return nil if buf.nil? || buf.empty? hash = JSON.parse(buf, symbolize_names: true) @struct_class.new(**hash) end |
#json_schema ⇒ Object
Return the JSON Schema derived from the Dry::Struct definition.
202 203 204 |
# File 'lib/restate/serde.rb', line 202 def json_schema @json_schema ||= Serde.dry_struct_to_json_schema(@struct_class) end |
#serialize(obj) ⇒ Object
Serialize a Dry::Struct (or hash-like object) to JSON bytes.
186 187 188 189 190 191 |
# File 'lib/restate/serde.rb', line 186 def serialize(obj) return EMPTY_BYTES if obj.nil? hash = obj.respond_to?(:to_h) ? obj.to_h : obj JSON.generate(hash).b end |