json 2.7.2 → 2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSON
4
+ module Ext
5
+ module Generator
6
+ class State
7
+ # call-seq: new(opts = {})
8
+ #
9
+ # Instantiates a new State object, configured by _opts_.
10
+ #
11
+ # _opts_ can have the following keys:
12
+ #
13
+ # * *indent*: a string used to indent levels (default: ''),
14
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
15
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
16
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
17
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
18
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
19
+ # generated, otherwise an exception is thrown, if these values are
20
+ # encountered. This options defaults to false.
21
+ # * *ascii_only*: true if only ASCII characters should be generated. This
22
+ # option defaults to false.
23
+ # * *buffer_initial_length*: sets the initial length of the generator's
24
+ # internal buffer.
25
+ def initialize(opts = nil)
26
+ if opts && !opts.empty?
27
+ configure(opts)
28
+ end
29
+ end
30
+
31
+ # call-seq: configure(opts)
32
+ #
33
+ # Configure this State instance with the Hash _opts_, and return
34
+ # itself.
35
+ def configure(opts)
36
+ unless opts.is_a?(Hash)
37
+ if opts.respond_to?(:to_hash)
38
+ opts = opts.to_hash
39
+ elsif opts.respond_to?(:to_h)
40
+ opts = opts.to_h
41
+ else
42
+ raise TypeError, "can't convert #{opts.class} into Hash"
43
+ end
44
+ end
45
+ _configure(opts)
46
+ end
47
+
48
+ alias_method :merge, :configure
49
+
50
+ # call-seq: to_h
51
+ #
52
+ # Returns the configuration instance variables as a hash, that can be
53
+ # passed to the configure method.
54
+ def to_h
55
+ result = {
56
+ indent: indent,
57
+ space: space,
58
+ space_before: space_before,
59
+ object_nl: object_nl,
60
+ array_nl: array_nl,
61
+ as_json: as_json,
62
+ allow_nan: allow_nan?,
63
+ ascii_only: ascii_only?,
64
+ max_nesting: max_nesting,
65
+ script_safe: script_safe?,
66
+ strict: strict?,
67
+ depth: depth,
68
+ buffer_initial_length: buffer_initial_length,
69
+ }
70
+
71
+ instance_variables.each do |iv|
72
+ iv = iv.to_s[1..-1]
73
+ result[iv.to_sym] = self[iv]
74
+ end
75
+
76
+ result
77
+ end
78
+
79
+ alias_method :to_hash, :to_h
80
+
81
+ # call-seq: [](name)
82
+ #
83
+ # Returns the value returned by method +name+.
84
+ def [](name)
85
+ if respond_to?(name)
86
+ __send__(name)
87
+ else
88
+ instance_variable_get("@#{name}") if
89
+ instance_variables.include?("@#{name}".to_sym) # avoid warning
90
+ end
91
+ end
92
+
93
+ # call-seq: []=(name, value)
94
+ #
95
+ # Sets the attribute name to value.
96
+ def []=(name, value)
97
+ if respond_to?(name_writer = "#{name}=")
98
+ __send__ name_writer, value
99
+ else
100
+ instance_variable_set "@#{name}", value
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
data/lib/json/ext.rb CHANGED
@@ -1,14 +1,44 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json/common'
2
4
 
3
5
  module JSON
4
6
  # This module holds all the modules/classes that implement JSON's
5
7
  # functionality as C extensions.
6
8
  module Ext
9
+ class Parser
10
+ class << self
11
+ def parse(...)
12
+ new(...).parse
13
+ end
14
+ alias_method :parse, :parse # Allow redefinition by extensions
15
+ end
16
+
17
+ def initialize(source, opts = nil)
18
+ @source = source
19
+ @config = Config.new(opts)
20
+ end
21
+
22
+ def source
23
+ @source.dup
24
+ end
25
+
26
+ def parse
27
+ @config.parse(@source)
28
+ end
29
+ end
30
+
7
31
  require 'json/ext/parser'
8
- require 'json/ext/generator'
9
- $DEBUG and warn "Using Ext extension for JSON."
10
- JSON.parser = Parser
11
- JSON.generator = Generator
32
+ Ext::Parser::Config = Ext::ParserConfig
33
+ JSON.parser = Ext::Parser
34
+
35
+ if RUBY_ENGINE == 'truffleruby'
36
+ require 'json/truffle_ruby/generator'
37
+ JSON.generator = ::JSON::TruffleRuby::Generator
38
+ else
39
+ require 'json/ext/generator'
40
+ JSON.generator = Generator
41
+ end
12
42
  end
13
43
 
14
44
  JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
@@ -1,4 +1,4 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  begin
3
3
  require 'ostruct'
4
4
  rescue LoadError