json 2.6.1 → 2.21.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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/BSDL +22 -0
  3. data/CHANGES.md +370 -17
  4. data/LEGAL +20 -0
  5. data/README.md +96 -213
  6. data/ext/json/ext/fbuffer/fbuffer.h +190 -118
  7. data/ext/json/ext/generator/extconf.rb +17 -2
  8. data/ext/json/ext/generator/generator.c +1532 -1074
  9. data/ext/json/ext/json.h +183 -0
  10. data/ext/json/ext/parser/extconf.rb +30 -25
  11. data/ext/json/ext/parser/parser.c +2837 -3258
  12. data/ext/json/ext/simd/conf.rb +24 -0
  13. data/ext/json/ext/simd/simd.h +208 -0
  14. data/ext/json/ext/vendor/fast_float_parser.h +814 -0
  15. data/ext/json/ext/vendor/fpconv.c +480 -0
  16. data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
  17. data/json.gemspec +48 -53
  18. data/lib/json/add/bigdecimal.rb +39 -10
  19. data/lib/json/add/complex.rb +29 -6
  20. data/lib/json/add/core.rb +2 -1
  21. data/lib/json/add/date.rb +27 -7
  22. data/lib/json/add/date_time.rb +26 -9
  23. data/lib/json/add/exception.rb +25 -7
  24. data/lib/json/add/ostruct.rb +32 -9
  25. data/lib/json/add/range.rb +33 -8
  26. data/lib/json/add/rational.rb +28 -6
  27. data/lib/json/add/regexp.rb +26 -8
  28. data/lib/json/add/set.rb +25 -6
  29. data/lib/json/add/string.rb +35 -0
  30. data/lib/json/add/struct.rb +29 -7
  31. data/lib/json/add/symbol.rb +34 -7
  32. data/lib/json/add/time.rb +29 -15
  33. data/lib/json/common.rb +746 -267
  34. data/lib/json/ext/generator/state.rb +104 -0
  35. data/lib/json/ext.rb +61 -5
  36. data/lib/json/generic_object.rb +7 -11
  37. data/lib/json/truffle_ruby/generator.rb +790 -0
  38. data/lib/json/version.rb +3 -7
  39. data/lib/json.rb +134 -24
  40. metadata +22 -26
  41. data/VERSION +0 -1
  42. data/ext/json/ext/generator/depend +0 -1
  43. data/ext/json/ext/generator/generator.h +0 -174
  44. data/ext/json/ext/parser/depend +0 -1
  45. data/ext/json/ext/parser/parser.h +0 -96
  46. data/ext/json/ext/parser/parser.rl +0 -977
  47. data/ext/json/extconf.rb +0 -3
  48. data/lib/json/pure/generator.rb +0 -479
  49. data/lib/json/pure/parser.rb +0 -337
  50. data/lib/json/pure.rb +0 -15
  51. /data/{LICENSE → COPYING} +0 -0
