json-extended 2.20.0

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,103 @@
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
+ }
58
+
59
+ allow_duplicate_key = allow_duplicate_key?
60
+ unless allow_duplicate_key.nil?
61
+ result[:allow_duplicate_key] = allow_duplicate_key
62
+ end
63
+
64
+ instance_variables.each do |iv|
65
+ iv = iv.to_s[1..-1]
66
+ result[iv.to_sym] = self[iv]
67
+ end
68
+
69
+ result
70
+ end
71
+
72
+ alias_method :to_hash, :to_h
73
+
74
+ # call-seq: [](name)
75
+ #
76
+ # Returns the value returned by method +name+.
77
+ def [](name)
78
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
79
+
80
+ if respond_to?(name)
81
+ __send__(name)
82
+ else
83
+ instance_variable_get("@#{name}") if
84
+ instance_variables.include?("@#{name}".to_sym) # avoid warning
85
+ end
86
+ end
87
+
88
+ # call-seq: []=(name, value)
89
+ #
90
+ # Sets the attribute name to value.
91
+ def []=(name, value)
92
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
93
+
94
+ if respond_to?(name_writer = "#{name}=")
95
+ __send__ name_writer, value
96
+ else
97
+ instance_variable_set "@#{name}", value
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
data/lib/json/ext.rb ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json/common'
4
+
5
+ module JSON
6
+ # This module holds all the modules/classes that implement JSON's
7
+ # functionality as C extensions.
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
+
31
+ require 'json/ext/parser'
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
+ JSON_LOADED = true unless defined?(JSON::JSON_LOADED)
45
+ end
@@ -0,0 +1,67 @@
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
7
+
8
+ module JSON
9
+ class GenericObject < OpenStruct
10
+ class << self
11
+ alias [] new
12
+
13
+ def json_creatable?
14
+ @json_creatable
15
+ end
16
+
17
+ attr_writer :json_creatable
18
+
19
+ def json_create(data)
20
+ data = data.dup
21
+ data.delete JSON.create_id
22
+ self[data]
23
+ end
24
+
25
+ def from_hash(object)
26
+ case
27
+ when object.respond_to?(:to_hash)
28
+ result = new
29
+ object.to_hash.each do |key, value|
30
+ result[key] = from_hash(value)
31
+ end
32
+ result
33
+ when object.respond_to?(:to_ary)
34
+ object.to_ary.map { |a| from_hash(a) }
35
+ else
36
+ object
37
+ end
38
+ end
39
+
40
+ def load(source, proc = nil, opts = {})
41
+ result = ::JSON.load(source, proc, opts.merge(:object_class => self))
42
+ result.nil? ? new : result
43
+ end
44
+
45
+ def dump(obj, *args)
46
+ ::JSON.dump(obj, *args)
47
+ end
48
+ end
49
+ self.json_creatable = false
50
+
51
+ def to_hash
52
+ table
53
+ end
54
+
55
+ def |(other)
56
+ self.class[other.to_hash.merge(to_hash)]
57
+ end
58
+
59
+ def as_json(*)
60
+ { JSON.create_id => self.class.name }.merge to_hash
61
+ end
62
+
63
+ def to_json(*a)
64
+ as_json.to_json(*a)
65
+ end
66
+ end if defined?(::OpenStruct)
67
+ end