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', '"' => '\"', '\\' => '\\\\', '<' => '\\u003c', }.freeze
- ESCAPE_PATTERN =
:nodoc:
/["\\\x0-\x1f]/- SCRIPT_SAFE_ESCAPE_PATTERN =
Regexp.union(ESCAPE_PATTERN, /<(?=\/?script)/i, /<(?=!--)/)
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:
51 52 53 |
# File 'lib/json/truffle_ruby/generator.rb', line 51 def self.native_key?(key) # :nodoc: (Symbol === key || String === key) end |
.native_type?(value) ⇒ Boolean
:nodoc:
47 48 49 |
# File 'lib/json/truffle_ruby/generator.rb', line 47 def self.native_type?(value) # :nodoc: (false == value || true == value || nil == value || String === value || Symbol === 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.
62 63 64 65 66 67 68 69 |
# File 'lib/json/truffle_ruby/generator.rb', line 62 def self.utf8_to_json(string, script_safe = false) # :nodoc: pattern = script_safe ? SCRIPT_SAFE_ESCAPE_PATTERN : ESCAPE_PATTERN if pattern.match?(string) string.gsub(pattern, MAP) else string end end |
.utf8_to_json_ascii(original_string, script_safe = false) ⇒ Object
:nodoc:
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/json/truffle_ruby/generator.rb', line 71 def self.utf8_to_json_ascii(original_string, script_safe = false) # :nodoc: string = utf8_to_json(original_string, script_safe).b 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:
55 56 57 58 |
# File 'lib/json/truffle_ruby/generator.rb', line 55 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
93 94 95 96 97 |
# File 'lib/json/truffle_ruby/generator.rb', line 93 def self.valid_utf8?(string) encoding = string.encoding (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) && string.valid_encoding? end |