summaryrefslogtreecommitdiff
path: root/ext/json/lib
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/lib')
-rw-r--r--ext/json/lib/json.rb7
-rw-r--r--ext/json/lib/json/common.rb23
-rw-r--r--ext/json/lib/json/ext.rb6
-rw-r--r--ext/json/lib/json/ext/generator/state.rb105
4 files changed, 112 insertions, 29 deletions
diff --git a/ext/json/lib/json.rb b/ext/json/lib/json.rb
index c28e853e13..dfd9b7dfc2 100644
--- a/ext/json/lib/json.rb
+++ b/ext/json/lib/json.rb
@@ -583,10 +583,5 @@ require 'json/common'
#
module JSON
require 'json/version'
-
- begin
- require 'json/ext'
- rescue LoadError
- require 'json/pure'
- end
+ require 'json/ext'
end
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb
index 546b6ec801..2269896ba8 100644
--- a/ext/json/lib/json/common.rb
+++ b/ext/json/lib/json/common.rb
@@ -32,9 +32,7 @@ module JSON
JSON.generate(object, opts)
end
- # Returns the JSON parser class that is used by JSON. This is either
- # JSON::Ext::Parser or JSON::Pure::Parser:
- # JSON.parser # => JSON::Ext::Parser
+ # Returns the JSON parser class that is used by JSON.
attr_reader :parser
# Set the JSON parser class _parser_ to be used by JSON.
@@ -97,14 +95,10 @@ module JSON
)
end
- # Returns the JSON generator module that is used by JSON. This is
- # either JSON::Ext::Generator or JSON::Pure::Generator:
- # JSON.generator # => JSON::Ext::Generator
+ # Returns the JSON generator module that is used by JSON.
attr_reader :generator
- # Sets or Returns the JSON generator state class that is used by JSON. This is
- # either JSON::Ext::Generator::State or JSON::Pure::Generator::State:
- # JSON.state # => JSON::Ext::Generator::State
+ # Sets or Returns the JSON generator state class that is used by JSON.
attr_accessor :state
end
@@ -207,16 +201,7 @@ module JSON
# JSON.parse('')
#
def parse(source, opts = nil)
- if opts.nil?
- Parser.new(source).parse
- else
- # NB: The ** shouldn't be required, but we have to deal with
- # different versions of the `json` and `json_pure` gems being
- # loaded concurrently.
- # Prior to 2.7.3, `JSON::Ext::Parser` would only take kwargs.
- # Ref: https://github.com/ruby/json/issues/650
- Parser.new(source, **opts).parse
- end
+ Parser.parse(source, opts)
end
# :call-seq:
diff --git a/ext/json/lib/json/ext.rb b/ext/json/lib/json/ext.rb
index 92ef61eaec..2082cae68f 100644
--- a/ext/json/lib/json/ext.rb
+++ b/ext/json/lib/json/ext.rb
@@ -8,14 +8,12 @@ module JSON
module Ext
if RUBY_ENGINE == 'truffleruby'
require 'json/ext/parser'
- require 'json/pure'
- $DEBUG and warn "Using Ext extension for JSON parser and Pure library for JSON generator."
+ require 'json/truffle_ruby/generator'
JSON.parser = Parser
- JSON.generator = JSON::Pure::Generator
+ JSON.generator = ::JSON::TruffleRuby::Generator
else
require 'json/ext/parser'
require 'json/ext/generator'
- $DEBUG and warn "Using Ext extension for JSON."
JSON.parser = Parser
JSON.generator = Generator
end
diff --git a/ext/json/lib/json/ext/generator/state.rb b/ext/json/lib/json/ext/generator/state.rb
new file mode 100644
index 0000000000..6cd9496e67
--- /dev/null
+++ b/ext/json/lib/json/ext/generator/state.rb
@@ -0,0 +1,105 @@
+# frozen_string_literal: true
+
+module JSON
+ module Ext
+ module Generator
+ class State
+ # call-seq: new(opts = {})
+ #
+ # Instantiates a new State object, configured by _opts_.
+ #
+ # _opts_ can have the following keys:
+ #
+ # * *indent*: a string used to indent levels (default: ''),
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
+ # generated, otherwise an exception is thrown, if these values are
+ # encountered. This options defaults to false.
+ # * *ascii_only*: true if only ASCII characters should be generated. This
+ # option defaults to false.
+ # * *buffer_initial_length*: sets the initial length of the generator's
+ # internal buffer.
+ def initialize(opts = nil)
+ if opts && !opts.empty?
+ configure(opts)
+ end
+ end
+
+ # call-seq: configure(opts)
+ #
+ # Configure this State instance with the Hash _opts_, and return
+ # itself.
+ def configure(opts)
+ unless opts.is_a?(Hash)
+ if opts.respond_to?(:to_hash)
+ opts = opts.to_hash
+ elsif opts.respond_to?(:to_h)
+ opts = opts.to_h
+ else
+ raise TypeError, "can't convert #{opts.class} into Hash"
+ end
+ end
+ _configure(opts)
+ end
+
+ alias_method :merge, :configure
+
+ # call-seq: to_h
+ #
+ # Returns the configuration instance variables as a hash, that can be
+ # passed to the configure method.
+ def to_h
+ result = {
+ indent: indent,
+ space: space,
+ space_before: space_before,
+ object_nl: object_nl,
+ array_nl: array_nl,
+ allow_nan: allow_nan?,
+ ascii_only: ascii_only?,
+ max_nesting: max_nesting,
+ script_safe: script_safe?,
+ strict: strict?,
+ depth: depth,
+ buffer_initial_length: buffer_initial_length,
+ }
+
+ instance_variables.each do |iv|
+ iv = iv.to_s[1..-1]
+ result[iv.to_sym] = self[iv]
+ end
+
+ result
+ end
+
+ alias_method :to_hash, :to_h
+
+ # call-seq: [](name)
+ #
+ # Returns the value returned by method +name+.
+ def [](name)
+ if respond_to?(name)
+ __send__(name)
+ else
+ instance_variable_get("@#{name}") if
+ instance_variables.include?("@#{name}".to_sym) # avoid warning
+ end
+ end
+
+ # call-seq: []=(name, value)
+ #
+ # Sets the attribute name to value.
+ def []=(name, value)
+ if respond_to?(name_writer = "#{name}=")
+ __send__ name_writer, value
+ else
+ instance_variable_set "@#{name}", value
+ end
+ end
+ end
+ end
+ end
+end