Method: JSON.utf8_to_json

Defined in:
lib/crazy_ivan/vendor/json-1.1.7/lib/json/pure/generator.rb,
lib/crazy_ivan/vendor/json-1.1.7/lib/json/pure/generator.rb

.utf8_to_json(string) ⇒ Object

:nodoc:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/crazy_ivan/vendor/json-1.1.7/lib/json/pure/generator.rb', line 43

def utf8_to_json(string) # :nodoc:
  string = string.dup
  string << '' # XXX workaround: avoid buffer sharing
  string.force_encoding(Encoding::ASCII_8BIT)
  string.gsub!(/["\\\/\x0-\x1f]/) { 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, "invalid utf8 byte: '#{c}'"
                  s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
                  s.gsub!(/.{4}/n, '\\\\u\&')
                }
  string.force_encoding(Encoding::UTF_8)
  string
rescue Iconv::Failure => e
  raise GeneratorError, "Caught #{e.class}: #{e}"
end