diff options
author | Jean Boussier <[email protected]> | 2025-01-13 14:04:15 +0100 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2025-01-14 09:08:02 +0100 |
commit | 599fbeaffa8e029e11223c24af47a55500f23fc3 () | |
tree | f2460eebc4afd103f29dc4ea76f580397a7a8928 /ext/json/lib | |
parent | c8d11edcf556088d10f0a245139741dcab1c7d56 (diff) |
[ruby/json] Refactor JSON::Ext::Parser to split configuration and parsing state
Ref: https://.com/ruby/json/pull/718 The existing `Parser` interface is pretty bad, as it forces to instantiate a new instance for each document. Instead it's preferable to only take the config and do all the initialization needed, and then keep the parsing state on the stack on in ephemeral memory. This refactor makes the `JSON::Coder` pull request much easier to implement in a performant way. https://.com/ruby/json/commit/c8d5236a92 Co-Authored-By: Étienne Barrié <[email protected]>
-rw-r--r-- | ext/json/lib/json/common.rb | 11 | ||||
-rw-r--r-- | ext/json/lib/json/ext.rb | 29 |
2 files changed, 31 insertions, 9 deletions
@@ -232,12 +232,13 @@ module JSON # - Option +max_nesting+, if not provided, defaults to +false+, # which disables checking for nesting depth. # - Option +allow_nan+, if not provided, defaults to +true+. - def parse!(source, opts = {}) - opts = { :max_nesting => false, :allow_nan => true - }.merge(opts) - Parser.new(source, **(opts||{})).parse end # :call-seq: @@ -258,7 +259,7 @@ module JSON # JSON.parse!(File.read(path, opts)) # # See method #parse! - def load_file!(filespec, opts = {}) parse!(File.read(filespec, encoding: Encoding::UTF_8), opts) end @@ -6,15 +6,36 @@ module JSON # This module holds all the modules/classes that implement JSON's # functionality as C extensions. module Ext if RUBY_ENGINE == 'truffleruby' - require 'json/ext/parser' require 'json/truffle_ruby/generator' - JSON.parser = Parser JSON.generator = ::JSON::TruffleRuby::Generator else - require 'json/ext/parser' require 'json/ext/generator' - JSON.parser = Parser JSON.generator = Generator end end |