Module: MsRest2::JSONable

Includes:
Serialization
Defined in:
lib/ms_rest2/jsonable.rb

Overview

Mixin to provide simple serialization / deserialization in AutoRest generated model classes

Instance Method Summary collapse

Methods included from Serialization

#deserialize, #serialize

Instance Method Details

#from_json(json, mapper = nil) ⇒ JSONable

Deserialize the object from JSON

Parameters:

  • json (String)

    JSON string representation of the object

Returns:

  • (JSONable)

    object built from json input



26
27
28
29
# File 'lib/ms_rest2/jsonable.rb', line 26

def from_json(json, mapper = nil)
  mapper = self.class.mapper if mapper.nil?
  deserialize(mapper, json)
end

#to_json(options = nil) ⇒ String

Serialize the object to JSON

Parameters:

  • options (Hash) (defaults to: nil)

    hash map contains options to convert to json.

Returns:

  • (String)

    JSON serialized version of the object



15
16
17
18
19
# File 'lib/ms_rest2/jsonable.rb', line 15

def to_json(options = nil)
  mapper = (options.nil? || !options.key?(:mapper))? self.class.mapper: options[:mapper]
  object = (options.nil? || !options.key?(:object))? self: options[:object]
  serialize(mapper, object)
end

#to_sString

String representation of the object

Returns:

  • (String)


35
36
37
# File 'lib/ms_rest2/jsonable.rb', line 35

def to_s
  "#{super} #{to_json.to_s}"
end