summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2016-06-03 13:05:23 +0200
committerFlorian Frank <flori@ping.de>2016-06-03 13:05:23 +0200
commitac0e272c9d2cb3c6d692ecefe2d1d8e5c1201120 (patch)
tree25322f83434466aedebd3bb391ed189b98aaa4b7
parent79074a0de768113364749cb584000b7e97be04e5 (diff)
downloadjson-ac0e272c9d2cb3c6d692ecefe2d1d8e5c1201120.tar.gz
Only support newer Rubies e. g. Encoding
-rw-r--r--lib/json/pure/generator.rb148
-rw-r--r--tests/json_encoding_test.rb10
-rw-r--r--tests/json_ext_parser_test.rb3
-rw-r--r--tests/json_generator_test.rb1
4 files changed, 48 insertions, 114 deletions
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index cf4afff..98bc369 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -38,85 +38,45 @@ module JSON
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
# UTF16 big endian characters as \u????, and return it.
- if defined?(::Encoding)
- def utf8_to_json(string) # :nodoc:
- string = string.dup
- string.force_encoding(::Encoding::ASCII_8BIT)
- string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
- string.force_encoding(::Encoding::UTF_8)
- string
- end
-
- def utf8_to_json_ascii(string) # :nodoc:
- string = string.dup
- string.force_encoding(::Encoding::ASCII_8BIT)
- 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, "invalid utf8 byte: '#{c}'"
- s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
- s.force_encoding(::Encoding::ASCII_8BIT)
- s.gsub!(/.{4}/n, '\\\\u\&')
- s.force_encoding(::Encoding::UTF_8)
- }
- string.force_encoding(::Encoding::UTF_8)
- string
- rescue => e
- raise GeneratorError.wrap(e)
- end
-
- def valid_utf8?(string)
- encoding = string.encoding
- (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) &&
- string.valid_encoding?
- end
- module_function :valid_utf8?
- else
- def utf8_to_json(string) # :nodoc:
- string.gsub(/["\\\x0-\x1f]/n) { MAP[$&] }
- end
+ def utf8_to_json(string) # :nodoc:
+ string = string.dup
+ string.force_encoding(::Encoding::ASCII_8BIT)
+ string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
+ string.force_encoding(::Encoding::UTF_8)
+ string
+ end
- def utf8_to_json_ascii(string) # :nodoc:
- string = 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.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
- s.gsub!(/.{4}/n, '\\\\u\&')
- }
- string
- rescue => e
- raise GeneratorError.wrap(e)
- end
+ def utf8_to_json_ascii(string) # :nodoc:
+ string = string.dup
+ string.force_encoding(::Encoding::ASCII_8BIT)
+ 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, "invalid utf8 byte: '#{c}'"
+ s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
+ s.force_encoding(::Encoding::ASCII_8BIT)
+ s.gsub!(/.{4}/n, '\\\\u\&')
+ s.force_encoding(::Encoding::UTF_8)
+ }
+ string.force_encoding(::Encoding::UTF_8)
+ string
+ rescue => e
+ raise GeneratorError.wrap(e)
+ end
- def valid_utf8?(string)
- string =~
- /\A( [\x09\x0a\x0d\x20-\x7e] # ASCII
- | [\xc2-\xdf][\x80-\xbf] # non-overlong 2-byte
- | \xe0[\xa0-\xbf][\x80-\xbf] # excluding overlongs
- | [\xe1-\xec\xee\xef][\x80-\xbf]{2} # straight 3-byte
- | \xed[\x80-\x9f][\x80-\xbf] # excluding surrogates
- | \xf0[\x90-\xbf][\x80-\xbf]{2} # planes 1-3
- | [\xf1-\xf3][\x80-\xbf]{3} # planes 4-15
- | \xf4[\x80-\x8f][\x80-\xbf]{2} # plane 16
- )*\z/nx
- end
+ def valid_utf8?(string)
+ encoding = string.encoding
+ (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) &&
+ string.valid_encoding?
end
module_function :utf8_to_json, :utf8_to_json_ascii, :valid_utf8?
-
module Pure
module Generator
# This class is used to create State instances, that are use to hold data
@@ -426,34 +386,20 @@ module JSON
end
module String
- if defined?(::Encoding)
- # This string should be encoded with UTF-8 A call to this method
- # returns a JSON string encoded with UTF16 big endian characters as
- # \u????.
- def to_json(state = nil, *args)
- state = State.from_state(state)
- if encoding == ::Encoding::UTF_8
- string = self
- else
- string = encode(::Encoding::UTF_8)
- end
- if state.ascii_only?
- '"' << JSON.utf8_to_json_ascii(string) << '"'
- else
- '"' << JSON.utf8_to_json(string) << '"'
- end
+ # This string should be encoded with UTF-8 A call to this method
+ # returns a JSON string encoded with UTF16 big endian characters as
+ # \u????.
+ def to_json(state = nil, *args)
+ state = State.from_state(state)
+ if encoding == ::Encoding::UTF_8
+ string = self
+ else
+ string = encode(::Encoding::UTF_8)
end
- else
- # This string should be encoded with UTF-8 A call to this method
- # returns a JSON string encoded with UTF16 big endian characters as
- # \u????.
- def to_json(state = nil, *args)
- state = State.from_state(state)
- if state.ascii_only?
- '"' << JSON.utf8_to_json_ascii(self) << '"'
- else
- '"' << JSON.utf8_to_json(self) << '"'
- end
+ if state.ascii_only?
+ '"' << JSON.utf8_to_json_ascii(string) << '"'
+ else
+ '"' << JSON.utf8_to_json(string) << '"'
end
end
diff --git a/tests/json_encoding_test.rb b/tests/json_encoding_test.rb
index c220f90..f8b88d2 100644
--- a/tests/json_encoding_test.rb
+++ b/tests/json_encoding_test.rb
@@ -34,15 +34,7 @@ class JSONEncodingTest < Test::Unit::TestCase
def test_generate
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
- if defined?(::Encoding)
- assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
- else
- # XXX checking of correct utf8 data is not as strict (yet?) without
- # :ascii_only
- assert_raises(JSON::GeneratorError) do
- JSON.generate(@utf_16_data, :ascii_only => true)
- end
- end
+ assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
end
def test_unicode
diff --git a/tests/json_ext_parser_test.rb b/tests/json_ext_parser_test.rb
index da3bdc1..ade9784 100644
--- a/tests/json_ext_parser_test.rb
+++ b/tests/json_ext_parser_test.rb
@@ -1,7 +1,4 @@
require 'test_helper'
-require 'stringio'
-require 'tempfile'
-require 'ostruct'
class JSONExtParserTest < Test::Unit::TestCase
if defined?(JSON::Ext::Parser)
diff --git a/tests/json_generator_test.rb b/tests/json_generator_test.rb
index 75e19d9..7087471 100644
--- a/tests/json_generator_test.rb
+++ b/tests/json_generator_test.rb
@@ -372,6 +372,5 @@ EOT
assert_nothing_raised(SystemStackError) do
assert_equal '["foo"]', JSON.generate([s.new('foo')])
end
-
end
end