Module | JSON |
In: |
lib/json.rb
lib/json/editor.rb |
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.
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. |
Switches on Unicode support, if enable is true. Otherwise switches Unicode support off.
# File lib/json.rb, line 138 138: def support_unicode=(enable) 139: @support_unicode = enable 140: 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.
# File lib/json.rb, line 454 454: def pretty_unparse(obj) 455: state = JSON::State.new( 456: :indent => ' ', 457: :space => ' ', 458: :object_nl => "\n", 459: :array_nl => "\n" 460: ) 461: obj.to_json(state) 462: 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.
# File lib/json.rb, line 448 448: def unparse(obj, state = nil) 449: obj.to_json(JSON::State.from_state(state)) 450: end
Convert string from UTF16 (big endian) encoding to UTF8 encoding and return it.
# File lib/json.rb, line 392 392: def utf16_to_utf8(string) 393: bytes = '' << string[0, 2].to_i(16) << string[2, 2].to_i(16) 394: JSON::UTF16toUTF8.iconv(bytes) 395: end
Convert a UTF8 encoded Ruby string string to a JSON string, encoded with UTF16 big endian characters as \u????, and return it.
# File lib/json.rb, line 399 399: def utf8_to_json(string) 400: i, n, result = 0, string.size, '' 401: while i < n 402: char = string[i] 403: case 404: when char == ?\b then result << '\b' 405: when char == ?\t then result << '\t' 406: when char == ?\n then result << '\n' 407: when char == ?\f then result << '\f' 408: when char == ?\r then result << '\r' 409: when char == ?" then result << '\"' 410: when char == ?\\ then result << '\\\\' 411: when char.between?(0x0, 0x1f) then result << "\\u%04x" % char 412: when char.between?(0x20, 0x7f) then result << char 413: when !(JSON.support_unicode? && $KCODE == 'UTF8') 414: # if utf8 mode is switched off or unicode not supported, just pass 415: # bytes through: 416: result << char 417: when char & 0xe0 == 0xc0 418: result << '\u' << utf8_to_utf16(string[i, 2]) 419: i += 1 420: when char & 0xf0 == 0xe0 421: result << '\u' << utf8_to_utf16(string[i, 3]) 422: i += 2 423: when char & 0xf8 == 0xf0 424: result << '\u' << utf8_to_utf16(string[i, 4]) 425: i += 3 426: when char & 0xfc == 0xf8 427: result << '\u' << utf8_to_utf16(string[i, 5]) 428: i += 4 429: when char & 0xfe == 0xfc 430: result << '\u' << utf8_to_utf16(string[i, 6]) 431: i += 5 432: else 433: raise JSON::UnparserError, "Encountered unknown UTF-8 byte: %x!" % char 434: end 435: i += 1 436: end 437: result 438: end