Class: MicroJSON::Decoder

Inherits:
Object show all
Defined in:
lib/moon/packages/micro_json/micro_json.rb

Overview

Class for decoding JSON streams.

Defined Under Namespace

Classes: InvalidNumeric, ReadError, StringCursor, UnexpectedChar, UnexpectedEnd, UnexpectedString

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Decoder

Returns a new instance of Decoder.


236
237
238
# File 'lib/moon/packages/micro_json/micro_json.rb', line 236

def initialize(options)
  @options = options
end

Class Method Details

.decode(str, options = {}, depth = 0) ⇒ Object

Decodes a JSON string as ruby.

Parameters:

Returns:


527
528
529
# File 'lib/moon/packages/micro_json/micro_json.rb', line 527

def self.decode(str, options = {}, depth = 0)
  new(options).decode(str, depth)
end

Instance Method Details

#decode(str, depth = 0) ⇒ Object

Decodes a JSON string as ruby.

Parameters:

Returns:


518
519
520
# File 'lib/moon/packages/micro_json/micro_json.rb', line 518

def decode(str, depth = 0)
  read_value StringCursor.new(str), depth
end

#read_array_body(cur, depth = 0) ⇒ Array

Reads the contents of an Array after after a [ character.

Parameters:

Returns:

Raises:


454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/moon/packages/micro_json/micro_json.rb', line 454

def read_array_body(cur, depth = 0)
  skip_spaces cur, depth + 1
  a = []
  end_found = false
  until cur.eos?
    case cur.char
    when ']'
      cur.next
      end_found = true
      break
    when ','
      cur.next
    else
      skip_spaces cur, depth + 1
      a << read_value(cur, depth + 1)
      read_array_continuation cur, depth + 1
    end
  end
  raise UnexpectedEnd, 'Unexpected end of Array.' unless end_found
  a
end

#read_array_continuation(cur, depth = 0) ⇒ Object

Skips along in the stream until a ], or , is encountered

Parameters:


445
446
447
# File 'lib/moon/packages/micro_json/micro_json.rb', line 445

def read_array_continuation(cur, depth = 0)
  read_boundry ']', cur, depth
end

#read_boundry(c, cur, depth = 0) ⇒ Object

Reads until a , or the provided character c is reached.

Parameters:

Raises:


421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/moon/packages/micro_json/micro_json.rb', line 421

def read_boundry(c, cur, depth = 0)
  #puts "reading boundry for #{c}"
  skip_spaces cur, depth + 1
  until cur.eos?
    case cur.char
    when c
      return
    when ','
      return
    when '//'
      skip_line cur, depth + 1
    when ' ', "\t", "\n"
      cur.next
    else
      raise
    end
  end
  raise UnexpectedEnd, 'Unexpected end.' unless end_found
end

#read_keyword(cur, depth = 0) ⇒ Object

Reads the next sequence in the stream as a keyword.

Parameters:

Returns:

  • (Object)

    value of the keyword


368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/moon/packages/micro_json/micro_json.rb', line 368

def read_keyword(cur, depth = 0)
  if cur.string(4) == 'true'
    cur.index += 4
    return true
  elsif cur.string(5) == 'false'
    cur.index += 5
    return false
  elsif cur.string(4) == 'null'
    cur.index += 4
    return nil
  else
    return UnexpectedString, 'expected false, null, or true.'
  end
end

#read_numeric(cur, depth = 0) ⇒ Numeric

Reads the next sequence in the stream as a Numeric value.

Parameters:

Returns:


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/moon/packages/micro_json/micro_json.rb', line 329

def read_numeric(cur, depth = 0)
  skip_spaces cur, depth + 1
  base = 10
  n = ''
  f = false # is float
  neg = false
  if cur.char == '-' || cur.char == '+'
    n << cur.char
    cur.next
  end
  until cur.eos?
    case c = cur.char
    when '0'..'9'
      n << c
    when 'A'..'F', 'a'..'f'
      unless base == 16
        raise UnexpectedChar, "reading HEX values for non HEX value."
      end
      n << c
    when 'x', 'X'
      raise InvalidNumeric, "cannot read a HEX and float value." if f
      base = 16
    when '.'
      raise InvalidNumeric, "cannot read a HEX and float value." if base == 16
      f = true
      n << c
    else
      break
    end
    cur.next
  end
  f ? n.to_f : n.to_i(base)
end

#read_object_body(cur, depth = 0) ⇒ Hash

