json_parse 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +21 -0
- data/README.md +149 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/json_parse.gemspec +43 -0
- data/lib/json_parse.rb +310 -0
- data/lib/json_parse/version.rb +3 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ad25f31480a214b84831cc94de6f493b4fa25dd9cf4cdc6a3547f46096319c38
|
4
|
+
data.tar.gz: f8d1dea85965fe7fcf1a1c1b007d3a7fada2d02ce01468ee193e462efdced2a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 93f4605824ce3d8d22d1d0e226a651cf620d52158502a1d36bdd3b6031897499b65ad6884a5b1767678f291292486bf57ddfc6b428dba48831650c9dad423d2b
|
7
|
+
data.tar.gz: 70358538198a185ae3e41ca514db90e8dc09467d043169b620a691a15eb0a84679e50882900936d8bdd555070bf99d541f0e7b8da6187fb650c3828982ca260b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
json_parse (0.1.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.1)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
concurrent-ruby (1.1.4)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
i18n (1.1.1)
|
18
|
+
concurrent-ruby (~> 1.0)
|
19
|
+
minitest (5.11.3)
|
20
|
+
rake (10.5.0)
|
21
|
+
rspec (3.8.0)
|
22
|
+
rspec-core (~> 3.8.0)
|
23
|
+
rspec-expectations (~> 3.8.0)
|
24
|
+
rspec-mocks (~> 3.8.0)
|
25
|
+
rspec-core (3.8.0)
|
26
|
+
rspec-support (~> 3.8.0)
|
27
|
+
rspec-expectations (3.8.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.8.0)
|
30
|
+
rspec-mocks (3.8.0)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.8.0)
|
33
|
+
rspec-support (3.8.0)
|
34
|
+
thread_safe (0.3.6)
|
35
|
+
tzinfo (1.2.5)
|
36
|
+
thread_safe (~> 0.1)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
bundler (~> 1.17)
|
43
|
+
json_parse!
|
44
|
+
rake (~> 10.0)
|
45
|
+
rspec (~> 3.2)
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Grapevine Gizmos LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# JsonParse
|
2
|
+
|
3
|
+
JsonParse is a gem for use in Rails to discover and navigate json documents with simple dot notation, and to extract subtrees, nodes and values with a minimum of fuss.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'json_parse'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install JsonParse
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Load with Json string:
|
25
|
+
|
26
|
+
`my_jsonstring= "{\"company_name\":\"Acme Industries\",\"company_id\":1085}"
|
27
|
+
`
|
28
|
+
`jp=JsonParse.new(my_jsonstring)`
|
29
|
+
|
30
|
+
Load with a hash
|
31
|
+
|
32
|
+
`jp=JsonParse.new({company_name: "Acme Industries", company_id: 1085})`
|
33
|
+
|
34
|
+
Access an element using dot notation:
|
35
|
+
|
36
|
+
`puts "#{jp.company_name} id: #{jp.company_id}` #Acme Industries id: 1085
|
37
|
+
|
38
|
+
Consider Json representation of a more complex object: (thanks to [cuberule.com](http://cuberule.com)!)
|
39
|
+
|
40
|
+
`cuberule = "{\"rule\":\"cube\",\"authority\":\"http://cuberule.com\",\"specification\":{\"salad\":{\"sides\":0,\"examples\":[{\"name\":\"steak\",\"type\":\"salad\"},{\"name\":\"mashed potatoes\",\"type\":\"salad\"},{\"name\":\"fried rice\",\"type\":\"salad\"},{\"name\":\"poutine\",\"type\":\"salad\"}],\"name\":\"salad\"},\"toast\":{\"sides\":1,\"examples\":[{\"name\":\"pizza\",\"type\":\"toast\"},{\"name\":\"muffins\",\"type\":\"toast\"},{\"name\":\"salmon sushi\",\"type\":\"toast\"}],\"name\":\"toast\"},\"sandwich\":{\"sides\":2,\"examples\":[{\"name\":\"lasagna\",\"type\":\"sandwich\"},{\"name\":\"quesadilla\",\"type\":\"sandwich\"}],\"name\":\"sandwich\"},\"taco\":{\"sides\":3,\"examples\":[{\"name\":\"hot dog\",\"type\":\"taco\"},{\"name\":\"slice of pie\",\"type\":\"taco\"},{\"name\":\"subway sandwich\",\"type\":\"taco\"}],\"name\":\"taco\"},\"sushi\":{\"sides\":4,\"examples\":[{\"name\":\"pigs in a blanket\",\"type\":\"sushi\"},{\"name\":\"enchilada\",\"type\":\"sushi\"},{\"name\":\"felafel wrap\",\"type\":\"sushi\"}],\"name\":\"sushi\"},\"quiche\":{\"sides\":5,\"examples\":[{\"name\":\"cheesecake\",\"type\":\"quiche\"},{\"name\":\"soup in a breadbowl\",\"type\":\"quiche\"},{\"name\":\"key lime pie\",\"type\":\"quiche\"},{\"name\":\"deep-dish pizza\",\"type\":\"quiche\"}],\"name\":\"quiche\"},\"calzone\":{\"sides\":6,\"examples\":[{\"name\":\"burrito\",\"type\":\"calzone\"},{\"name\":\"corn dog\",\"type\":\"calzone\"},{\"name\":\"pop tart\",\"type\":\"calzone\"}],\"name\":\"calzone\"}}}"`
|
41
|
+
|
42
|
+
Pretty printing the hash using jp.hash gives a comprehensible view.
|
43
|
+
|
44
|
+
|
45
|
+
`jp= JsonParse.new(cuberule)`
|
46
|
+
`puts pp jp.hash`
|
47
|
+
```ruby
|
48
|
+
{:rule=>"cube",
|
49
|
+
:authority=>"http://cuberule.com",
|
50
|
+
:specification=>
|
51
|
+
{:salad=>
|
52
|
+
{:sides=>0,
|
53
|
+
:examples=>
|
54
|
+
[{"name"=>"steak", "type"=>"salad"},
|
55
|
+
{"name"=>"mashed potatoes", "type"=>"salad"},
|
56
|
+
{"name"=>"fried rice", "type"=>"salad"},
|
57
|
+
{"name"=>"poutine", "type"=>"salad"}],
|
58
|
+
:name=>"salad"},
|
59
|
+
:toast=>
|
60
|
+
{:sides=>1,
|
61
|
+
:examples=>
|
62
|
+
[{"name"=>"pizza", "type"=>"toast"},
|
63
|
+
{"name"=>"muffins", "type"=>"toast"},
|
64
|
+
{"name"=>"salmon sushi", "type"=>"toast"}],
|
65
|
+
:name=>"toast"},
|
66
|
+
[...]
|
67
|
+
:calzone=>
|
68
|
+
{:sides=>6,
|
69
|
+
:examples=>
|
70
|
+
[{"name"=>"burrito", "type"=>"calzone"},
|
71
|
+
{"name"=>"corn dog", "type"=>"calzone"},
|
72
|
+
{"name"=>"pop tart", "type"=>"calzone"}],
|
73
|
+
:name=>"calzone"}}}
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
We can retrieve the value of any node by following the tree:
|
78
|
+
```Ruby
|
79
|
+
jp.specification.toast.sides
|
80
|
+
=> 6
|
81
|
+
jp.specification.calzone.examples[0].name'
|
82
|
+
=>"burrito"
|
83
|
+
````
|
84
|
+
|
85
|
+
Obtain the keys to any branch of the json document using the `keys` method. If the referenced element is a container of other elements, the keys it contains are returned by the keys method. If the element contains an array, '[]' is returned. If the keys method is called on an element whose value is a scalar, an undefined method error will result.
|
86
|
+
|
87
|
+
```
|
88
|
+
`puts jp.specification.calzone.keys`
|
89
|
+
=> [:sides, :examples, :name]`
|
90
|
+
|
91
|
+
jp.specification.calzone.examples.keys
|
92
|
+
=> "[]"
|
93
|
+
jp.specification.calzone.sides.keys
|
94
|
+
jp.specification.calzone.sides.keys
|
95
|
+
=> undefined method `keys' for 6:Integer
|
96
|
+
```
|
97
|
+
|
98
|
+
|
99
|
+
Dot notation is used to access the value of an element in the json docuemnt. if the element is a key to another set of json elements, a new JsonParse is created and returned to the caller.
|
100
|
+
|
101
|
+
```Ruby
|
102
|
+
branch=jp.specification.calzone #brach contains a new JsonParse object
|
103
|
+
branch.keys #[:sides, :examples, :names]
|
104
|
+
branch.sides
|
105
|
+
=> 6
|
106
|
+
````
|
107
|
+
If the referenced element contains an array, it will be retrieved as an array of JsonParse. Its elements can be accessed by index. The keys method can be used on the indexed element to reveal the available symbols.
|
108
|
+
```
|
109
|
+
examples = jp.specification.calzone.examples
|
110
|
+
puts examples[1].keys
|
111
|
+
=>[:name, :type]
|
112
|
+
|
113
|
+
puts "#{examples[1].name} type: #{examples[1].type}"
|
114
|
+
=> "corn dog type: calzone"
|
115
|
+
```
|
116
|
+
|
117
|
+
JsonParse arrays can be interated with the `each` method.
|
118
|
+
```Ruby
|
119
|
+
jp.specification.salad.examples.each do |example| puts example.name end`
|
120
|
+
|
121
|
+
steak
|
122
|
+
mashed potatoes
|
123
|
+
fried rice
|
124
|
+
poutine
|
125
|
+
```
|
126
|
+
|
127
|
+
In additions to `keys`, JsonParse wraps the convenience methods `length` & `count` to return the number of keys in a document element, or the number of items in an array element. The `rootkey` method returns the name of the singular document root element (if present).
|
128
|
+
|
129
|
+
The `hash` and `json_string` methods return a hash and json representation of the object respectively.
|
130
|
+
These methods can be useful to extract the relevant portion of a subbracnch and generate new json representation of just that branch. The `source` method returns the string or hash supplied when the object was initialized.
|
131
|
+
|
132
|
+
Because some of these methods may also represent keys in a json string (viz. `count`, `keys`, `source`), if a matching key exists as an attribute name, the attribute value will be returned instead of the method's value.
|
133
|
+
Appending a '!' to the method name (viz `count!`, `keys!`, `hash!`) will return the intended method result irrespective of the presence of an identical key in the json document.
|
134
|
+
|
135
|
+
|
136
|
+
## Development
|
137
|
+
|
138
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
139
|
+
|
140
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
141
|
+
|
142
|
+
## Contributing
|
143
|
+
|
144
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/JsonParse.
|
145
|
+
|
146
|
+
## License
|
147
|
+
|
148
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
149
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "json_parse"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/json_parse.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "json_parse/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "json_parse"
|
8
|
+
spec.version = JsonParse::VERSION
|
9
|
+
spec.authors = ["Grapevine Gizmos LLC"]
|
10
|
+
spec.email = ["grapevinegizmos@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Easy to use JsonParser for Rails"
|
13
|
+
spec.description = "Load JsonParse with a json string or hash, and then refer to elements with dot notation."
|
14
|
+
spec.homepage = "https://github.com/grapevinegizmos/JsonParse"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = "https://github.com/grapevinegizmos/JsonParse.git"
|
24
|
+
spec.metadata["changelog_uri"] = "https://github.com/grapevinegizmos/JsonParse.git"
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
42
|
+
spec.add_dependency "activesupport"
|
43
|
+
end
|
data/lib/json_parse.rb
ADDED
@@ -0,0 +1,310 @@
|
|
1
|
+
require "json_parse/version"
|
2
|
+
require "active_support/all"
|
3
|
+
|
4
|
+
class JsonParse
|
5
|
+
include Enumerable
|
6
|
+
@jsonstring = nil
|
7
|
+
@rootkey = nil
|
8
|
+
@throwmissing = true #determines whether the parser throws an exception or returns nil if a key is not found. Set to false after debugging!
|
9
|
+
@source = nil
|
10
|
+
@h = nil
|
11
|
+
|
12
|
+
|
13
|
+
def method_missing(m, *args)
|
14
|
+
|
15
|
+
#looks for top level keys and returns the value
|
16
|
+
a = args
|
17
|
+
if !(self.keys!.include?(m))
|
18
|
+
if @throwmissing
|
19
|
+
super
|
20
|
+
else
|
21
|
+
return nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
self._value(m)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [JsonParse]
|
28
|
+
# @param [Object] symbol
|
29
|
+
def get_by_key(symbol)
|
30
|
+
method_missing(symbol.to_sym, nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
def each(&block)
|
34
|
+
if self._check_for_keyword("each")
|
35
|
+
return _value(:each)
|
36
|
+
end
|
37
|
+
_each(&block)
|
38
|
+
end
|
39
|
+
|
40
|
+
def each!(&block)
|
41
|
+
_each(&block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def length
|
45
|
+
if self._check_for_keyword("length")
|
46
|
+
return _value(:length)
|
47
|
+
end
|
48
|
+
@h.length
|
49
|
+
end
|
50
|
+
|
51
|
+
def length!
|
52
|
+
return _length
|
53
|
+
end
|
54
|
+
|
55
|
+
def keys!
|
56
|
+
_keys
|
57
|
+
end
|
58
|
+
|
59
|
+
def keys
|
60
|
+
if self._check_for_keyword("keys")
|
61
|
+
return self._value(:keys)
|
62
|
+
end
|
63
|
+
_keys
|
64
|
+
end
|
65
|
+
|
66
|
+
def source!
|
67
|
+
_source
|
68
|
+
end
|
69
|
+
def source
|
70
|
+
if self._check_for_keyword("keys")
|
71
|
+
return self._value(:keys)
|
72
|
+
end
|
73
|
+
_source
|
74
|
+
end
|
75
|
+
|
76
|
+
def add(key, value)
|
77
|
+
@h[key.to_sym] = value
|
78
|
+
end
|
79
|
+
|
80
|
+
def initialize(val, rootkey = nil, throwmissing = false) #, jsonstring = '{"root": {"name":"Josh Kramer", "age":60, "children": [{"name": "jack", "age": 21},{"name": "sally","age":14}], "pets": ["ruby","buddy","fluffy"], "address": {"city" : "Bainbridge Island", "state" : "WA"}}}')
|
81
|
+
@source = val
|
82
|
+
if val.nil?
|
83
|
+
@h = {}
|
84
|
+
@rootkey = nil
|
85
|
+
return
|
86
|
+
end
|
87
|
+
@throwmissing = throwmissing
|
88
|
+
@rootkey = rootkey
|
89
|
+
if val.is_a?(JsonParse)
|
90
|
+
@h = val.hash
|
91
|
+
@rootkey = val.rootkey
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
if val.is_a?(Array)
|
96
|
+
retval = []
|
97
|
+
@rootkey = rootkey
|
98
|
+
val.each do |item|
|
99
|
+
if item.is_a?(Hash) || item.is_a?(Array)
|
100
|
+
retval.push(JsonParse.new(item))
|
101
|
+
else
|
102
|
+
retval.push(item);
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
@h = retval
|
107
|
+
end
|
108
|
+
if val.is_a?(Hash)
|
109
|
+
@h = val.symbolize_keys!
|
110
|
+
_discard_root #
|
111
|
+
end
|
112
|
+
if val.is_a?(String) #if it sa string, it should be a json string
|
113
|
+
_load(val)
|
114
|
+
@jsonstring = val
|
115
|
+
end
|
116
|
+
|
117
|
+
if @h.nil?
|
118
|
+
raise "an unusable object was provided to the initializer"
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
def rootkey
|
125
|
+
if self._check_for_keyword("rootkey")
|
126
|
+
return _value(:rootkey)
|
127
|
+
end
|
128
|
+
_rootkey
|
129
|
+
end
|
130
|
+
def rootkey!
|
131
|
+
_rootkey
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
def json_string
|
136
|
+
if self._check_for_keyword("json_string")
|
137
|
+
return _value(:json_string)
|
138
|
+
end
|
139
|
+
@h.to_json
|
140
|
+
end
|
141
|
+
|
142
|
+
def json_string!
|
143
|
+
_json_string
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
def contains_key(key = nil)
|
149
|
+
if key == nil && self._check_for_keyword("contains_key")
|
150
|
+
return _value(:contains_key)
|
151
|
+
end
|
152
|
+
@h.keys.include?(key)
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
def [](index)
|
159
|
+
if @h.is_a?(Array)
|
160
|
+
return @h[index]
|
161
|
+
end
|
162
|
+
nil
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
def count!
|
167
|
+
_count
|
168
|
+
end
|
169
|
+
def count
|
170
|
+
if self._check_for_keyword("hash")
|
171
|
+
return _value(:hash)
|
172
|
+
end
|
173
|
+
_count
|
174
|
+
end
|
175
|
+
|
176
|
+
def hash!
|
177
|
+
_hash
|
178
|
+
end
|
179
|
+
def hash
|
180
|
+
if self._check_for_keyword("hash")
|
181
|
+
return _value(:hash)
|
182
|
+
end
|
183
|
+
_hash
|
184
|
+
end
|
185
|
+
|
186
|
+
def _symbolize(h)
|
187
|
+
if self._check_for_keyword("_symbolize")
|
188
|
+
return _value("_symbolize")
|
189
|
+
end
|
190
|
+
|
191
|
+
h.symbolize_keys!
|
192
|
+
h.each do |k, v|
|
193
|
+
if v.is_a?(Hash)
|
194
|
+
self._symbolize(v)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
def _check_for_keyword(keyword)
|
203
|
+
mykeys = []
|
204
|
+
if @h.is_a?(Hash)
|
205
|
+
mykeys = @h.keys
|
206
|
+
end
|
207
|
+
mykeys.include?(keyword.to_sym)
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
# @return (Hash)
|
212
|
+
# @param [String] jsonString
|
213
|
+
def _parse_json(jsonString)
|
214
|
+
v = ActiveSupport::JSON.decode(jsonString)
|
215
|
+
end
|
216
|
+
|
217
|
+
def _value(key)
|
218
|
+
if @h.is_a?(Array)
|
219
|
+
return @h[key]
|
220
|
+
end
|
221
|
+
if @h.key?(key.to_sym)
|
222
|
+
if @h[key].is_a?(Hash) || @h[key].is_a?(Array)
|
223
|
+
return JsonParse.new(@h[key], key)
|
224
|
+
else
|
225
|
+
retval = @h[key]
|
226
|
+
|
227
|
+
return retval
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
|
233
|
+
private
|
234
|
+
|
235
|
+
def _length
|
236
|
+
return @h.length
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
def _keys
|
241
|
+
if @h.is_a?(Hash)
|
242
|
+
return @h.keys
|
243
|
+
end
|
244
|
+
if @h.is_a?(Array)
|
245
|
+
return "[]"
|
246
|
+
else
|
247
|
+
return nil if @h.nil?
|
248
|
+
raise "expecting hash or array, got #{@h.class.name}"
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
def _each(&block)
|
254
|
+
if block_given?
|
255
|
+
@h.each(&block)
|
256
|
+
else
|
257
|
+
return nil
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def _source
|
262
|
+
@source
|
263
|
+
end
|
264
|
+
def _hash
|
265
|
+
@h
|
266
|
+
end
|
267
|
+
def _count
|
268
|
+
@h.length
|
269
|
+
end
|
270
|
+
|
271
|
+
def _load(jsonstring)
|
272
|
+
if jsonstring.nil? && self._check_for_keyword("load")
|
273
|
+
return _value(:load)
|
274
|
+
end
|
275
|
+
# var [Hash] @h
|
276
|
+
@h = _parse_json(jsonstring)
|
277
|
+
self._symbolize(@h)
|
278
|
+
self._discard_root
|
279
|
+
end
|
280
|
+
|
281
|
+
def _json_string
|
282
|
+
@h.to_json
|
283
|
+
end
|
284
|
+
def _rootkey
|
285
|
+
@rootkey.to_s
|
286
|
+
end
|
287
|
+
|
288
|
+
def _discard_root()
|
289
|
+
|
290
|
+
h2 = {}
|
291
|
+
|
292
|
+
if @h.length == 1
|
293
|
+
@rootkey = @h.keys[0]
|
294
|
+
|
295
|
+
if @h.values[0].is_a?(Hash)
|
296
|
+
@h.values[0].each do |k, v|
|
297
|
+
h2[k] = v
|
298
|
+
end
|
299
|
+
end
|
300
|
+
end
|
301
|
+
if h2.length > 0
|
302
|
+
@h = h2
|
303
|
+
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
end
|
310
|
+
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_parse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Grapevine Gizmos LLC
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Load JsonParse with a json string or hash, and then refer to elements
|
70
|
+
with dot notation.
|
71
|
+
email:
|
72
|
+
- grapevinegizmos@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- json_parse.gemspec
|
88
|
+
- lib/json_parse.rb
|
89
|
+
- lib/json_parse/version.rb
|
90
|
+
homepage: https://github.com/grapevinegizmos/JsonParse
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata:
|
94
|
+
allowed_push_host: https://rubygems.org
|
95
|
+
homepage_uri: https://github.com/grapevinegizmos/JsonParse
|
96
|
+
source_code_uri: https://github.com/grapevinegizmos/JsonParse.git
|
97
|
+
changelog_uri: https://github.com/grapevinegizmos/JsonParse.git
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.7.7
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Easy to use JsonParser for Rails
|
118
|
+
test_files: []
|