File: jsonable.rb

package info (click to toggle)
ruby-ms-rest 0.7.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 424 kB
  • sloc: ruby: 693; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 1,205 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# encoding: utf-8
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

module MsRest
  # Mixin to provide simple serialization / deserialization in AutoRest generated model classes
  module JSONable
    include MsRest::Serialization

    #
    # Serialize the object to JSON
    # @param options [Hash] hash map contains options to convert to json.
    # @return [String] JSON serialized version of the object
    #
    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

    #
    # Deserialize the object from JSON
    # @param json [String] JSON string representation of the object
    # @return [JSONable] object built from json input
    #
    def from_json(json, mapper = nil)
      mapper = self.class.mapper if mapper.nil?
      deserialize(mapper, json)
    end

    #
    # String representation of the object
    # @return [String]
    #
    def to_s
      "#{super} #{to_json.to_s}"
    end
  end
end