json_pure 2.7.6 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc10fe68c32c66130244c95986e80f41d17b72ed2d8311fde09c1d82dd8fa154
4
- data.tar.gz: 3a9cd865c6b542d9ae199bac464d96d4f0116aee65eb35f7b4acfa1ab97a4f83
3
+ metadata.gz: 4c79a21e52d8328f2d07a73c932b6680f2b6db7a3725ea5c84d5f73e43a90e37
4
+ data.tar.gz: b182b612e32fb6151c245999f985bbd5b3e1fb888a168ba2e5412604f3d88a9e
5
5
  SHA512:
6
- metadata.gz: 87ba58d3641b73445fc3b07ba70197b05efd2e319410530c946c8b3733c7ff2b5b3c8db61e482d3c3c52930d544d7637ee442a53670796b7cb7b5dbb9ecea0c0
7
- data.tar.gz: 2f35def657dcae3cbbe4593f25513c4340bfbbc1b2ec7b7accec7914007893abf333e2aed047a92cdd24f13d85d03622518f07bad8cbf61caef17c0211235239
6
+ metadata.gz: 40bdbd82974a44e90b5ac767fa2938241c18a4cd1c45b3544da9287de4326ac69d6a9affef5401bdbf856ebbff65f475ded1a10f980143b1a7d736acb446254b
7
+ data.tar.gz: 165032b45f38308eb32cf93ca6a73357e328dc789e7ed585701c95d2d0d0e52d0b9aaddd2a44fc62b1eaebaf804fe56d07a06aa6509eed4cb72872661668fd8f
data/CHANGES.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # Changes
2
2
 
3
- ### 2024-11-04 (2.7.6)
3
+ ### UNRELEASED
4
4
 
5
- * Fix a regression in JSON.generate when dealing with Hash keys that are string subclasses, call `to_json` on them.
5
+ * Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explictly enabled.
6
+ * Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
7
+ * Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
8
+ * Bump required_ruby_version to 2.7.
9
+ * More performance improvments to `JSON.dump` and `JSON.generate`.
6
10
 
7
11
  ### 2024-10-25 (2.7.5)
8
12
 
data/README.md CHANGED
@@ -5,16 +5,10 @@
5
5
  ## Description
6
6
 
7
7
  This is an implementation of the JSON specification according to RFC 7159
8
- http://www.ietf.org/rfc/rfc7159.txt . There is two variants available:
8
+ http://www.ietf.org/rfc/rfc7159.txt .
9
9
 
10
- * A pure ruby variant, that relies on the `strscan` extensions, which is
11
- part of the ruby standard library.
12
- * The quite a bit faster native extension variant, which is in parts
13
- implemented in C or Java and comes with a parser generated by the [Ragel]
14
- state machine compiler.
15
-
16
- Both variants of the JSON generator generate UTF-8 character sequences by
17
- default. If an :ascii\_only option with a true value is given, they escape all
10
+ The JSON generator generate UTF-8 character sequences by default.
11
+ If an :ascii\_only option with a true value is given, they escape all
18
12
  non-ASCII and control characters with \uXXXX escape sequences, and support
19
13
  UTF-16 surrogate pairs in order to be able to generate the whole range of
20
14
  unicode code points.
@@ -82,50 +76,11 @@ You can also use the `pretty_generate` method (which formats the output more
82
76
  verbosely and nicely) or `fast_generate` (which doesn't do any of the security
83
77
  checks generate performs, e. g. nesting deepness checks).
84
78
 
85
- There are also the JSON and JSON[] methods which use parse on a String or
86
- generate a JSON document from an array or hash:
87
-
88
- ```ruby
89
- document = JSON 'test' => 23 # => "{\"test\":23}"
90
- document = JSON['test' => 23] # => "{\"test\":23}"
91
- ```
92
-
93
- and
94
-
95
- ```ruby
96
- data = JSON '{"test":23}' # => {"test"=>23}
97
- data = JSON['{"test":23}'] # => {"test"=>23}
98
- ```
99
-
100
- You can choose to load a set of common additions to ruby core's objects if
101
- you
102
-
103
- ```ruby
104
- require 'json/add/core'
105
- ```
106
-
107
- After requiring this you can, e. g., serialise/deserialise Ruby ranges:
108
-
109
- ```ruby
110
- JSON JSON(1..10) # => 1..10
111
- ```
112
-
113
- To find out how to add JSON support to other or your own classes, read the
114
- section "More Examples" below.
115
-
116
- ## Serializing exceptions
117
-
118
- The JSON module doesn't extend `Exception` by default. If you convert an `Exception`
119
- object to JSON, it will by default only include the exception message.
120
-
121
- To include the full details, you must either load the `json/add/core` mentioned
122
- above, or specifically load the exception addition:
123
-
124
- ```ruby
125
- require 'json/add/exception'
126
- ```
79
+ ## Handling arbitrary types
127
80
 