@@ -0,0 +1,104 @@
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
+ # Argument +opts+, if given, contains a \Hash of options for the generation.
12
+ # See {Generating Options}[rdoc-ref:JSON@Generating+Options].
13
+ def initialize(opts = nil)
14
+ if opts && !opts.empty?
15
+ configure(opts)
16
+ end
17
+ end
18
+
19
+ # call-seq: configure(opts)
20
+ #
21
+ # Configure this State instance with the Hash _opts_, and return
22
+ # itself.
23
+ def configure(opts)
24
+ unless opts.is_a?(Hash)
25
+ if opts.respond_to?(:to_hash)
26
+ opts = opts.to_hash
27
+ elsif opts.respond_to?(:to_h)
28
+ opts = opts.to_h
29
+ else
30
+ raise TypeError, "can't convert #{opts.class} into Hash"
31
+ end
32
+ end
33
+ _configure(opts)
34
+ end
35
+
36
+ alias_method :merge, :configure
37
+
38
+ # call-seq: to_h
39
+ #
40
+ # Returns the configuration instance variables as a hash, that can be
41
+ # passed to the configure method.
42
+ def to_h
43
+ result = {
44
+ indent: indent,
45
+ space: space,
46
+ space_before: space_before,
47
+ object_nl: object_nl,
48
+ array_nl: array_nl,
49
+ as_json: as_json,
50
+ allow_nan: allow_nan?,
51
+ ascii_only: ascii_only?,
52
+ max_nesting: max_nesting,
53
+ script_safe: script_safe?,
54
+ strict: strict?,
55
+ depth: depth,
56
+ buffer_initial_length: buffer_initial_length,
57
+ sort_keys: sort_keys
58
+ }
59
+
60
+ allow_duplicate_key = allow_duplicate_key?
61
+ unless allow_duplicate_key.nil?
62
+ result[:allow_duplicate_key] = allow_duplicate_key
63
+ end
64
+
65
+ instance_variables.each do |iv|
66
+ iv = iv.to_s[1..-1]
67
+ result[iv.to_sym] = self[iv]
68
+ end
69
+
70
+ result
71
+ end
72
+
73
+ alias_method :to_hash, :to_h
74
+
75
+ # call-seq: [](name)
76
+ #
77
+ # Returns the value returned by method +name+.
78
+ def [](name)
79
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
80
+
81
+ if respond_to?(name)
82
+ __send__(name)
83
+ else
84
+ instance_variable_get("@#{name}") if
85
+ instance_variables.include?("@#{name}".to_sym) # avoid warning
86
+ end
87
+ end
88
+
89
+ # call-seq: []=(name, value)
90
+ #
91
+ # Sets the attribute name to value.
92
+ def []=(name, value)
93
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
94
+
95
+ if respond_to?(name_writer = "#{name}=")
96
+ __send__ name_writer, value
97
+ else
98
+ instance_variable_set "@#{name}", value
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
data/lib/json/ext.rb CHANGED
@@ -1,15 +1,71 @@
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
42
+ end
43
+
44
+ if defined?(ResumableParser) # Not yet available on JRuby
45
+ class ResumableParser
46
+ # Returns whether the parser is entirely done: no unconsumed bytes in
47
+ # the buffer, no document under construction and no parsed value
48
+ # awaiting retrieval.
49
+ #
50
+ # The main use case is detecting a truncated stream once the input is
51
+ # exhausted:
52
+ #
53
+ # loop do
54
+ # begin
55
+ # parser << socket.readpartial(4096)
56
+ # rescue EOFError
57
+ # break
58
+ # end
59
+ # while parser.parse
60
+ # process(parser.value)
61
+ # end
62
+ # end
63
+ # warn "stream was truncated" unless parser.empty?
64
+ def empty?
65
+ eos? && !partial_value? && !value?
66
+ end
67
+ end
12
68
  end
13
69
 
14
- JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
70
+ JSON_LOADED = true unless defined?(JSON::JSON_LOADED)
15
71
  end
@@ -1,5 +1,9 @@
1
- #frozen_string_literal: false
2
- require 'ostruct'
1
+ # frozen_string_literal: true
2
+ begin
3
+ require 'ostruct'
4
+ rescue LoadError
5
+ warn "JSON::GenericObject requires 'ostruct'. Please install it with `gem install ostruct`."
6
+ end
3
7
 
4
8
  module JSON
5
9
  class GenericObject < OpenStruct
@@ -48,14 +52,6 @@ module JSON
48
52
  table
49
53
  end
50
54
 
51
- def [](name)
52
- __send__(name)
53
- end unless method_defined?(:[])
54
-
55
- def []=(name, value)
56
- __send__("#{name}=", value)
57
- end unless method_defined?(:[]=)
58
-
59
55
  def |(other)
60
56
  self.class[other.to_hash.merge(to_hash)]
61
57
  end
@@ -67,5 +63,5 @@ module JSON
67
63
  def to_json(*a)
68
64
  as_json.to_json(*a)
69
65
  end
70
- end
66
+ end if defined?(::OpenStruct)
71
67
  end