Module: Safrano::XJSON

Defined in:
lib/odata/request/json.rb

Overview

This is used from the Test-suite code ! it does recursive / deep symbolize additionally to inbound casting

Class Method Summary collapse

Class Method Details

.cast_values_in(resd) ⇒ Object

symbolise keys and cast/parse values back to the proper ruby type; proper type meaning the one that Sequel chooses when loading data from the DB apply recursively to nested navigation attributes sub-structures



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
# File 'lib/odata/request/json.rb', line 50

def self.cast_values_in(resd)
  resd.symbolize_keys!

  if (defered = resd[:__deferred])
    defered.symbolize_keys!
  elsif (meta = resd[:__metadata])
    meta.symbolize_keys!

    # type is normally namespaced !  --> split.last is the class
    typ = get_class_from_meta(meta)

    typ.db_schema.each do |f, props|
       = typ.[f]
      case props[:type]
      when :datetime
        #              resd[f] = Time.parse(resd[f]) if resd[f]
        #              resd[f] = DateTime.strptime(resd[f], '/Date(%Q)').to_time if resd[f]
        resd[f] = Sequel.datetime_class.from_edm_json(resd[f]) if resd[f]
      when :decimal
        resd[f] = BigDecimal(resd[f])
      when :float
        resd[f] = Float(resd[f])
      else
        # TODO: better typ-system
        # Currently Sequel loads Numeric(x,y) as Float
        resd[f] = Float(resd[f]) if [:edm_type] =~ /\A\s*Edm.Decimal/
      end
    end

    if typ.nav_entity_attribs
      typ.nav_entity_attribs.each_key do |nattr|
        cast_values_in(resd[nattr.to_sym]) if resd[nattr.to_sym]
      end
    end

    if typ.nav_collection_attribs
      typ.nav_collection_attribs.each_key do |ncattr|
        if (resd_attr = resd[ncattr.to_sym])
          if (defered = resd_attr['__deferred'])
            defered.symbolize_keys! if defered
          else
            resd_attr.symbolize_keys! #  'results' --> :results
            resd_attr[:results].each { |enty| cast_values_in(enty) }
          end
        end
      end
    end
  end
  resd
end

.get_class_from_meta(meta) ⇒ Object



42
43
44
45
# File 'lib/odata/request/json.rb', line 42

def self.get_class_from_meta(meta)
  # type is normally namespaced !  --> split.last is the class
  meta[:type].split('.').last.constantize
end

.parse_coll(*args) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/odata/request/json.rb', line 151

def self.parse_coll(*args)
  resh = ::JSON.parse(*args)

  resh['d']['results'].map! { |currd| cast_values_in(currd) }

  resh['d']
end

.parse_coll_nocast(*args) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/odata/request/json.rb', line 167

def self.parse_coll_nocast(*args)
  resh = ::JSON.parse(*args)

  resh['d']['results'].map! { |currd| symbolize_attribs_in(currd) }

  resh['d']
end

.parse_one(*args) ⇒ Object



138
139
140
141
# File 'lib/odata/request/json.rb', line 138

def self.parse_one(*args)
  resh = ::JSON.parse(*args)
  cast_values_in(resh['d'])
end

.parse_one_nocast(*args) ⇒ Object



133
134
135
136
# File 'lib/odata/request/json.rb', line 133

def self.parse_one_nocast(*args)
  resh = ::JSON.parse(*args)
  symbolize_attribs_in(resh['d'])
end

.symbolize_attribs_in(resd) ⇒ Object

symbolise attrib names (h-keys) apply recursively to nested navigation attributes sub-structures



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/odata/request/json.rb', line 103

def self.symbolize_attribs_in(resd)
  resd.symbolize_keys!

  if (defered = resd[:__deferred])
    defered.symbolize_keys!
  elsif (meta = resd[:__metadata])
    meta.symbolize_keys!

    typ = get_class_from_meta(meta)

    if typ.nav_entity_attribs
      typ.nav_entity_attribs.each_key do |nattr|
        symbolize_attribs_in(resd[nattr.to_sym])
      end
    end

    if typ.nav_collection_attribs
      typ.nav_collection_attribs.each_key do |ncattr|
        if (defered = resd[ncattr.to_sym]['__deferred'])
          defered.symbolize_keys!
        else
          resd[ncattr.to_sym].each { |enty| symbolize_attribs_in(enty) }
        end
      end
    end

  end
  resd
end

.v1_parse_coll(*args) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/odata/request/json.rb', line 143

def self.v1_parse_coll(*args)
  resh = ::JSON.parse(*args)

  resh['d'].map! { |currd| cast_values_in(currd) }

  resh['d']
end

.v1_parse_coll_nocast(*args) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/odata/request/json.rb', line 159

def self.v1_parse_coll_nocast(*args)
  resh = ::JSON.parse(*args)

  resh['d'].map! { |currd| symbolize_attribs_in(currd) }

  resh['d']
end