json-generic_object 0.0.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.
- checksums.yaml +7 -0
- data/.envrc +1 -0
- data/.gitignore +9 -0
- data/Gemfile +3 -0
- data/README.md +123 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/json-generic_object.gemspec +32 -0
- data/lib/json/generic_object.rb +185 -0
- data/lib/json-generic_object/version.rb +13 -0
- data/test/json_generic_object_test.rb +52 -0
- data/test/test_helper.rb +11 -0
- metadata +146 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 62d5a7a766c5cc7f93b60d8e4e282fa112e7716670a42ef01161fa52627cb44b
|
|
4
|
+
data.tar.gz: 8fbf1462385f189dbb796000d89c754e37336afa6022cf030686a272fbb30ce6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 892e4f38dbed074b9327bf144f25713c39efb5f0987b8c5954ff84b939b2f10e753b1f943d3b48b8de214713c6016c7ef0bea49591463c7023d83e4701e237af
|
|
7
|
+
data.tar.gz: 2032469e797222570de5853801a0d60312d6291d271e308379651c38585251962165e81e98af57702dc6f87fab6ffebd8e9dd9331a74b7d2ff3bc56740e33d44
|
data/.envrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export OLLAMA_CHAT_TOOLS_TEST_RUNNER="bundle exec rake test"
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# JSON::GenericObject 🧊
|
|
2
|
+
|
|
3
|
+
## Description 📝
|
|
4
|
+
|
|
5
|
+
This gem provides `JSON::GenericObject` for method-style access to JSON data.
|
|
6
|
+
Instead of drilling into nested hashes with `obj["a"]["b"]["c"]`, you can
|
|
7
|
+
access attributes directly: `obj.a.b.c`. 🚀
|
|
8
|
+
|
|
9
|
+
This class was previously part of the [`json` gem](https://github.com/ruby/json)
|
|
10
|
+
and was extracted into its own gem for json 3.0.0.
|
|
11
|
+
|
|
12
|
+
## Installation 📦
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
gem install json-generic_object
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Or add it to your Gemfile:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem 'json-generic_object'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Then `require 'json/generic_object'` in your application. The `json` (>= 2.0)
|
|
25
|
+
and `ostruct` gems are pulled in automatically as dependencies.
|
|
26
|
+
|
|
27
|
+
## Usage 🛠️
|
|
28
|
+
|
|
29
|
+
### Creating from a hash
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
go = JSON::GenericObject[a: 1, b: 2]
|
|
33
|
+
go.a #=> 1
|
|
34
|
+
go.b #=> 2
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Parsing JSON with method access
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
result = JSON('{"name":"Jane","address":{"city":"Berlin"}}',
|
|
41
|
+
object_class: JSON::GenericObject
|
|
42
|
+
)
|
|
43
|
+
result.name #=> "Jane"
|
|
44
|
+
result.address.city #=> "Berlin"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Serializing with `json_class`
|
|
48
|
+
|
|
49
|
+
`to_json` embeds a `json_class` marker in the output. Note that the json
|
|
50
|
+
parser (>= 3.0) no longer consumes this marker — re-parsing yields a plain
|
|
51
|
+
`Hash` unless you pass `object_class:`:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
go = JSON::GenericObject[hello: 'world']
|
|
55
|
+
json = go.to_json
|
|
56
|
+
# => '{"json_class":"JSON::GenericObject","hello":"world"}'
|
|
57
|
+
|
|
58
|
+
# Re-parse with method access:
|
|
59
|
+
parsed = JSON.parse(json, object_class: JSON::GenericObject)
|
|
60
|
+
parsed.hello #=> "world"
|
|
61
|
+
parsed.class #=> JSON::GenericObject
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Converting plain data structures
|
|
65
|
+
|
|
66
|
+
`from_hash` recursively converts hashes and arrays into a tree of
|
|
67
|
+
`GenericObject` instances:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
result = JSON::GenericObject.from_hash(
|
|
71
|
+
foo: { bar: { baz: true }, quux: [{ foobar: true }] }
|
|
72
|
+
)
|
|
73
|
+
result.foo.bar.baz #=> true
|
|
74
|
+
result.foo.quux.first.foobar #=> true
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Merging
|
|
78
|
+
|
|
79
|
+
The `|` operator merges two objects, with the receiver's attributes
|
|
80
|
+
taking precedence on key conflicts:
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
a = JSON::GenericObject[x: 1, y: 2]
|
|
84
|
+
b = JSON::GenericObject[y: 99, z: 3]
|
|
85
|
+
merged = b | a
|
|
86
|
+
merged.x #=> 1
|
|
87
|
+
merged.y #=> 2 (a wins)
|
|
88
|
+
merged.z #=> 3
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API Reference 📚
|
|
92
|
+
|
|
93
|
+
### Class Methods
|
|
94
|
+
|
|
95
|
+
- **`[]`** — Creates a new instance from a hash of attributes
|
|
96
|
+
- **`json_create(data)`** — Deserialization hook (deprecated: the json
|
|
97
|
+
3.0+ parser no longer calls this)
|
|
98
|
+
- **`from_hash(object)`** — Recursively converts a data structure
|
|
99
|
+
- **`load(source, proc, opts)`** — Parses a JSON string into a GenericObject
|
|
100
|
+
- **`dump(obj, *args)`** — Serializes an object to a JSON string
|
|
101
|
+
- **`json_creatable?`** / **`json_creatable=`** — Toggle the `json_class`
|
|
102
|
+
reconstruction protocol (defaults to `false`)
|
|
103
|
+
|
|
104
|
+
### Instance Methods
|
|
105
|
+
|
|
106
|
+
- **`to_hash`** — Returns the internal attribute table as a plain Hash
|
|
107
|
+
- **`|(other)`** — Merges another object into a new GenericObject
|
|
108
|
+
- **`as_json`** — Returns the JSON-serializable representation with `json_class`
|
|
109
|
+
- **`to_json`** — Serializes this object to a JSON string
|
|
110
|
+
|
|
111
|
+
## Download 📥
|
|
112
|
+
|
|
113
|
+
The homepage of this library is located at
|
|
114
|
+
|
|
115
|
+
* https://github.com/flori/json-generic_object
|
|
116
|
+
|
|
117
|
+
## Author 👨💻
|
|
118
|
+
|
|
119
|
+
[Florian Frank](mailto:flori@ping.de)
|
|
120
|
+
|
|
121
|
+
## License 📄
|
|
122
|
+
|
|
123
|
+
This software is licensed under the [MIT license](LICENSE).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'gem_hadar'
|
|
2
|
+
|
|
3
|
+
GemHadar do
|
|
4
|
+
name 'json-generic_object'
|
|
5
|
+
path_module 'JSON::GenericObject'
|
|
6
|
+
module_type 'class'
|
|
7
|
+
author 'Florian Frank'
|
|
8
|
+
email 'flori@ping.de'
|
|
9
|
+
homepage "https://github.com/flori/#{name}"
|
|
10
|
+
summary 'JSON::GenericObject extracted from the json gem.'
|
|
11
|
+
description <<~EOT
|
|
12
|
+
Provides JSON::GenericObject for method-style access to JSON
|
|
13
|
+
objects. Previously part of the json gem, extracted for
|
|
14
|
+
json 3.0.0.
|
|
15
|
+
EOT
|
|
16
|
+
test_dir 'test'
|
|
17
|
+
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.AppleDouble',
|
|
18
|
+
'.bundle', '.yardoc', 'tags', 'cscope.out', 'doc'
|
|
19
|
+
readme 'README.md'
|
|
20
|
+
|
|
21
|
+
required_ruby_version '>= 2.7'
|
|
22
|
+
dependency 'json', '>= 2.0'
|
|
23
|
+
dependency 'ostruct'
|
|
24
|
+
development_dependency 'rake'
|
|
25
|
+
development_dependency 'test-unit'
|
|
26
|
+
development_dependency 'simplecov', '~> 1.3'
|
|
27
|
+
licenses << 'MIT'
|
|
28
|
+
end
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.0
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
# stub: json-generic_object 0.0.0 ruby lib
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |s|
|
|
5
|
+
s.name = "json-generic_object".freeze
|
|
6
|
+
s.version = "0.0.0".freeze
|
|
7
|
+
|
|
8
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
|
9
|
+
s.require_paths = ["lib".freeze]
|
|
10
|
+
s.authors = ["Florian Frank".freeze]
|
|
11
|
+
s.date = "1980-01-02"
|
|
12
|
+
s.description = "Provides JSON::GenericObject for method-style access to JSON\nobjects. Previously part of the json gem, extracted for\njson 3.0.0.\n".freeze
|
|
13
|
+
s.email = "flori@ping.de".freeze
|
|
14
|
+
s.extra_rdoc_files = ["README.md".freeze, "lib/json-generic_object/version.rb".freeze, "lib/json/generic_object.rb".freeze]
|
|
15
|
+
s.files = [".envrc".freeze, ".gitignore".freeze, "Gemfile".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "json-generic_object.gemspec".freeze, "lib/json-generic_object/version.rb".freeze, "lib/json/generic_object.rb".freeze, "test/json_generic_object_test.rb".freeze, "test/test_helper.rb".freeze]
|
|
16
|
+
s.homepage = "https://github.com/flori/json-generic_object".freeze
|
|
17
|
+
s.licenses = ["MIT".freeze]
|
|
18
|
+
s.rdoc_options = ["--title".freeze, "Json-genericObject - JSON::GenericObject extracted from the json gem.".freeze, "--main".freeze, "README.md".freeze]
|
|
19
|
+
s.required_ruby_version = Gem::Requirement.new(">= 2.7".freeze)
|
|
20
|
+
s.rubygems_version = "4.0.24".freeze
|
|
21
|
+
s.summary = "JSON::GenericObject extracted from the json gem.".freeze
|
|
22
|
+
s.test_files = ["test/json_generic_object_test.rb".freeze, "test/test_helper.rb".freeze]
|
|
23
|
+
|
|
24
|
+
s.specification_version = 4
|
|
25
|
+
|
|
26
|
+
s.add_development_dependency(%q<gem_hadar>.freeze, [">= 2.24.0".freeze])
|
|
27
|
+
s.add_development_dependency(%q<rake>.freeze, [">= 0".freeze])
|
|
28
|
+
s.add_development_dependency(%q<test-unit>.freeze, [">= 0".freeze])
|
|
29
|
+
s.add_development_dependency(%q<simplecov>.freeze, ["~> 1.3".freeze])
|
|
30
|
+
s.add_runtime_dependency(%q<json>.freeze, [">= 2.0".freeze])
|
|
31
|
+
s.add_runtime_dependency(%q<ostruct>.freeze, [">= 0".freeze])
|
|
32
|
+
end
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'ostruct'
|
|
5
|
+
|
|
6
|
+
module JSON
|
|
7
|
+
# Provides a generic object for method-style access to JSON data.
|
|
8
|
+
#
|
|
9
|
+
# Parsed JSON objects can be converted into instances of this class,
|
|
10
|
+
# allowing attribute access via `object.foo` instead of
|
|
11
|
+
# `object["foo"]`. Nested hashes and arrays are recursively converted.
|
|
12
|
+
#
|
|
13
|
+
# This class was previously part of the {file:README.md `json` gem}
|
|
14
|
+
# and was extracted into its own gem for json 3.0.0.
|
|
15
|
+
#
|
|
16
|
+
# @example Creating from a hash
|
|
17
|
+
# go = JSON::GenericObject[a: 1, b: 2]
|
|
18
|
+
# go.a #=> 1
|
|
19
|
+
# go.b #=> 2
|
|
20
|
+
#
|
|
21
|
+
# @example Parsing JSON with method access
|
|
22
|
+
# result = JSON('{"name":"Jane"}', object_class: JSON::GenericObject)
|
|
23
|
+
# result.name #=> "Jane"
|
|
24
|
+
class GenericObject < OpenStruct
|
|
25
|
+
class << self
|
|
26
|
+
# Creates a new GenericObject from a hash of attributes.
|
|
27
|
+
#
|
|
28
|
+
# @return [GenericObject] a new instance
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
31
|
+
# go = JSON::GenericObject[a: 1, b: 2]
|
|
32
|
+
# go.a #=> 1
|
|
33
|
+
alias [] new
|
|
34
|
+
|
|
35
|
+
# Checks whether GenericObject instances can be created during
|
|
36
|
+
# JSON parsing via the `json_class` / `json_create` protocol.
|
|
37
|
+
#
|
|
38
|
+
# @note The `create_additions` option was removed in json 3.0,
|
|
39
|
+
# so the json parser no longer consults this flag or calls
|
|
40
|
+
# #json_create. This method is retained for API compatibility
|
|
41
|
+
# and for use with custom serializers.
|
|
42
|
+
#
|
|
43
|
+
# @return [Boolean, nil] the current creatable flag
|
|
44
|
+
def json_creatable?
|
|
45
|
+
@json_creatable
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Sets whether GenericObject instances can be created during
|
|
49
|
+
# JSON parsing. See #json_creatable?.
|
|
50
|
+
#
|
|
51
|
+
# @note No effect with json >= 3.0, as the parser no longer
|
|
52
|
+
# supports `create_additions`. Retained for API compatibility.
|
|
53
|
+
#
|
|
54
|
+
# @param value [Boolean] whether to enable json_create
|
|
55
|
+
# @return [Boolean] the value that was set
|
|
56
|
+
attr_writer :json_creatable
|
|
57
|
+
|
|
58
|
+
# Creates a GenericObject from a parsed JSON hash.
|
|
59
|
+
#
|
|
60
|
+
# Part of the `json_class` / `json_create` deserialization
|
|
61
|
+
# protocol. The `json_class` key is stripped before
|
|
62
|
+
# construction.
|
|
63
|
+
#
|
|
64
|
+
# @note The json parser no longer calls this method since
|
|
65
|
+
# `create_additions` was removed in json 3.0. It is retained
|
|
66
|
+
# for use with custom serializers.
|
|
67
|
+
#
|
|
68
|
+
# @param data [Hash] the parsed JSON object
|
|
69
|
+
# @return [GenericObject] a new instance
|
|
70
|
+
def json_create(data)
|
|
71
|
+
data = data.dup
|
|
72
|
+
data.delete JSON.create_id
|
|
73
|
+
self[data]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Recursively converts a plain Ruby data structure into a tree
|
|
77
|
+
# of GenericObject instances.
|
|
78
|
+
#
|
|
79
|
+
# Hashes become GenericObjects, arrays are mapped element-wise,
|
|
80
|
+
# and scalar values (strings, numbers, booleans, `nil`) are
|
|
81
|
+
# returned unchanged.
|
|
82
|
+
#
|
|
83
|
+
# @param object [Object] the data structure to convert
|
|
84
|
+
# @return [GenericObject, Array, Object] the converted structure
|
|
85
|
+
#
|
|
86
|
+
# @example
|
|
87
|
+
# result = JSON::GenericObject.from_hash(
|
|
88
|
+
# foo: { bar: { baz: true } }
|
|
89
|
+
# )
|
|
90
|
+
# result.foo.bar.baz #=> true
|
|
91
|
+
def from_hash(object)
|
|
92
|
+
case
|
|
93
|
+
when object.respond_to?(:to_hash)
|
|
94
|
+
result = new
|
|
95
|
+
object.to_hash.each do |key, value|
|
|
96
|
+
result[key] = from_hash(value)
|
|
97
|
+
end
|
|
98
|
+
result
|
|
99
|
+
when object.respond_to?(:to_ary)
|
|
100
|
+
object.to_ary.map { |a| from_hash(a) }
|
|
101
|
+
else
|
|
102
|
+
object
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Parses a JSON string into a GenericObject.
|
|
107
|
+
#
|
|
108
|
+
# Convenience wrapper around `JSON.load` that sets
|
|
109
|
+
# `object_class` to GenericObject. If the source is `nil` or
|
|
110
|
+
# parses to `nil`, an empty GenericObject is returned.
|
|
111
|
+
#
|
|
112
|
+
# @param source [String, nil] JSON string to parse
|
|
113
|
+
# @param proc [Proc, nil] optional value transformation block
|
|
114
|
+
# @param opts [Hash] additional keyword options for JSON.load
|
|
115
|
+
# @return [GenericObject] the parsed object
|
|
116
|
+
#
|
|
117
|
+
# @example
|
|
118
|
+
# go = JSON::GenericObject.load('{"hello":"world"}')
|
|
119
|
+
# go.hello #=> "world"
|
|
120
|
+
def load(source, proc = nil, opts = {})
|
|
121
|
+
result = ::JSON.load(source, proc, opts.merge(:object_class => self))
|
|
122
|
+
result.nil? ? new : result
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Serializes an object to a JSON string.
|
|
126
|
+
#
|
|
127
|
+
# Thin wrapper around `JSON.dump` for symmetry with #load.
|
|
128
|
+
#
|
|
129
|
+
# @param obj [Object] the object to serialize
|
|
130
|
+
# @param args [Array] additional arguments passed to JSON.dump
|
|
131
|
+
# @return [String] the JSON representation
|
|
132
|
+
def dump(obj, *args)
|
|
133
|
+
::JSON.dump(obj, *args)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
self.json_creatable = false
|
|
137
|
+
|
|
138
|
+
# Returns the internal attribute table as a plain Hash.
|
|
139
|
+
#
|
|
140
|
+
# @return [Hash] the underlying key/value storage
|
|
141
|
+
def to_hash
|
|
142
|
+
table
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Merges another object into a new GenericObject.
|
|
146
|
+
#
|
|
147
|
+
# Attributes from +other+ are merged under +self+; on key
|
|
148
|
+
# conflicts, +self+ wins.
|
|
149
|
+
#
|
|
150
|
+
# @param other [Object] an object responding to `to_hash`
|
|
151
|
+
# @return [GenericObject] a new merged instance
|
|
152
|
+
def |(other)
|
|
153
|
+
self.class[other.to_hash.merge(to_hash)]
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Returns the JSON-serializable representation of this object.
|
|
157
|
+
#
|
|
158
|
+
# Includes the `json_class` marker in the output. Note that the
|
|
159
|
+
# json parser no longer consumes this marker (the
|
|
160
|
+
# `create_additions` option was removed in json 3.0), so
|
|
161
|
+
# round-tripping through `JSON.parse` will yield a plain Hash
|
|
162
|
+
# unless a custom serializer handles the tag.
|
|
163
|
+
#
|
|
164
|
+
# @return [Hash] a hash with `json_class` and all attributes
|
|
165
|
+
def as_json(*)
|
|
166
|
+
{ JSON.create_id => self.class.name }.merge to_hash
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Serializes this GenericObject to a JSON string.
|
|
170
|
+
#
|
|
171
|
+
# Delegates to #as_json and then calls `to_json` on the resulting
|
|
172
|
+
# hash, embedding the `json_class` marker automatically.
|
|
173
|
+
#
|
|
174
|
+
# @param a [Array] additional arguments passed through
|
|
175
|
+
# @return [String] the JSON representation
|
|
176
|
+
#
|
|
177
|
+
# @example
|
|
178
|
+
# go = JSON::GenericObject[hello: 'world']
|
|
179
|
+
# go.to_json
|
|
180
|
+
# #=> '{"json_class":"JSON::GenericObject","hello":"world"}'
|
|
181
|
+
def to_json(*a)
|
|
182
|
+
as_json.to_json(*a)
|
|
183
|
+
end
|
|
184
|
+
end unless defined? JSON::GenericObject
|
|
185
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Version information for the JSON::GenericObject gem.
|
|
2
|
+
#
|
|
3
|
+
# This class is auto-generated by GemHadar and exposes the
|
|
4
|
+
# current release version as a set of constants. Do not edit it
|
|
5
|
+
# by hand; it is rewritten on every `rake version` invocation.
|
|
6
|
+
class JSON::GenericObject
|
|
7
|
+
# The current version of the JSON::GenericObject gem as a String.
|
|
8
|
+
VERSION = '0.0.0'
|
|
9
|
+
VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
|
|
10
|
+
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
|
|
11
|
+
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
|
|
12
|
+
VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
|
|
13
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require_relative 'test_helper'
|
|
3
|
+
|
|
4
|
+
class JSONGenericObjectTest < Test::Unit::TestCase
|
|
5
|
+
def setup
|
|
6
|
+
@go = JSON::GenericObject[ :a => 1, :b => 2 ]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def test_attributes
|
|
10
|
+
assert_equal 1, @go.a
|
|
11
|
+
assert_equal 1, @go[:a]
|
|
12
|
+
assert_equal 2, @go.b
|
|
13
|
+
assert_equal 2, @go[:b]
|
|
14
|
+
assert_nil @go.c
|
|
15
|
+
assert_nil @go[:c]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_parse_json
|
|
19
|
+
assert_kind_of Hash,
|
|
20
|
+
JSON('{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }')
|
|
21
|
+
parsed = JSON('{ "a": { "b": 2 } }', :object_class => JSON::GenericObject)
|
|
22
|
+
assert_equal JSON::GenericObject[:a => JSON::GenericObject[:b => 2]], parsed
|
|
23
|
+
assert_kind_of JSON::GenericObject, parsed
|
|
24
|
+
assert_equal 2, parsed.a.b
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_from_hash
|
|
28
|
+
result = JSON::GenericObject.from_hash(
|
|
29
|
+
:foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] })
|
|
30
|
+
assert_kind_of JSON::GenericObject, result.foo
|
|
31
|
+
assert_kind_of JSON::GenericObject, result.foo.bar
|
|
32
|
+
assert_equal true, result.foo.bar.baz
|
|
33
|
+
assert_kind_of JSON::GenericObject, result.foo.quux.first
|
|
34
|
+
assert_equal true, result.foo.quux.first.foobar
|
|
35
|
+
assert_equal true, JSON::GenericObject.from_hash(true)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_json_generic_object_load
|
|
39
|
+
empty = JSON::GenericObject.load(nil)
|
|
40
|
+
assert_kind_of JSON::GenericObject, empty
|
|
41
|
+
simple_json = '{"json_class":"JSON::GenericObject","hello":"world"}'
|
|
42
|
+
simple = JSON::GenericObject.load(simple_json)
|
|
43
|
+
assert_kind_of JSON::GenericObject, simple
|
|
44
|
+
assert_equal "world", simple.hello
|
|
45
|
+
converting = JSON::GenericObject.load('{ "hello": "world" }')
|
|
46
|
+
assert_kind_of JSON::GenericObject, converting
|
|
47
|
+
assert_equal "world", converting.hello
|
|
48
|
+
|
|
49
|
+
json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world'])
|
|
50
|
+
assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}')
|
|
51
|
+
end
|
|
52
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: json-generic_object
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Florian Frank
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: gem_hadar
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 2.24.0
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 2.24.0
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rake
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: test-unit
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: simplecov
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '1.3'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '1.3'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: json
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '2.0'
|
|
75
|
+
type: :runtime
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '2.0'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: ostruct
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
type: :runtime
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
description: |
|
|
97
|
+
Provides JSON::GenericObject for method-style access to JSON
|
|
98
|
+
objects. Previously part of the json gem, extracted for
|
|
99
|
+
json 3.0.0.
|
|
100
|
+
email: flori@ping.de
|
|
101
|
+
executables: []
|
|
102
|
+
extensions: []
|
|
103
|
+
extra_rdoc_files:
|
|
104
|
+
- README.md
|
|
105
|
+
- lib/json-generic_object/version.rb
|
|
106
|
+
- lib/json/generic_object.rb
|
|
107
|
+
files:
|
|
108
|
+
- ".envrc"
|
|
109
|
+
- ".gitignore"
|
|
110
|
+
- Gemfile
|
|
111
|
+
- README.md
|
|
112
|
+
- Rakefile
|
|
113
|
+
- VERSION
|
|
114
|
+
- json-generic_object.gemspec
|
|
115
|
+
- lib/json-generic_object/version.rb
|
|
116
|
+
- lib/json/generic_object.rb
|
|
117
|
+
- test/json_generic_object_test.rb
|
|
118
|
+
- test/test_helper.rb
|
|
119
|
+
homepage: https://github.com/flori/json-generic_object
|
|
120
|
+
licenses:
|
|
121
|
+
- MIT
|
|
122
|
+
metadata: {}
|
|
123
|
+
rdoc_options:
|
|
124
|
+
- "--title"
|
|
125
|
+
- Json-genericObject - JSON::GenericObject extracted from the json gem.
|
|
126
|
+
- "--main"
|
|
127
|
+
- README.md
|
|
128
|
+
require_paths:
|
|
129
|
+
- lib
|
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
|
+
requirements:
|
|
132
|
+
- - ">="
|
|
133
|
+
- !ruby/object:Gem::Version
|
|
134
|
+
version: '2.7'
|
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
|
+
requirements:
|
|
137
|
+
- - ">="
|
|
138
|
+
- !ruby/object:Gem::Version
|
|
139
|
+
version: '0'
|
|
140
|
+
requirements: []
|
|
141
|
+
rubygems_version: 4.0.24
|
|
142
|
+
specification_version: 4
|
|
143
|
+
summary: JSON::GenericObject extracted from the json gem.
|
|
144
|
+
test_files:
|
|
145
|
+
- test/json_generic_object_test.rb
|
|
146
|
+
- test/test_helper.rb
|