summaryrefslogtreecommitdiff
path: root/lib
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 /lib
parent79074a0de768113364749cb584000b7e97be04e5 (diff)
downloadjson-ac0e272c9d2cb3c6d692ecefe2d1d8e5c1201120.tar.gz
Only support newer Rubies e. g. Encoding
Diffstat (limited to 'lib')
-rw-r--r--lib/json/pure/generator.rb148
1 files changed, 47 insertions, 101 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