128
- ## More Examples
81
+ > [!CAUTION]
82
+ > You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,
83
+ > as it can lead to remove code execution vulnerabilities.
129
84
 
130
85
  To create a JSON document from a ruby data structure, you can call
131
86
  `JSON.generate` like that:
@@ -191,7 +146,7 @@ JSON.parse json
191
146
  # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
192
147
  json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
193
148
  # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
194
- JSON.parse json, :create_additions => true
149
+ JSON.unsafe_load json
195
150
  # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
196
151
  ```
197
152
 
data/json_pure.gemspec CHANGED
@@ -1,50 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  version = File.foreach(File.join(__dir__, "lib/json/version.rb")) do |line|
2
4
  /^\s*VERSION\s*=\s*'(.*)'/ =~ line and break $1
3
5
  end rescue nil
4
6
 
5
7
  Gem::Specification.new do |s|
6
- s.name = "json_pure".freeze
8
+ s.name = "json_pure"
7
9
  s.version = version
8
10
 
9
- s.summary = "JSON Implementation for Ruby".freeze
10
- s.description = "This is a JSON implementation in pure Ruby.".freeze
11
- s.licenses = ["Ruby".freeze]
12
- s.authors = ["Florian Frank".freeze]
13
- s.email = "flori@ping.de".freeze
11
+ s.summary = "JSON Implementation for Ruby"
12
+ s.description = "This is a JSON implementation in pure Ruby."
13
+ s.licenses = ["Ruby"]
14
+ s.authors = ["Florian Frank"]
15
+ s.email = "flori@ping.de"
14
16
 
15
- s.extra_rdoc_files = ["README.md".freeze]
16
- s.rdoc_options = ["--title".freeze, "JSON implementation for ruby".freeze, "--main".freeze, "README.md".freeze]
17
+ s.extra_rdoc_files = ["README.md"]
18
+ s.rdoc_options = ["--title", "JSON implementation for ruby", "--main", "README.md"]
17
19
  s.files = [
18
- "CHANGES.md".freeze,
19
- "COPYING".freeze,
20
- "BSDL".freeze,
21
- "LEGAL".freeze,
22
- "README.md".freeze,
23
- "json_pure.gemspec".freeze,
24
- "lib/json.rb".freeze,
25
- "lib/json/add/bigdecimal.rb".freeze,
26
- "lib/json/add/complex.rb".freeze,
27
- "lib/json/add/core.rb".freeze,
28
- "lib/json/add/date.rb".freeze,
29
- "lib/json/add/date_time.rb".freeze,
30
- "lib/json/add/exception.rb".freeze,
31
- "lib/json/add/ostruct.rb".freeze,
32
- "lib/json/add/range.rb".freeze,
33
- "lib/json/add/rational.rb".freeze,
34
- "lib/json/add/regexp.rb".freeze,
35
- "lib/json/add/set.rb".freeze,
36
- "lib/json/add/struct.rb".freeze,
37
- "lib/json/add/symbol.rb".freeze,
38
- "lib/json/add/time.rb".freeze,
39
- "lib/json/common.rb".freeze,
40
- "lib/json/ext.rb".freeze,
41
- "lib/json/generic_object.rb".freeze,
42
- "lib/json/pure.rb".freeze,
43
- "lib/json/pure/generator.rb".freeze,
44
- "lib/json/pure/parser.rb".freeze,
45
- "lib/json/version.rb".freeze,
20
+ "CHANGES.md",
21
+ "COPYING",
22
+ "BSDL",
23
+ "LEGAL",
24
+ "README.md",
25
+ "json_pure.gemspec",
26
+ "lib/json/pure.rb",
46
27
  ]
47
- s.homepage = "https://ruby.github.io/json".freeze
28
+ s.homepage = "https://ruby.github.io/json"
48
29
  s.metadata = {
49
30
  'bug_tracker_uri' => 'https://github.com/ruby/json/issues',
50
31
  'changelog_uri' => 'https://github.com/ruby/json/blob/master/CHANGES.md',
@@ -54,5 +35,7 @@ Gem::Specification.new do |s|
54
35
  'wiki_uri' => 'https://github.com/ruby/json/wiki'
55
36
  }
56
37
 
57
- s.required_ruby_version = Gem::Requirement.new(">= 2.3".freeze)
38
+ s.add_dependency "json"
39
+
40
+ s.required_ruby_version = Gem::Requirement.new(">= 2.7")
58
41
  end
data/lib/json/pure.rb CHANGED
@@ -1,16 +1,4 @@
1
1
  # frozen_string_literal: true
2
- require 'json/common'
3
2
 
4
- module JSON
5
- # This module holds all the modules/classes that implement JSON's
6
- # functionality in pure ruby.
7
- module Pure
8
- require 'json/pure/parser'
9
- require 'json/pure/generator'
10
- $DEBUG and warn "Using Pure library for JSON."
11
- JSON.parser = Parser
12
- JSON.generator = Generator
13
- end
14
-
15
- JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
16
- end
3
+ warn "`json_pure` is deprecated and has no effect, just use `json`"
4
+ require "json"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_pure
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.6
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-04 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: This is a JSON implementation in pure Ruby.
14
28
  email: flori@ping.de
15
29
  executables: []
@@ -23,28 +37,7 @@ files:
23
37
  - LEGAL
24
38
  - README.md
25
39
  - json_pure.gemspec
26
- - lib/json.rb
27
- - lib/json/add/bigdecimal.rb
28
- - lib/json/add/complex.rb
29
- - lib/json/add/core.rb
30
- - lib/json/add/date.rb
31
- - lib/json/add/date_time.rb
32
- - lib/json/add/exception.rb
33
- - lib/json/add/ostruct.rb
34
- - lib/json/add/range.rb
35
- - lib/json/add/rational.rb
36
- - lib/json/add/regexp.rb
37
- - lib/json/add/set.rb
38
- - lib/json/add/struct.rb
39
- - lib/json/add/symbol.rb
40
- - lib/json/add/time.rb
41
- - lib/json/common.rb
42
- - lib/json/ext.rb
43
- - lib/json/generic_object.rb
44
40
  - lib/json/pure.rb
45
- - lib/json/pure/generator.rb
46
- - lib/json/pure/parser.rb
47
- - lib/json/version.rb
48
41
  homepage: https://ruby.github.io/json
49
42
  licenses:
50
43
  - Ruby
@@ -67,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
60
  requirements:
68
61
  - - ">="
69
62
  - !ruby/object:Gem::Version
70
- version: '2.3'
63
+ version: '2.7'
71
64
  required_rubygems_version: !ruby/object:Gem::Requirement
72
65
  requirements:
73
66
  - - ">="
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
- unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
- require 'json'
4
- end
5
- begin
6
- require 'bigdecimal'
7
- rescue LoadError
8
- end
9
-
10
- class BigDecimal
11
-
12
- # See #as_json.
13
- def self.json_create(object)
14
- BigDecimal._load object['b']
15
- end
16
-
17
- # Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used
18
- # to serialize and deserialize a \BigDecimal object;
19
- # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
20
- #
21
- # \Method <tt>BigDecimal#as_json</tt> serializes +self+,
22
- # returning a 2-element hash representing +self+:
23
- #
24
- # require 'json/add/bigdecimal'
25
- # x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
26
- # y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
27
- # z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
28
- #
29
- # \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object:
30
- #
31
- # BigDecimal.json_create(x) # => 0.2e1
32
- # BigDecimal.json_create(y) # => 0.2e1
33
- # BigDecimal.json_create(z) # => 0.2e1
34
- #
35
- def as_json(*)
36
- {
37
- JSON.create_id => self.class.name,
38
- 'b' => _dump,
39
- }
40
- end
41
-
42
- # Returns a JSON string representing +self+:
43
- #
44
- # require 'json/add/bigdecimal'
45
- # puts BigDecimal(2).to_json
46
- # puts BigDecimal(2.0, 4).to_json
47
- # puts BigDecimal(Complex(2, 0)).to_json
48
- #
49
- # Output:
50
- #
51
- # {"json_class":"BigDecimal","b":"27:0.2e1"}
52
- # {"json_class":"BigDecimal","b":"36:0.2e1"}
53
- # {"json_class":"BigDecimal","b":"27:0.2e1"}
54
- #
55
- def to_json(*args)
56
- as_json.to_json(*args)
57
- end
58
- end if defined?(::BigDecimal)
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
- unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
- require 'json'
4
- end
5
-
6
- class Complex
7
-
8
- # See #as_json.
9
- def self.json_create(object)
10
- Complex(object['r'], object['i'])
11
- end
12
-
13
- # Methods <tt>Complex#as_json</tt> and +Complex.json_create+ may be used
14
- # to serialize and deserialize a \Complex object;
15
- # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
16
- #
17
- # \Method <tt>Complex#as_json</tt> serializes +self+,
18
- # returning a 2-element hash representing +self+:
19
- #
20
- # require 'json/add/complex'
21
- # x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0}
22
- # y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
23
- #
24
- # \Method +JSON.create+ deserializes such a hash, returning a \Complex object:
25
- #
26
- # Complex.json_create(x) # => (2+0i)
27
- # Complex.json_create(y) # => (2.0+4i)
28
- #
29
- def as_json(*)
30
- {
31
- JSON.create_id => self.class.name,
32
- 'r' => real,
33
- 'i' => imag,
34
- }
35
- end
36
-
37
- # Returns a JSON string representing +self+:
38
- #
39
- # require 'json/add/complex'
40
- # puts Complex(2).to_json
41
- # puts Complex(2.0, 4).to_json
42
- #
43
- # Output:
44
- #
45
- # {"json_class":"Complex","r":2,"i":0}
46
- # {"json_class":"Complex","r":2.0,"i":4}
47
- #
48
- def to_json(*args)
49
- as_json.to_json(*args)
50
- end
51
- end
data/lib/json/add/core.rb DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
- # This file requires the implementations of ruby core's custom objects for
3
- # serialisation/deserialisation.
4
-
5
- require 'json/add/date'
6
- require 'json/add/date_time'
7
- require 'json/add/exception'
8
- require 'json/add/range'
9
- require 'json/add/regexp'
10
- require 'json/add/struct'
11
- require 'json/add/symbol'
12
- require 'json/add/time'
data/lib/json/add/date.rb DELETED
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
- unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
- require 'json'
4
- end
5
- require 'date'
6
-
7
- class Date
8
-
9
- # See #as_json.
10
- def self.json_create(object)
11
- civil(*object.values_at('y', 'm', 'd', 'sg'))
12
- end
13
-
14
- alias start sg unless method_defined?(:start)
15
-
16
- # Methods <tt>Date#as_json</tt> and +Date.json_create+ may be used
17
- # to serialize and deserialize a \Date object;
18
- # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
19
- #
20
- # \Method <tt>Date#as_json</tt> serializes +self+,
21
- # returning a 2-element hash representing +self+:
22
- #
23
- # require 'json/add/date'
24
- # x = Date.today.as_json
25
- # # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
26
- #
27
- # \Method +JSON.create+ deserializes such a hash, returning a \Date object:
28
- #
29
- # Date.json_create(x)
30
- # # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
31
- #
32
- def as_json(*)
33
- {
34
- JSON.create_id => self.class.name,
35
- 'y' => year,
36
- 'm' => month,
37
- 'd' => day,
38
- 'sg' => start,
39
- }
40
- end
41
-
42
- # Returns a JSON string representing +self+:
43
- #
44
- # require 'json/add/date'
45
- # puts Date.today.to_json
46
- #
47
- # Output:
48
- #
49
- # {"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
50
- #
51
- def to_json(*args)
52
- as_json.to_json(*args)
53
- end
54
- end
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
- unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
- require 'json'
4
- end
5
- require 'date'
6
-
7
- class DateTime
8
-
9
- # See #as_json.
10
- def self.json_create(object)
11
- args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
12
- of_a, of_b = object['of'].split('/')
13
- if of_b and of_b != '0'
14
- args << Rational(of_a.to_i, of_b.to_i)
15
- else
16
- args << of_a
17
- end
18
- args << object['sg']
19
- civil(*args)
20
- end
21
-
22
- alias start sg unless method_defined?(:start)
23
-
24
- # Methods <tt>DateTime#as_json</tt> and +DateTime.json_create+ may be used
25
- # to serialize and deserialize a \DateTime object;
26
- # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
27
- #
28
- # \Method <tt>DateTime#as_json</tt> serializes +self+,
29
- # returning a 2-element hash representing +self+:
30
- #
31
- # require 'json/add/datetime'
32
- # x = DateTime.now.as_json
33
- # # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
34
- #
35
- # \Method +JSON.create+ deserializes such a hash, returning a \DateTime object:
36
- #
37
- # DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
38
- #
39
- def as_json(*)
40
- {
41
- JSON.create_id => self.class.name,
42
- 'y' => year,
43
- 'm' => month,
44
- 'd' => day,
45
- 'H' => hour,
46
- 'M' => min,
47
- 'S' => sec,
48
- 'of' => offset.to_s,
49
- 'sg' => start,
50
- }
51
- end
52
-
53
- # Returns a JSON string representing +self+:
54
- #
55
- # require 'json/add/datetime'
56
- # puts DateTime.now.to_json
57
- #
58
- # Output:
59
- #
60
- # {"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
61
- #
62
- def to_json(*args)
63
- as_json.to_json(*args)
64
- end
65
- end
66
-
67
-
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
- unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
- require 'json'
4
- end
5
-
6
- class Exception
7
-
8
- # See #as_json.
9
- def self.json_create(object)
10
- result = new(object['m'])
11
- result.set_backtrace object['b']
12
- result
13
- end
14
-
15
- # Methods <tt>Exception#as_json</tt> and +Exception.json_create+ may be used
16
- # to serialize and deserialize a \Exception object;
17
- # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
18
- #
19
- # \Method <tt>Exception#as_json</tt> serializes +self+,
20
- # returning a 2-element hash representing +self+:
21
- #
22
- # require 'json/add/exception'
23
- # x = Exception.new('Foo').as_json # => {"json_class"=>"Exception", "m"=>"Foo", "b"=>nil}
24
- #
25
- # \Method +JSON.create+ deserializes such a hash, returning a \Exception object:
26
- #
27
- # Exception.json_create(x) # => #<Exception: Foo>
28
- #
29
- def as_json(*)
30
- {
31
- JSON.create_id => self.class.name,
32
- 'm' => message,
33
- 'b' => backtrace,
34
- }
35
- end
36
-
37
- # Returns a JSON string representing +self+:
38
- #
39
- # require 'json/add/exception'
40
- # puts Exception.new('Foo').to_json
41
- #
42
- # Output:
43
- #
44
- # {"json_class":"Exception","m":"Foo","b":null}
45
- #
46
- def to_json(*args)
47
- as_json.to_json(*args)
48
- end
49
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
- unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
- require 'json'
4
- end
5
- begin
6
- require 'ostruct'
7
- rescue LoadError
8
- end
9
-
10
- class OpenStruct
11
-
12
- # See #as_json.
13
- def self.json_create(object)
14
- new(object['t'] || object[:t])
15
- end
16
-
17
- # Methods <tt>OpenStruct#as_json</tt> and +OpenStruct.json_create+ may be used
18
- # to serialize and deserialize a \OpenStruct object;
19
- # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
20
- #
21
- # \Method <tt>OpenStruct#as_json</tt> serializes +self+,
22
- # returning a 2-element hash representing +self+:
23
- #
24
- # require 'json/add/ostruct'
25
- # x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json
26
- # # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}
27
- #
28
- # \Method +JSON.create+ deserializes such a hash, returning a \OpenStruct object:
29
- #
30
- # OpenStruct.json_create(x)
31
- # # => #<OpenStruct name='Rowdy', age=nil>
32
- #
33
- def as_json(*)
34
- klass = self.class.name
35
- klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
36
- {
37
- JSON.create_id => klass,
38
- 't' => table,
39
- }
40
- end
41
-
42
- # Returns a JSON string representing +self+:
43
- #
44
- # require 'json/add/ostruct'
45
- # puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
46
- #
47
- # Output:
48
- #
49
- # {"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
50
- #
51
- def to_json(*args)
52
- as_json.to_json(*args)
53
- end
54
- end if defined?(::OpenStruct)