Class: DatawireQuarkCore::JSONObject
- Inherits:
-
Object
- Object
- DatawireQuarkCore::JSONObject
- Defined in:
- lib/datawire-quark-core.rb
Constant Summary collapse
- UNDEFINED =
Undefined
Class.new
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#getBool ⇒ Object
Bool.
- #getListItem(index) ⇒ Object
- #getNumber ⇒ Object
- #getObjectItem(key) ⇒ Object
- #getString ⇒ Object
- #getType ⇒ Object
-
#initialize(value = nil) ⇒ JSONObject
constructor
A new instance of JSONObject.
- #isBool ⇒ Object
- #isDefined ⇒ Object
-
#isList ⇒ Object
List.
-
#isNull ⇒ Object
Null.
-
#isNumber ⇒ Object
Number.
-
#isObject ⇒ Object
Object.
-
#isString ⇒ Object
String.
- #isUndefined ⇒ Object
- #keys ⇒ Object
- #set(value) ⇒ Object (also: #setString, #setNumber, #setBool)
- #setList ⇒ Object
- #setListItem(index, item) ⇒ Object
- #setNull ⇒ Object
- #setObject ⇒ Object
- #setObjectItem(key, item) ⇒ Object
- #size ⇒ Object
- #toString ⇒ Object
- #undefined ⇒ Object
Constructor Details
#initialize(value = nil) ⇒ JSONObject
Returns a new instance of JSONObject.
191 192 193 |
# File 'lib/datawire-quark-core.rb', line 191 def initialize(value=nil) @value = value end |
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
189 190 191 |
# File 'lib/datawire-quark-core.rb', line 189 def value @value end |
Class Method Details
.parse(json) ⇒ Object
195 196 197 |
# File 'lib/datawire-quark-core.rb', line 195 def self.parse(json) new JSON.parse("[#{json}]")[0] end |
Instance Method Details
#==(other) ⇒ Object
199 200 201 |
# File 'lib/datawire-quark-core.rb', line 199 def ==(other) not other.nil? and value == other.value end |
#getBool ⇒ Object
Bool
322 323 324 325 326 |
# File 'lib/datawire-quark-core.rb', line 322 def getBool return nil unless isBool value end |
#getListItem(index) ⇒ Object
279 280 281 282 283 |
# File 'lib/datawire-quark-core.rb', line 279 def getListItem(index) return undefined unless isList and (0...size).include? index self.class.new value[index] end |
#getNumber ⇒ Object
312 313 314 315 316 |
# File 'lib/datawire-quark-core.rb', line 312 def getNumber return nil unless isNumber value end |
#getObjectItem(key) ⇒ Object
248 249 250 251 252 |
# File 'lib/datawire-quark-core.rb', line 248 def getObjectItem(key) return undefined unless isObject and value.key? key self.class.new value[key] end |
#getString ⇒ Object
298 299 300 301 302 |
# File 'lib/datawire-quark-core.rb', line 298 def getString return nil unless isString value end |
#getType ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/datawire-quark-core.rb', line 211 def getType case @value when Hash 'object' when Array 'list' when String 'string' when Numeric 'number' when true, false 'bool' when nil 'null' else raise RuntimeError, "Unknown JSONObject type #{@value.inspect}" end end |
#isBool ⇒ Object
328 329 330 |
# File 'lib/datawire-quark-core.rb', line 328 def isBool [true, false].include? value end |
#isDefined ⇒ Object
354 355 356 |
# File 'lib/datawire-quark-core.rb', line 354 def isDefined not isUndefined end |
#isList ⇒ Object
List
269 270 271 |
# File 'lib/datawire-quark-core.rb', line 269 def isList value.is_a? Array end |
#isNull ⇒ Object
Null
336 337 338 |
# File 'lib/datawire-quark-core.rb', line 336 def isNull value == nil end |
#isNumber ⇒ Object
Number
308 309 310 |
# File 'lib/datawire-quark-core.rb', line 308 def isNumber value.is_a? Numeric end |
#isObject ⇒ Object
Object
238 239 240 |
# File 'lib/datawire-quark-core.rb', line 238 def isObject value.is_a? Hash end |
#isString ⇒ Object
String
294 295 296 |
# File 'lib/datawire-quark-core.rb', line 294 def isString value.is_a? String end |
#isUndefined ⇒ Object
350 351 352 |
# File 'lib/datawire-quark-core.rb', line 350 def isUndefined UNDEFINED.equal? value end |
#keys ⇒ Object
261 262 263 264 265 |
# File 'lib/datawire-quark-core.rb', line 261 def keys return undefined unless isObject List.new value.keys end |
#set(value) ⇒ Object Also known as: setString, setNumber, setBool
230 231 232 233 234 |
# File 'lib/datawire-quark-core.rb', line 230 def set(value) @value = value self end |
#setList ⇒ Object
273 274 275 276 277 |
# File 'lib/datawire-quark-core.rb', line 273 def setList @value = List.new self end |
#setListItem(index, item) ⇒ Object
285 286 287 288 289 290 |
# File 'lib/datawire-quark-core.rb', line 285 def setListItem(index, item) setList unless isList value[index] = item.value self end |
#setNull ⇒ Object
340 341 342 343 344 |
# File 'lib/datawire-quark-core.rb', line 340 def setNull @value = nil self end |
#setObject ⇒ Object
242 243 244 245 246 |
# File 'lib/datawire-quark-core.rb', line 242 def setObject @value = {} self end |
#setObjectItem(key, item) ⇒ Object
254 255 256 257 258 259 |
# File 'lib/datawire-quark-core.rb', line 254 def setObjectItem(key, item) setObject unless isObject value[key] = item.value self end |
#size ⇒ Object
207 208 209 |
# File 'lib/datawire-quark-core.rb', line 207 def size isList || isObject ? value.size : 1 end |
#toString ⇒ Object
203 204 205 |
# File 'lib/datawire-quark-core.rb', line 203 def toString value.to_json end |
#undefined ⇒ Object
358 359 360 |
# File 'lib/datawire-quark-core.rb', line 358 def undefined self.class.new UNDEFINED end |