Module: JSON::TruffleRuby::Generator
- Defined in:
- lib/json/truffle_ruby/generator.rb
Defined Under Namespace
Modules: GeneratorMethods Classes: State
Constant Summary collapse
- MAP =
{ "\x0" => '\u0000', "\x1" => '\u0001', "\x2" => '\u0002', "\x3" => '\u0003', "\x4" => '\u0004', "\x5" => '\u0005', "\x6" => '\u0006', "\x7" => '\u0007', "\b" => '\b', "\t" => '\t', "\n" => '\n', "\xb" => '\u000b', "\f" => '\f', "\r" => '\r', "\xe" => '\u000e', "\xf" => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011', "\x12" => '\u0012', "\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016', "\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001a', "\x1b" => '\u001b', "\x1c" => '\u001c', "\x1d" => '\u001d', "\x1e" => '\u001e', "\x1f" => '\u001f', '"' => '\"', '\\' => '\\\\', }.freeze
- SCRIPT_SAFE_MAP =
:nodoc:
MAP.merge( '/' => '\\/', "\u2028" => '\u2028', "\u2029" => '\u2029', ).freeze
- SCRIPT_SAFE_ESCAPE_PATTERN =
/[\/"\\\x0-\x1f\u2028-\u2029]/
Class Method Summary collapse
-
.native_key?(key) ⇒ Boolean
:nodoc:.
-
.native_type?(value) ⇒ Boolean
:nodoc:.
-
.utf8_to_json(string, script_safe = false) ⇒ Object
Convert a UTF8 encoded Ruby string string to a JSON string, encoded with UTF16 big endian characters as u????, and return it.
-
.utf8_to_json_ascii(original_string, script_safe = false) ⇒ Object
:nodoc:.
-
.valid_encoding?(string) ⇒ Boolean
:nodoc:.
- .valid_utf8?(string) ⇒ Boolean
Class Method Details
.native_key?(key) ⇒ Boolean
:nodoc:
54 55 56 |
# File 'lib/json/truffle_ruby/generator.rb', line 54 def self.native_key?(key) # :nodoc: (Symbol === key || String === key) end |
.native_type?(value) ⇒ Boolean
:nodoc:
50 51 52 |
# File 'lib/json/truffle_ruby/generator.rb', line 50 def self.native_type?(value) # :nodoc: (false == value || true == value || nil == value || String === value || Array === value || Hash === value || Integer === value || Float === value || Fragment === value) end |
.utf8_to_json(string, script_safe = false) ⇒ Object
Convert a UTF8 encoded Ruby string string to a JSON string, encoded with UTF16 big endian characters as u????, and return it.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/json/truffle_ruby/generator.rb', line 65 def self.utf8_to_json(string, script_safe = false) # :nodoc: if script_safe if SCRIPT_SAFE_ESCAPE_PATTERN.match?(string) string.gsub(SCRIPT_SAFE_ESCAPE_PATTERN, SCRIPT_SAFE_MAP) else string end else if /["\\\x0-\x1f]/.match?(string) string.gsub(/["\\\x0-\x1f]/, MAP) else string end end end |
.utf8_to_json_ascii(original_string, script_safe = false) ⇒ Object
:nodoc:
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/json/truffle_ruby/generator.rb', line 81 def self.utf8_to_json_ascii(original_string, script_safe = false) # :nodoc: string = original_string.b map = script_safe ? SCRIPT_SAFE_MAP : MAP string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& } string.gsub!(/( (?: [\xc2-\xdf][\x80-\xbf] | [\xe0-\xef][\x80-\xbf]{2} | [\xf0-\xf4][\x80-\xbf]{3} )+ | [\x80-\xc1\xf5-\xff] # invalid )/nx) { |c| c.size == 1 and raise GeneratorError.new("invalid utf8 byte: '#{c}'", original_string) s = c.encode(::Encoding::UTF_16BE, ::Encoding::UTF_8).unpack('H*')[0] s.force_encoding(::Encoding::BINARY) s.gsub!(/.{4}/n, '\\\\u\&') s.force_encoding(::Encoding::UTF_8) } string.force_encoding(::Encoding::UTF_8) string rescue => e raise GeneratorError.new(e., original_string) end |
.valid_encoding?(string) ⇒ Boolean
:nodoc:
58 59 60 61 |
# File 'lib/json/truffle_ruby/generator.rb', line 58 def self.valid_encoding?(string) # :nodoc: return false unless string.encoding == ::Encoding::UTF_8 || string.encoding == ::Encoding::US_ASCII string.is_a?(Symbol) || string.valid_encoding? end |
.valid_utf8?(string) ⇒ Boolean
105 106 107 108 109 |
# File 'lib/json/truffle_ruby/generator.rb', line 105 def self.valid_utf8?(string) encoding = string.encoding (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) && string.valid_encoding? end |