json-generic_object 0.0.1 → 0.1.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 +4 -4
- data/VERSION +1 -1
- data/json-generic_object.gemspec +2 -2
- data/lib/json/generic_object.rb +27 -3
- data/lib/json-generic_object/version.rb +1 -1
- data/test/json_generic_object_test.rb +17 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: da8b8d08b8e0c15b7d94f9a0ad9cc96867cff18ca27a01f8f515f58f6e3c49e2
|
|
4
|
+
data.tar.gz: e4fefe3c74b8774ebf22339a1265f83e46a490c1ecdaace248221045c4b0149d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 751eda3dda595b976cfc796f6736188130bba3cff4e7eb1f067335ce3384481da088e1b3054b3d4125604363f4685e79c309c6661695b409983c2fc372611ddd
|
|
7
|
+
data.tar.gz: b7d9185cd4055feb9a9831eb2c5feb8e3eb0169e1e3b4ff17717597b25a4b36d7e4d78a646f04f8eb57b29ce6ed52cf14e01a81324abfa76560ab7b1966f6540
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.0
|
|
1
|
+
0.1.0
|
data/json-generic_object.gemspec
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
# stub: json-generic_object 0.0
|
|
2
|
+
# stub: json-generic_object 0.1.0 ruby lib
|
|
3
3
|
|
|
4
4
|
Gem::Specification.new do |s|
|
|
5
5
|
s.name = "json-generic_object".freeze
|
|
6
|
-
s.version = "0.0
|
|
6
|
+
s.version = "0.1.0".freeze
|
|
7
7
|
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
|
9
9
|
s.require_paths = ["lib".freeze]
|
data/lib/json/generic_object.rb
CHANGED
|
@@ -23,6 +23,25 @@ module JSON
|
|
|
23
23
|
# result.name #=> "Jane"
|
|
24
24
|
class GenericObject < OpenStruct
|
|
25
25
|
class << self
|
|
26
|
+
# Returns the key name used to mark the class identity of a
|
|
27
|
+
# serialized GenericObject.
|
|
28
|
+
#
|
|
29
|
+
# On json 2.x this delegates to `JSON.create_id`; on json 3.x
|
|
30
|
+
# (where `create_id` was removed) it falls back to the literal
|
|
31
|
+
# string `"json_class"`.
|
|
32
|
+
#
|
|
33
|
+
# Used internally by #json_create and #as_json to read or write
|
|
34
|
+
# the class tag in the serialized output.
|
|
35
|
+
#
|
|
36
|
+
# @return [String] the key name (e.g. `"json_class"`)
|
|
37
|
+
def json_create_id
|
|
38
|
+
if ::JSON.respond_to?(:create_id)
|
|
39
|
+
JSON.create_id
|
|
40
|
+
else
|
|
41
|
+
'json_class'
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
26
45
|
# Creates a new GenericObject from a hash of attributes.
|
|
27
46
|
#
|
|
28
47
|
# @return [GenericObject] a new instance
|
|
@@ -69,7 +88,7 @@ module JSON
|
|
|
69
88
|
# @return [GenericObject] a new instance
|
|
70
89
|
def json_create(data)
|
|
71
90
|
data = data.dup
|
|
72
|
-
data.delete JSON.
|
|
91
|
+
data.delete JSON::GenericObject.json_create_id
|
|
73
92
|
self[data]
|
|
74
93
|
end
|
|
75
94
|
|
|
@@ -118,7 +137,12 @@ module JSON
|
|
|
118
137
|
# go = JSON::GenericObject.load('{"hello":"world"}')
|
|
119
138
|
# go.hello #=> "world"
|
|
120
139
|
def load(source, proc = nil, opts = {})
|
|
121
|
-
result =
|
|
140
|
+
result =
|
|
141
|
+
if ::JSON.method(:load).parameters.any? { |pt,| pt == :keyrest }
|
|
142
|
+
::JSON.load(source, proc, **opts.merge(:object_class => self))
|
|
143
|
+
else
|
|
144
|
+
::JSON.load(source, proc, opts.merge(:object_class => self))
|
|
145
|
+
end
|
|
122
146
|
result.nil? ? new : result
|
|
123
147
|
end
|
|
124
148
|
|
|
@@ -163,7 +187,7 @@ module JSON
|
|
|
163
187
|
#
|
|
164
188
|
# @return [Hash] a hash with `json_class` and all attributes
|
|
165
189
|
def as_json(*)
|
|
166
|
-
{ JSON.
|
|
190
|
+
{ JSON::GenericObject.json_create_id => self.class.name }.merge to_hash
|
|
167
191
|
end
|
|
168
192
|
|
|
169
193
|
# Serializes this GenericObject to a JSON string.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
# by hand; it is rewritten on every `rake version` invocation.
|
|
6
6
|
class JSON::GenericObject
|
|
7
7
|
# The current version of the JSON::GenericObject gem as a String.
|
|
8
|
-
VERSION = '0.0
|
|
8
|
+
VERSION = '0.1.0'
|
|
9
9
|
VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
|
|
10
10
|
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
|
|
11
11
|
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
|
|
@@ -49,4 +49,21 @@ class JSONGenericObjectTest < Test::Unit::TestCase
|
|
|
49
49
|
json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world'])
|
|
50
50
|
assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}')
|
|
51
51
|
end
|
|
52
|
+
|
|
53
|
+
def test_json_create_id
|
|
54
|
+
id = JSON::GenericObject.json_create_id
|
|
55
|
+
assert_equal 'json_class', id
|
|
56
|
+
|
|
57
|
+
h = @go.as_json
|
|
58
|
+
assert_equal 'JSON::GenericObject', h[id]
|
|
59
|
+
assert_equal 1, h[:a]
|
|
60
|
+
assert_equal 2, h[:b]
|
|
61
|
+
|
|
62
|
+
go = JSON::GenericObject.json_create(
|
|
63
|
+
id => 'JSON::GenericObject', 'a' => 42
|
|
64
|
+
)
|
|
65
|
+
assert_kind_of JSON::GenericObject, go
|
|
66
|
+
assert_equal 42, go.a
|
|
67
|
+
assert_nil go[:json_class]
|
|
68
|
+
end
|
|
52
69
|
end
|