Class: TidyJson::Serializer Private

Inherits:
Object show all
Defined in:
lib/tidy_json/serializer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A purpose-built JSON generator.

Class Method Summary collapse

Class Method Details

.serialize(obj, json_hash) ⇒ {String => #to_s}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Searches obj to a maximum depth of 2 for readable attributes, storing them as key-value pairs in json_hash.

Parameters:

  • obj (Object)

    A Ruby object that can be parsed as JSON.

  • json_hash ({String,Symbol => #to_s})

    Accumulator.

Returns:

  • ({String => #to_s})

    A hash mapping of obj's visible attributes.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tidy_json/serializer.rb', line 16

def self.serialize(obj, json_hash)
  obj.instance_variables.each do |m|
    key = m.to_s[/[^\@]\w*/].to_sym

    next unless key && !key.eql?('')

    begin
      val = obj.send(key) # assuming readable attributes . . .
    rescue NoMethodError # . . . which may not be always be the case !
      json_hash[key] = nil
    end

    begin
      # process class members of Hash type
      if val.instance_of?(Hash)
        nested_key = ''
        nested = nil

        val.each.any? do |k, v|
          unless v.instance_variables.empty?
            nested_key = k
            nested = v
          end
        end

        json_hash[key] = val

        if nested
          pos = val.keys.select { |k| k === nested_key }.first.to_sym
          nested.instance_variables.each do
            json_hash[key][pos] = serialize(nested,
                                            class: nested.class.name)
          end
        end

      # process class members of Array type
      elsif val.instance_of?(Array)
        json_hash[key] = []

        val.each do |elem|
          i = val.index(elem)

          # member is a multi-dimensional collection
          if elem.respond_to?(:each)
            nested = []
            elem.each do |e|
              j = if elem.respond_to?(:key)
                    elem.key(e)
                  else elem.index(e)
                  end

              # nested element is a class object
              if !e.instance_variables.empty?
                json_hash[key][j] = { class: e.class.name }

                # recur over the contained object
                serialize(e, json_hash[key][j])

              # some kind of collection?
              elsif e.respond_to?(:each)
                temp = []
                e.each do |el|
                  temp << if el.instance_variables.empty? then el
                          else JSON.parse(el.stringify)
                          end
                end

                nested << temp

              # scalar type
              else nested << e
              end
            end
            # ~iteration of nested array elements

            json_hash[key] << nested

          # member is a flat array
          elsif !elem.instance_variables.empty? # class object?
            json_hash[key] << { class: elem.class.name }
            serialize(elem, json_hash[key][i])

          # scalar type
          else json_hash[key] << elem
          end
        end
        # ~iteration of top-level array elements

      end
    rescue NoMethodError
      # we expected an array to behave like a hash, or vice-versa
      json_hash.store(key, val) # a shallow copy is better than nothing
    end
  end
  # ~iteration of instance variables

  json_hash
end