|
| 1 | +# ------------------------------------------------------------------------------------ |
| 2 | +# <copyright company="Aspose" file="json_data_load_options.rb"> |
| 3 | +# Copyright (c) 2021 Aspose.Words for Cloud |
| 4 | +# </copyright> |
| 5 | +# <summary> |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +# </summary> |
| 24 | +# ------------------------------------------------------------------------------------ |
| 25 | + |
| 26 | +require 'date' |
| 27 | + |
| 28 | +module AsposeWordsCloud |
| 29 | + |
| 30 | + # Represents options for parsing JSON data. |
| 31 | + class JsonDataLoadOptions |
| 32 | + # Gets or sets a value indicating whether a generated data source will always contain |
| 33 | + # an object for a JSON root element. If a JSON root element contains a single complex |
| 34 | + # property, such an object is not created by default. |
| 35 | + attr_accessor :always_generate_root_object |
| 36 | + |
| 37 | + # Gets or sets exact formats for parsing JSON date-time values while loading JSON. |
| 38 | + # The default is null. |
| 39 | + attr_accessor :exact_date_time_parse_formats |
| 40 | + |
| 41 | + # Gets or sets a mode for parsing JSON simple values (null, boolean, number, integer, |
| 42 | + # and string) while loading JSON. Such a mode does not affect parsing of date-time |
| 43 | + # values. The default is Aspose.Words.Reporting.JsonSimpleValueParseMode.Loose. |
| 44 | + attr_accessor :simple_value_parse_mode |
| 45 | + |
| 46 | + class EnumAttributeValidator |
| 47 | + attr_reader :datatype |
| 48 | + attr_reader :allowable_values |
| 49 | + |
| 50 | + def initialize(datatype, allowable_values) |
| 51 | + @allowable_values = allowable_values.map do |value| |
| 52 | + case datatype.to_s |
| 53 | + when /Integer/i |
| 54 | + value.to_i |
| 55 | + when /Float/i |
| 56 | + value.to_f |
| 57 | + else |
| 58 | + value |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def valid?(value) |
| 64 | + !value || allowable_values.include?(value) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + # Attribute mapping from ruby-style variable name to JSON key. |
| 69 | + def self.attribute_map |
| 70 | + { |
| 71 | + :'always_generate_root_object' => :'AlwaysGenerateRootObject', |
| 72 | + :'exact_date_time_parse_formats' => :'ExactDateTimeParseFormats', |
| 73 | + :'simple_value_parse_mode' => :'SimpleValueParseMode' |
| 74 | + } |
| 75 | + end |
| 76 | + |
| 77 | + # Attribute type mapping. |
| 78 | + def self.swagger_types |
| 79 | + { |
| 80 | + :'always_generate_root_object' => :'BOOLEAN', |
| 81 | + :'exact_date_time_parse_formats' => :'Array<String>', |
| 82 | + :'simple_value_parse_mode' => :'String' |
| 83 | + } |
| 84 | + end |
| 85 | + |
| 86 | + # Initializes the object |
| 87 | + # @param [Hash] attributes Model attributes in the form of hash |
| 88 | + def initialize(attributes = {}) |
| 89 | + return unless attributes.is_a?(Hash) |
| 90 | + |
| 91 | + # convert string to symbol for hash key |
| 92 | + attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } |
| 93 | + |
| 94 | + if attributes.key?(:'AlwaysGenerateRootObject') |
| 95 | + self.always_generate_root_object = attributes[:'AlwaysGenerateRootObject'] |
| 96 | + end |
| 97 | + |
| 98 | + if attributes.key?(:'ExactDateTimeParseFormats') |
| 99 | + if (value = attributes[:'ExactDateTimeParseFormats']).is_a?(Array) |
| 100 | + self.exact_date_time_parse_formats = value |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + if attributes.key?(:'SimpleValueParseMode') |
| 105 | + self.simple_value_parse_mode = attributes[:'SimpleValueParseMode'] |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + # Show invalid properties with the reasons. Usually used together with valid? |
| 110 | + # @return Array for valid properies with the reasons |
| 111 | + def list_invalid_properties |
| 112 | + invalid_properties = [] |
| 113 | + return invalid_properties |
| 114 | + end |
| 115 | + |
| 116 | + # Check to see if the all the properties in the model are valid |
| 117 | + # @return true if the model is valid |
| 118 | + def valid? |
| 119 | + simple_value_parse_mode_validator = EnumAttributeValidator.new('String', ["Loose", "Strict"]) |
| 120 | + return false unless simple_value_parse_mode_validator.valid?(@simple_value_parse_mode) |
| 121 | + |
| 122 | + return true |
| 123 | + end |
| 124 | + |
| 125 | + # Custom attribute writer method checking allowed values (enum). |
| 126 | + # @param [Object] simple_value_parse_mode Object to be assigned |
| 127 | + def simple_value_parse_mode=(simple_value_parse_mode) |
| 128 | + validator = EnumAttributeValidator.new('String', ["Loose", "Strict"]) |
| 129 | + if simple_value_parse_mode.to_i == 0 |
| 130 | + unless validator.valid?(simple_value_parse_mode) |
| 131 | + raise ArgumentError, "invalid value for 'simple_value_parse_mode', must be one of #{validator.allowable_values}." |
| 132 | + end |
| 133 | + @simple_value_parse_mode = simple_value_parse_mode |
| 134 | + else |
| 135 | + @simple_value_parse_mode = validator.allowable_values[simple_value_parse_mode.to_i] |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + |
| 140 | + # Checks equality by comparing each attribute. |
| 141 | + # @param [Object] Object to be compared |
| 142 | + def ==(other) |
| 143 | + return true if self.equal?(other) |
| 144 | + self.class == other.class && |
| 145 | + always_generate_root_object == other.always_generate_root_object && |
| 146 | + exact_date_time_parse_formats == other.exact_date_time_parse_formats && |
| 147 | + simple_value_parse_mode == other.simple_value_parse_mode |
| 148 | + end |
| 149 | + |
| 150 | + # @see the `==` method |
| 151 | + # @param [Object] Object to be compared |
| 152 | + def eql?(other) |
| 153 | + self == other |
| 154 | + end |
| 155 | + |
| 156 | + # Calculates hash code according to all attributes. |
| 157 | + # @return [Fixnum] Hash code |
| 158 | + def hash |
| 159 | + [always_generate_root_object, exact_date_time_parse_formats, simple_value_parse_mode].hash |
| 160 | + end |
| 161 | + |
| 162 | + # Builds the object from hash |
| 163 | + # @param [Hash] attributes Model attributes in the form of hash |
| 164 | + # @return [Object] Returns the model itself |
| 165 | + def build_from_hash(attributes) |
| 166 | + return nil unless attributes.is_a?(Hash) |
| 167 | + self.class.swagger_types.each_pair do |key, type| |
| 168 | + if type =~ /\AArray<(.*)>/i |
| 169 | + # check to ensure the input is an array given that the the attribute |
| 170 | + # is documented as an array but the input is not |
| 171 | + if attributes[self.class.attribute_map[key]].is_a?(Array) |
| 172 | + self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) }) |
| 173 | + end |
| 174 | + elsif !attributes[self.class.attribute_map[key]].nil? |
| 175 | + self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]])) |
| 176 | + end |
| 177 | + # or else data not found in attributes(hash), not an issue as the data can be optional |
| 178 | + end |
| 179 | + |
| 180 | + self |
| 181 | + end |
| 182 | + |
| 183 | + # Deserializes the data based on type |
| 184 | + # @param string type Data type |
| 185 | + # @param string value Value to be deserialized |
| 186 | + # @return [Object] Deserialized data |
| 187 | + def _deserialize(type, value) |
| 188 | + case type.to_sym |
| 189 | + when :DateTime |
| 190 | + Time.at(/\d/.match(value)[0].to_f).to_datetime |
| 191 | + when :Date |
| 192 | + Time.at(/\d/.match(value)[0].to_f).to_date |
| 193 | + when :String |
| 194 | + value.to_s |
| 195 | + when :Integer |
| 196 | + value.to_i |
| 197 | + when :Float |
| 198 | + value.to_f |
| 199 | + when :BOOLEAN |
| 200 | + if value.to_s =~ /\A(true|t|yes|y|1)\z/i |
| 201 | + true |
| 202 | + else |
| 203 | + false |
| 204 | + end |
| 205 | + when :Object |
| 206 | + # generic object (usually a Hash), return directly |
| 207 | + value |
| 208 | + when /\AArray<(?<inner_type>.+)>\z/ |
| 209 | + inner_type = Regexp.last_match[:inner_type] |
| 210 | + value.map { |v| _deserialize(inner_type, v) } |
| 211 | + when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/ |
| 212 | + k_type = Regexp.last_match[:k_type] |
| 213 | + v_type = Regexp.last_match[:v_type] |
| 214 | + {}.tap do |hash| |
| 215 | + value.each do |k, v| |
| 216 | + hash[_deserialize(k_type, k)] = _deserialize(v_type, v) |
| 217 | + end |
| 218 | + end |
| 219 | + else |
| 220 | + # model |
| 221 | + temp_model = AsposeWordsCloud.const_get(type).new |
| 222 | + temp_model.build_from_hash(value) |
| 223 | + end |
| 224 | + end |
| 225 | + |
| 226 | + # Returns the string representation of the object |
| 227 | + # @return [String] String presentation of the object |
| 228 | + def to_s |
| 229 | + to_hash.to_s |
| 230 | + end |
| 231 | + |
| 232 | + # to_body is an alias to to_hash (backward compatibility) |
| 233 | + # @return [Hash] Returns the object in the form of hash |
| 234 | + def to_body |
| 235 | + to_hash |
| 236 | + end |
| 237 | + |
| 238 | + # Returns the object in the form of hash |
| 239 | + # @return [Hash] Returns the object in the form of hash |
| 240 | + def to_hash |
| 241 | + hash = {} |
| 242 | + self.class.attribute_map.each_pair do |attr, param| |
| 243 | + value = self.send(attr) |
| 244 | + next if value.nil? |
| 245 | + hash[param] = _to_hash(value) |
| 246 | + end |
| 247 | + hash |
| 248 | + end |
| 249 | + |
| 250 | + # Outputs non-array value in the form of hash |
| 251 | + # For object, use to_hash. Otherwise, just return the value |
| 252 | + # @param [Object] value Any valid value |
| 253 | + # @return [Hash] Returns the value in the form of hash |
| 254 | + def _to_hash(value) |
| 255 | + if value.is_a?(Array) |
| 256 | + value.compact.map { |v| _to_hash(v) } |
| 257 | + elsif value.is_a?(Hash) |
| 258 | + {}.tap do |hash| |
| 259 | + value.each { |k, v| hash[k] = _to_hash(v) } |
| 260 | + end |
| 261 | + elsif value.respond_to? :to_hash |
| 262 | + value.to_hash |
| 263 | + else |
| 264 | + value |
| 265 | + end |
| 266 | + end |
| 267 | + |
| 268 | + end |
| 269 | +end |
0 commit comments