Class: MicrosoftKiotaSerializationJson::JsonSerializationWriter

Inherits:
Object
  • Object
show all
Includes:
MicrosoftKiotaAbstractions::SerializationWriter
Defined in:
lib/microsoft_kiota_serialization_json/json_serialization_writer.rb

Instance Method Summary collapse

Constructor Details

#initializeJsonSerializationWriter

Returns a new instance of JsonSerializationWriter.


12
13
14
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 12

def initialize()
  @writer = Hash.new()
end

Instance Method Details

#get_serialized_contentObject


181
182
183
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 181

def get_serialized_content()
  return @writer.to_json #TODO encode to byte array to stay content type agnostic
end

#write_additional_data(value) ⇒ Object


185
186
187
188
189
190
191
192
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 185

def write_additional_data(value)
  if !value
    return
  end
  value.each do |x, y|
    self.write_any_value(x,y)
  end
end

#write_any_value(key, value) ⇒ Object


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 194

def write_any_value(key, value)
  if value
    if !!value == value
      return value
    elsif value.instance_of? String
      return self.write_string_value(key, value)
    elsif value.instance_of? Integer
      return self.write_number_value(key, value)
    elsif value.instance_of? Float
      return self.write_float_value(key, value)
    elsif value.instance_of? DateTime
      return self.write_date_time_value(key, value)
    elsif value.instance_of? Time
      return self.write_time_value(key, value)
    elsif value.instance_of? Date
      return self.write_date_value(key, value)
    elsif value.instance_of? MicrosoftKiotaAbstractions::ISODuration
      return self.write_duration_value(key, value)
    elsif value.instance_of? Array
      return self.write_collection_of_primitive_values(key, value)
    elsif value.is_a? Object
      return value.to_s
    else
      raise StandardError, "encountered unknown value type during serialization #{value.to_s}"
    end
  else
    if key
      @writer[key] = nil
    else
      raise StandardError, "no key included when writing json property"
    end
  end
end

#write_boolean_value(key, value) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 34

def write_boolean_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_boolean_value(key, value)"
  end
  if !key 
    return value
  end
  @writer[key] = value
end

#write_collection_of_object_values(key, values) ⇒ Object


147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 147

def write_collection_of_object_values(key, values)
  if values
    if !key
      return values.map do |v|
        self.write_object_value(nil, v)
      end
    end
    @writer[key] = values.map do |v|
      self.write_object_value(nil, v).writer
    end
  end
end

#write_collection_of_primitive_values(key, values) ⇒ Object


134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 134

def write_collection_of_primitive_values(key, values)
  if values
    if !key
      return values.map do |v|
        self.write_any_value(nil, v)
      end
    end
    @writer[key] = values.map do |v|
      self.write_any_value(key, v)
    end
  end
end

#write_date_time_value(key, value) ⇒ Object


106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 106

def write_date_time_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_date_time_value(key, value)"
  end
  if !key
    return value.strftime("%Y-%m-%dT%H:%M:%S%Z")
  end
  if !value
    @writer[key] = nil
  else
    @writer[key] = value.strftime("%Y-%m-%dT%H:%M:%S%Z")
  end
end

#write_date_value(key, value) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 78

def write_date_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_date_value(key, value)"
  end
  if !key
    return value.strftime("%Y-%m-%d")
  end
  if !value
    @writer[key] = nil
  else
    @writer[key] = value.strftime("%Y-%m-%d")
  end
end

#write_duration_value(key, value) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 120

def write_duration_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_duration_value(key, value)"
  end
  if !key
    return value.string
  end
  if !value
    @writer[key] = nil
  else
    @writer[key] = value.string
  end
end

#write_enum_value(key, values) ⇒ Object


177
178
179
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 177

def write_enum_value(key, values)
  self.write_string_value(key, values.to_s)
end

#write_float_value(key, value) ⇒ Object


54
55
56
57
58
59
60
61
62
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 54

def write_float_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_float_value(key, value)"
  end
  if !key
    return value
  end
  @writer[key] = value
end

#write_guid_value(key, value) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 64

def write_guid_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_guid_value(key, value)"
  end
  if !key
    return value.to_s
  end
  if !value
    @writer[key] = nil
  else
    @writer[key] = value.to_s
  end
end

#write_number_value(key, value) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 44

def write_number_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_number_value(key, value)"
  end
  if !key
    return value
  end
  @writer[key] = value
end

#write_object_value(key, value) ⇒ Object


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 160

def write_object_value(key, value)
  if value
    if !key
      temp = JsonSerializationWriter.new()
      value.serialize(temp)
      return temp
    end
    begin
      temp = JsonSerializationWriter.new()
      value.serialize(temp)
      @writer[key] = temp.writer
    rescue StandardError => e
      raise e.class, "no key or value included in write_boolean_value(key, value)" 
    end
  end
end

#write_string_value(key, value) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 20

def write_string_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_string_value(key, value)"
  end
  if !key
    return value.to_s
  end
  if !value
    @writer[key] = nil
  else
    @writer[key] = value
  end
end

#write_time_value(key, value) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 92

def write_time_value(key, value)
  if !key && !value
    raise StandardError, "no key or value included in write_time_value(key, value)"
  end
  if !key
    return value.strftime("%H:%M:%S%Z")
  end
  if !value
    @writer[key] = nil
  else
    @writer[key] = value.strftime("%H:%M:%S%Z")
  end
end

#writerObject


16
17
18
# File 'lib/microsoft_kiota_serialization_json/json_serialization_writer.rb', line 16

def writer
  @writer
end