Class: OnlinePayments::SDK::JSON::DefaultMarshaller
- Inherits:
-
Marshaller
- Object
- Marshaller
- OnlinePayments::SDK::JSON::DefaultMarshaller
- Includes:
- Singleton
- Defined in:
- lib/onlinepayments/sdk/json/default_marshaller.rb
Overview
Marshals objects to and from JSON format. Currently supports marshalling and unmarshalling of classes that support class.new_from_hash and class#to_h
Instance Method Summary collapse
-
#marshal(request_object) ⇒ Object
Marshals the request_object to a JSON string using request_object#to_h.
-
#unmarshal(json_string, klass) ⇒ Object
Unmarshals a JSON string into an object of type klass using klass.new_from_hash.
Instance Method Details
#marshal(request_object) ⇒ Object
Marshals the request_object to a JSON string using request_object#to_h
14 15 16 17 |
# File 'lib/onlinepayments/sdk/json/default_marshaller.rb', line 14 def marshal(request_object) return 'null' if request_object.nil? ::JSON.pretty_generate(request_object.to_h) end |
#unmarshal(json_string, klass) ⇒ Object
Unmarshals a JSON string into an object of type klass using klass.new_from_hash
20 21 22 23 24 25 26 27 |
# File 'lib/onlinepayments/sdk/json/default_marshaller.rb', line 20 def unmarshal(json_string, klass) return nil if json_string.nil? || json_string.empty? if klass.respond_to?(:new_from_hash) klass.new_from_hash(::JSON.load(json_string)) else raise NotImplementedError end end |