ed-precompiled_json 2.15.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,99 @@
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}[#module-JSON-label-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
+ if respond_to?(name)
79
+ __send__(name)
80
+ else
81
+ instance_variable_get("@#{name}") if
82
+ instance_variables.include?("@#{name}".to_sym) # avoid warning
83
+ end
84
+ end
85
+
86
+ # call-seq: []=(name, value)
87
+ #
88
+ # Sets the attribute name to value.
89
+ def []=(name, value)
90
+ if respond_to?(name_writer = "#{name}=")
91
+ __send__ name_writer, value
92
+ else
93
+ instance_variable_set "@#{name}", value
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
data/lib/json/ext.rb ADDED
@@ -0,0 +1,57 @@
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
+ ruby_version = /(\d+\.\d+)/.match(::RUBY_VERSION)
32
+
33
+ begin
34
+ require "json/ext/#{ruby_version}/parser"
35
+ rescue LoadError
36
+ require 'json/ext/parser'
37
+ end
38
+
39
+ Ext::Parser::Config = Ext::ParserConfig
40
+ JSON.parser = Ext::Parser
41
+
42
+ if RUBY_ENGINE == 'truffleruby'
43
+ require 'json/truffle_ruby/generator'
44
+ JSON.generator = JSON::TruffleRuby::Generator
45
+ else
46
+ begin
47
+ require "json/ext/#{ruby_version}/generator"
48
+ rescue LoadError
49
+ require 'json/ext/generator'
50
+ end
51
+
52
+ JSON.generator = Generator
53
+ end
54
+ end
55
+
56
+ JSON_LOADED = true unless defined?(JSON::JSON_LOADED)
57
+ 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