Module JSON
In: lib/json/editor.rb
lib/json.rb
StringScanner Parser JSONTreeView MainWindow Gtk::TreeView OptionsMenu EditMenu PopUpMenu FileMenu Gtk::Window Enumerable TreeIter Gtk State lib/json.rb lib/json/editor.rb MenuExtension lib/json/editor.rb Gtk Editor JSON dot/m_1_0.png

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

Module JSON::Editor
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.
UTF16toUTF8 = Iconv.new('utf-8', 'utf-16')   An iconv instance to convert from UTF8 to UTF16 Big Endian.
UTF8toUTF16 = Iconv.new('utf-16', 'utf-8');   An iconv instance to convert from UTF16 Big Endian to UTF8.
UTF8toUTF16 = swapper.new(UTF8toUTF16)
UTF16toUTF8 = swapper.new(UTF16toUTF8)

Public Class methods

[Source]

     # File lib/json.rb, line 170
170:           def initialize(iconv)
171:             @iconv = iconv
172:           end

[Source]

     # File lib/json.rb, line 183
183:           def initialize(iconv)
184:             @iconv = iconv
185:           end

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

[Source]

     # File lib/json.rb, line 138
138:     def support_unicode=(enable)
139:       @support_unicode = enable
140:     end

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

If loading of the iconv library fails, or it doesn‘t support utf8/utf16 encoding, this will be set to false, as a fallback.

[Source]

     # File lib/json.rb, line 146
146:     def support_unicode?
147:       !!@support_unicode
148:     end

Swap consecutive bytes in string in place.

[Source]

     # File lib/json.rb, line 207
207:   def self.swap!(string)
208:     0.upto(string.size / 2) do |i|
209:       break unless string[2 * i + 1]
210:       string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
211:     end
212:     string
213:   end

Public Instance methods

[Source]

     # File lib/json.rb, line 187
187:           def iconv(string)
188:             string = JSON.swap!(string.dup)
189:             @iconv.iconv(string)
190:           end

[Source]

     # File lib/json.rb, line 174
174:           def iconv(string)
175:             result = @iconv.iconv(string)
176:             JSON.swap!(result)
177:           end

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

[Source]

     # File lib/json.rb, line 497
497:   def parse(source)
498:     Parser.new(source).parse
499:   end

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.

[Source]

     # File lib/json.rb, line 510
510:   def pretty_unparse(obj)
511:     state = JSON::State.new(
512:       :indent     => '  ',
513:       :space      => ' ',
514:       :object_nl  => "\n",
515:       :array_nl   => "\n"
516:     )
517:     obj.to_json(state)
518:   end

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.

[Source]

     # File lib/json.rb, line 504
504:   def unparse(obj, state = nil)
505:     obj.to_json(JSON::State.from_state(state))
506:   end

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

[Source]

     # File lib/json.rb, line 447
447:   def utf16_to_utf8(string)
448:     bytes = '' << string[0, 2].to_i(16) << string[2, 2].to_i(16)
449:     JSON::UTF16toUTF8.iconv(bytes)
450:   end

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

[Source]

     # File lib/json.rb, line 454
454:   def utf8_to_json(string)
455:     i, n, result = 0, string.size, ''
456:     while i < n
457:       char = string[i]
458:       case
459:       when char == ?\b then result << '\b'
460:       when char == ?\t then result << '\t'
461:       when char == ?\n then result << '\n'
462:       when char == ?\f then result << '\f'
463:       when char == ?\r then result << '\r'
464:       when char == ?"  then result << '\"'
465:       when char == ?\\ then result << '\\\\'
466:       when char == ?/ then result << '\/'
467:       when char.between?(0x0, 0x1f) then result << "\\u%04x" % char
468:       when char.between?(0x20, 0x7f) then result << char
469:       when !(JSON.support_unicode? && $KCODE == 'UTF8')
470:         # if utf8 mode is switched off or unicode not supported, just pass
471:         # bytes through:
472:         result << char
473:       when char & 0xe0 == 0xc0
474:         result << '\u' << utf8_to_utf16(string[i, 2])
475:         i += 1
476:       when char & 0xf0 == 0xe0
477:         result << '\u' << utf8_to_utf16(string[i, 3])
478:         i += 2
479:       when char & 0xf8 == 0xf0
480:         result << '\u' << utf8_to_utf16(string[i, 4])
481:         i += 3
482:       when char & 0xfc == 0xf8
483:         result << '\u' << utf8_to_utf16(string[i, 5])
484:         i += 4
485:       when char & 0xfe == 0xfc
486:         result << '\u' << utf8_to_utf16(string[i, 6])
487:         i += 5
488:       else
489:         raise JSON::UnparserError, "Encountered unknown UTF-8 byte: %x!" % char
490:       end
491:       i += 1
492:     end
493:     result
494:   end

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

[Source]

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

[Validate]