summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2020-10-20 21:34:14 +0900
committerGitHub <noreply@github.com>2020-10-20 21:34:14 +0900
commitd76e5fc1d2cedeba29fe7a5292f5d451dea0da57 (patch)
treea7b26f7d3d1debb29000b1de774e3c62a49170cf /lib
parent01e4823ee1b9837d7289ae4eb35c9d74535e0f5e (diff)
parent9bf8aa21b36de3bd90bed85e6c85412426b03d9d (diff)
downloadjson-d76e5fc1d2cedeba29fe7a5292f5d451dea0da57.tar.gz
Merge pull request #447 from Shopify/global-freeze-option
Implement a freeze: parser option
Diffstat (limited to 'lib')
-rw-r--r--lib/json/pure/parser.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb
index 6f37e0c..d795a3b 100644
--- a/lib/json/pure/parser.rb
+++ b/lib/json/pure/parser.rb
@@ -61,6 +61,8 @@ module JSON
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
# defiance of RFC 7159 to be parsed by the Parser. This option defaults
# to false.
+ # * *freeze*: If set to true, all parsed objects will be frozen. Parsed
+ # string will be deduplicated if possible.
# * *symbolize_names*: If set to true, returns symbols for the names
# (keys) in a JSON object. Otherwise strings are returned, which is
# also the default. It's not possible to use this option in
@@ -86,6 +88,7 @@ module JSON
end
@allow_nan = !!opts[:allow_nan]
@symbolize_names = !!opts[:symbolize_names]
+ @freeze = !!opts[:freeze]
if opts.key?(:create_additions)
@create_additions = !!opts[:create_additions]
else
@@ -120,6 +123,7 @@ module JSON
obj = parse_value
UNPARSED.equal?(obj) and raise ParserError,
"source is not valid JSON!"
+ obj.freeze if @freeze
end
while !eos? && skip(IGNORE) do end
eos? or raise ParserError, "source is not valid JSON!"
@@ -161,6 +165,7 @@ module JSON
EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
end
+ STR_UMINUS = ''.respond_to?(:-@)
def parse_string
if scan(STRING)
return '' if self[1].empty?
@@ -180,6 +185,15 @@ module JSON
if string.respond_to?(:force_encoding)
string.force_encoding(::Encoding::UTF_8)
end
+
+ if @freeze
+ if STR_UMINUS
+ string = -string
+ else
+ string.freeze
+ end
+ end
+
if @create_additions and @match_string
for (regexp, klass) in @match_string
klass.json_creatable? or next