Module: JSON

Defined in:
lib/facets/json.rb

Overview

This module is the namespace for all the JSON related classes. It also defines some module functions to expose a nicer API to users, instead of using the parser and other classes directly.

Defined Under Namespace

Classes: Parser, State

Constant Summary collapse

JSONError =

The base exception for JSON errors.

Class.new StandardError
ParserError =

This exception is raise, if a parser error occurs.

Class.new JSONError
UnparserError =

This exception is raise, if a unparser error occurs.

Class.new JSONError
CircularDatastructure =

If a circular data structure is encountered while unparsing this exception is raised.

Class.new UnparserError

Class Method Summary collapse

Class Method Details

.parse(source) ⇒ Object

Parse the JSON string source into a Ruby data structure and return it.



459
460
461
# File 'lib/facets/json.rb', line 459

def parse(source)
  Parser.new(source).parse
end

.pretty_unparse(obj) ⇒ Object

Unparse the Ruby data structure obj into a JSON string and return it. The returned string is a prettier form of the string returned by #unparse.



472
473
474
475
476
477
478
479
480
# File 'lib/facets/json.rb', line 472

def pretty_unparse(obj)
  state = JSON::State.new(
    :indent     => '  ',
    :space      => ' ',
    :object_nl  => "\n",
    :array_nl   => "\n"
  )
  obj.to_json(state)
end

.support_unicode=(enable) ⇒ Object

Switches on Unicode support, if enable is true. Otherwise switches Unicode support off.



156
157
158
# File 'lib/facets/json.rb', line 156

def support_unicode=(enable)
  @support_unicode = enable
end

.support_unicode?Boolean

Returns true if JSON supports unicode, otherwise false is returned.

Returns:

  • (Boolean)


161
162
163
# File 'lib/facets/json.rb', line 161

def support_unicode?
  !!@support_unicode
end

.unparse(obj, state = nil) ⇒ Object

Unparse the Ruby data structure obj into a single line JSON string and return it. state is a JSON::State object, that can be used to configure the output further.



466
467
468
# File 'lib/facets/json.rb', line 466

def unparse(obj, state = nil)
  obj.to_json(JSON::State.from_state(state))
end

.utf16_to_utf8(string) ⇒ Object

Convert string from UTF16 (big endian) encoding to UTF8 encoding and return it.



410
411
412
413
# File 'lib/facets/json.rb', line 410

def utf16_to_utf8(string)
  bytes = '' << string[0, 2].to_i(16) << string[2, 2].to_i(16)
  JSON::UTF16toUTF8.iconv(bytes)
end

.utf8_to_json(string) ⇒ Object

Convert a UTF8 encoded Ruby string string to a JSON string, encoded with UTF16 big endian characters as u????, and return it.



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/facets/json.rb', line 417

def utf8_to_json(string)
  i, n, result = 0, string.size, ''
  while i < n
    char = string[i]
    case
    when char == ?\b then result << '\b'
    when char == ?\t then result << '\t'
    when char == ?\n then result << '\n'
    when char == ?\f then result << '\f'
    when char == ?\r then result << '\r'
    when char == ?"  then result << '\"'
    when char == ?\\ then result << '\\\\'
    when char.between?(0x0, 0x1f) then result << "\\u%04x" % char
    when char.between?(0x20, 0x7f) then result << char
    when !(JSON.support_unicode? && $KCODE == 'UTF8')
      # if utf8 mode is switched off or unicode not supported, just pass
      # bytes through:
      result << char
    when char & 0xe0 == 0xc0
      result << '\u' << utf8_to_utf16(string[i, 2])
      i += 1
    when char & 0xf0 == 0xe0
      result << '\u' << utf8_to_utf16(string[i, 3])
      i += 2
    when char & 0xf8 == 0xf0
      result << '\u' << utf8_to_utf16(string[i, 4])
      i += 3
    when char & 0xfc == 0xf8
      result << '\u' << utf8_to_utf16(string[i, 5])
      i += 4
    when char & 0xfe == 0xfc
      result << '\u' << utf8_to_utf16(string[i, 6])
      i += 5
    else
      raise JSON::UnparserError, "Encountered unknown UTF-8 byte: %x!" % char
    end
    i += 1
  end
  result
end

.utf8_to_utf16(string) ⇒ Object

Convert string from UTF8 encoding to UTF16 (big endian) encoding and return it.



404
405
406
# File 'lib/facets/json.rb', line 404

def utf8_to_utf16(string)
  JSON::UTF8toUTF16.iconv(string).unpack('H*')[0]
end