Reads the contents of an Object after the { character

Parameters:

Returns:

Raises:


490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/moon/packages/micro_json/micro_json.rb', line 490

def read_object_body(cur, depth = 0)
  skip_spaces cur
  h = {}
  end_found = false
  until cur.eos?
    case cur.char
    when '}'
      cur.next
      end_found = true
      break
    when ','
      cur.next
    else
      key = read_object_key cur, depth + 1
      value = read_value cur, depth + 1
      h[key] = value
      read_object_continuation cur, depth + 1
    end
  end
  raise UnexpectedEnd, 'Unexpected end of Hash/Object.' unless end_found
  h
end

#read_object_continuation(cur, depth = 0) ⇒ Void

Reads stream until a continuation or end character is found.

Parameters:

Returns:

  • (Void)

481
482
483
# File 'lib/moon/packages/micro_json/micro_json.rb', line 481

def read_object_continuation(cur, depth = 0)
  read_boundry '}', cur, depth
end

#read_object_key(cur, depth = 0) ⇒ String

Reads a String using #read_string followed by a :. If the next character after the string is not a :, and UnexpectedChar error is raised.

Parameters:

Returns:


314
315
316
317
318
319
320
321
322
# File 'lib/moon/packages/micro_json/micro_json.rb', line 314

def read_object_key(cur, depth = 0)
  s = read_string cur, depth + 1
  skip_spaces cur
  unless cur.char == ':'
    raise UnexpectedChar, "expected : (got #{cur.char})"
  end
  cur.next
  s
end

#read_string(cur, depth = 0) ⇒ String

Reads the first “ that can be found from the current cursor position. If the first valid character is not an ”, an UnexpectedChar error is raised.

Parameters:

Returns:


298
299
300
301
302
303
304
305
# File 'lib/moon/packages/micro_json/micro_json.rb', line 298

def read_string(cur, depth = 0)
  skip_spaces cur, depth + 1
  unless cur.char == '"'
    raise UnexpectedChar, "expected \" (got #{cur.char})"
  end
  cur.next
  read_string_body cur
end

#read_string_body(cur, depth = 0) ⇒ String

Reads the stream value after a “ character as a String.

Parameters:

Returns:

Raises:


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/moon/packages/micro_json/micro_json.rb', line 267

def read_string_body(cur, depth = 0)
  s = ''
  end_found = false
  until cur.eos?
    case c = cur.char
    when '\\'
      cur.next
      break if cur.eos?
      s << cur.char
      cur.next
    when '"'
      end_found = true
      cur.next
      break
    else
      s << c
      cur.next
    end
  end
  raise UnexpectedEnd, 'Unexpected end of string.' unless end_found
  s = s.slice(1, s.length - 1).to_sym if @options[:symbols] && s.start_with?(':')
  s
end

#read_value(cur, depth = 0) ⇒ Object

Reads the next value from the sequence.

Parameters:

Returns:


388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/moon/packages/micro_json/micro_json.rb', line 388

def read_value(cur, depth = 0)
  skip_spaces cur, depth + 1
  until cur.eos?
    case cur.char
    when 'a'..'z', 'A'..'Z'
      return read_keyword cur, depth + 1
    when '-', '+', '0'..'9'
      return read_numeric cur, depth + 1
    when '['
      cur.next
      return read_array_body cur, depth + 1
    when '"'
      cur.next
      return read_string_body cur, depth + 1
    when '{'
      cur.next
      return read_object_body cur, depth + 1
    # comment
    when '//'
      skip_line cur, depth + 1
    when ' ', "\t", "\n"
      cur.next
    else
      break
    end
  end
end

#skip_line(cur, depth = 0) ⇒ Object

Skips all characters in the stream until a newline or the end is encountered.

Parameters:


245
246
247
248
# File 'lib/moon/packages/micro_json/micro_json.rb', line 245

def skip_line(cur, depth = 0)
  cur.next until cur.eos? || cur.char == "\n"
  cur.next
end

#skip_spaces(cur, depth = 0) ⇒ Object

Skips all spaces, tabs and newlines in the stream until something else is encountered.

Parameters:


255
256
257
258
259
260
# File 'lib/moon/packages/micro_json/micro_json.rb', line 255

def skip_spaces(cur, depth = 0)
  # skip spaces, tabs and new lines
  while cur.char == " " || cur.char == "\t" || cur.char == "\n"
    cur.next
  end
end