Class: MicrosoftKiotaSerializationJson::JsonParseNode

Inherits:
Object
  • Object
show all
Includes:
MicrosoftKiotaAbstractions::ParseNode
Defined in:
lib/microsoft_kiota_serialization_json/json_parse_node.rb

Constant Summary collapse

PRIMITIVE_READERS =

The generator passes the type as a class, except for booleans which it passes as a plain string. A case cannot dispatch on that: when String asks whether the type is an instance of String, and a class is an instance of Class, so every branch fell through to the string reader. A hash keys on the class object itself.

{
  String => :get_string_value,
  Float => :get_float_value,
  Integer => :get_number_value,
  Date => :get_date_value,
  DateTime => :get_date_time_value,
  Time => :get_time_value,
  MicrosoftKiotaAbstractions::ISODuration => :get_duration_value,
  UUIDTools::UUID => :get_guid_value,
  'boolean' => :get_boolean_value,
  'Boolean' => :get_boolean_value
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ JsonParseNode

Returns a new instance of JsonParseNode.



13
14
15
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 13

def initialize(node)
  @current_node = node
end

Instance Method Details

#assign_field_values(item) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 110

def assign_field_values(item)
  fields = item.get_field_deserializers
  @current_node.each do |k, v|
    next if v.nil?

    deserializer = fields[k]
    if deserializer
      deserializer.call(JsonParseNode.new(v))
    elsif item.additional_data
      item.additional_data[k] = v
    else
      item.additional_data = Hash.new({ k => v })
    end
  end
end

#get_boolean_valueObject



24
25
26
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 24

def get_boolean_value
  [true, false].include?(@current_node) ? @current_node : nil
end

#get_child_node(name) ⇒ Object

Raises:

  • (StandardError)


138
139
140
141
142
143
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 138

def get_child_node(name)
  raise StandardError, 'Name cannot be null' if name.nil? || name.empty?

  raw_value = @current_node[name]
  JsonParseNode.new(raw_value) if raw_value
end

#get_collection_of_object_values(factory) ⇒ Object

Raises:

  • (StandardError)


89
90
91
92
93
94
95
96
97
98
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 89

def get_collection_of_object_values(factory)
  raise StandardError, 'Factory cannot be null' if factory.nil?

  @current_node.map do |object|
    next if object.nil?

    current_parse_node = JsonParseNode.new(object)
    current_parse_node.get_object_value(factory)
  end
end

#get_collection_of_primitive_values(type) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 75

def get_collection_of_primitive_values(type)
  reader = PRIMITIVE_READERS[type]
  @current_node.map do |object|
    next if object.nil?
    # an untyped collection is generated as Object, which has no reader of its own; the parsed
    # JSON scalar is already the value, so it passes through rather than being stringified
    next object if reader.nil?

    JsonParseNode.new(object).public_send(reader)
  rescue StandardError => e
    raise e.class, "Failed to fetch #{type} type: #{e.message}"
  end
end

#get_date_time_valueObject



50
51
52
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 50

def get_date_time_value
  DateTime.parse(@current_node)
end

#get_date_valueObject



42
43
44
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 42

def get_date_value
  Date.parse(@current_node)
end

#get_duration_valueObject



54
55
56
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 54

def get_duration_value
  MicrosoftKiotaAbstractions::ISODuration.new(@current_node)
end

#get_enum_value(type) ⇒ Object



133
134
135
136
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 133

def get_enum_value(type)
  items = get_enum_values(type).map(&:to_sym)
  items[0] if items.length.positive?
end

#get_enum_values(_type) ⇒ Object



126
127
128
129
130
131
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 126

def get_enum_values(_type)
  raw_values = get_string_value
  return [] if raw_values.nil?

  raw_values.split(',').map(&:strip)
end

#get_float_valueObject

Widened to Numeric because JSON writes a whole number without a fraction, so a float field can legitimately arrive as an Integer.



34
35
36
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 34

def get_float_value
  @current_node.is_a?(Numeric) ? @current_node.to_f : nil
end

#get_guid_valueObject



38
39
40
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 38

def get_guid_value
  UUIDTools::UUID.parse(@current_node)
end

#get_number_valueObject



28
29
30
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 28

def get_number_value
  @current_node.is_a?(Integer) ? @current_node : nil
end

#get_object_value(factory) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 100

def get_object_value(factory)
  raise StandardError, 'Factory cannot be null' if factory.nil?

  item = factory.call(self)
  assign_field_values(item)
  item
rescue StandardError => e
  raise e.class, 'Error during deserialization'
end

#get_string_valueObject

The scalar readers answer only for the type they are named after and return nil otherwise, matching the dotnet and Python runtimes. Coercing instead would make every reader answer for every payload, which leaves a composed type unable to tell which member it holds.



20
21
22
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 20

def get_string_value
  @current_node.is_a?(String) ? @current_node : nil
end

#get_time_valueObject



46
47
48
# File 'lib/microsoft_kiota_serialization_json/json_parse_node.rb', line 46

def get_time_value
  Time.parse(@current_node)
end