Module: Nexpose::JsonSerializer

Included in:
Alert, GlobalBlackout, ScanFilter, Site, VulnFilter
Defined in:
lib/nexpose/json_serializer.rb

Constant Summary collapse

@@namespace =
'Nexpose'

Instance Method Summary collapse

Instance Method Details

#deserialize(data) ⇒ Object



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
# File 'lib/nexpose/json_serializer.rb', line 5

def deserialize(data)
  data.each do |key, value|
    if respond_to?(key)
      property = value

      if value.respond_to? :each
        obj = resolve_type(key)

        unless obj.nil?
          if value.is_a?(Array)
            property = value.map { |dv|  ((dv.respond_to? :each) ? create_object(obj, dv).deserialize(dv): dv) }
          else
            property = create_object(obj, value).deserialize(value)
          end
        end
      elsif value.is_a?(String) && value.match(/^\d{8}T\d{6}\.\d{3}/)
        property = ISO8601.to_time(value)
      end

      instance_variable_set("@#{key}", property)
    end
  end

  self
end

#serializeObject



31
32
33
34
35
36
37
# File 'lib/nexpose/json_serializer.rb', line 31

def serialize()
  hash = to_hash(Hash.new)

  unless hash.nil?
    JSON.generate(hash)
  end
end

#to_hash(hash) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/nexpose/json_serializer.rb', line 39

def to_hash(hash)
  self.instance_variables.each do |m|
    value = self.instance_variable_get(m)
    hash[m.to_s.delete('@')] = do_hash(value)
  end

  hash
end