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.

Methods
Classes and Modules
Class JSON::Parser
Class JSON::State
Constants
JSONError = Class.new StandardError
  The base exception for JSON errors.
ParserError = Class.new JSONError
  This exception is raise, if a parser error occurs.
UnparserError = Class.new JSONError
  This exception is raise, if a unparser error occurs.
CircularDatastructure = Class.new UnparserError
  If a circular data structure is encountered while unparsing this exception is raised.
UTF16toUTF8 = Iconv.new('utf-8', 'utf-16be')
  An iconv instance to convert from UTF8 to UTF16 Big Endian.
UTF8toUTF16 = Iconv.new('utf-16be', 'utf-8');
  An iconv instance to convert from UTF16 Big Endian to UTF8.
Public Class methods
support_unicode=(enable)

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

# File lib/facets/more/json.rb, line 156
    def support_unicode=(enable)
      @support_unicode = enable
    end
support_unicode?()

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

# File lib/facets/more/json.rb, line 161
    def support_unicode?
      !!@support_unicode
    end
Public Instance methods
parse(source)

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

# File lib/facets/more/json.rb, line 459
  def parse(source)
    Parser.new(source).parse
  end
pretty_unparse(obj)

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.

# File lib/facets/more/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
unparse(obj, state = nil)

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.

# File lib/facets/more/json.rb, line 466
  def unparse(obj, state = nil)
    obj.to_json(JSON::State.from_state(state))
  end
utf16_to_utf8(string)

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

# File lib/facets/more/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)

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

# File lib/facets/more/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)

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

# File lib/facets/more/json.rb, line 404
  def utf8_to_utf16(string)
    JSON::UTF8toUTF16.iconv(string).unpack('H*')[0]
  end