leenookx-json-mangler 0.1.2 → 0.2.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.
- data/README.rdoc +4 -1
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/features/test_comparisons/2level_extraction.json +1 -1
- data/json-mangler.gemspec +5 -6
- data/lib/json-mangler/json_extractor.rb +52 -19
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -10,6 +10,7 @@ format.
|
|
10
10
|
== FEATURES/PROBLEMS:
|
11
11
|
|
12
12
|
* compact Compacts all duplicated branches.
|
13
|
+
* extract Allows searching through the JSON data.
|
13
14
|
* prune Allows removal of branches.
|
14
15
|
|
15
16
|
== SYNOPSIS:
|
@@ -18,11 +19,13 @@ format.
|
|
18
19
|
|
19
20
|
== REQUIREMENTS:
|
20
21
|
|
22
|
+
* cucumber (only for the unit tests)
|
21
23
|
* json
|
22
24
|
|
23
25
|
== INSTALL:
|
24
26
|
|
25
|
-
* sudo gem
|
27
|
+
* [sudo] gem sources -a http://gems.github.com
|
28
|
+
* [sudo] gem install leenookx-json-mangler
|
26
29
|
|
27
30
|
== LICENSE:
|
28
31
|
|
data/Rakefile
CHANGED
@@ -17,7 +17,6 @@ begin
|
|
17
17
|
gemspec.description = "JSON data format mangling tools."
|
18
18
|
gemspec.email = "lnookx@googlemail.com"
|
19
19
|
gemspec.homepage = "http://github.com/leenookx/json-mangler"
|
20
|
-
gemspec.description = "TODO"
|
21
20
|
gemspec.authors = ["lee nookx"]
|
22
21
|
end
|
23
22
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1
|
1
|
+
0.2.1
|
@@ -1 +1 @@
|
|
1
|
-
{ "results": {"title": "A test document", "author": "My Name", "published": "1999", "keywords": ["rubbish", "nonsense"]} }
|
1
|
+
{ "results": {"doc": {"title": "A test document", "author": "My Name", "published": "1999", "keywords": ["rubbish", "nonsense"]} } }
|
data/json-mangler.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{json-mangler}
|
8
|
-
s.version = "0.1
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["lee nookx"]
|
12
|
-
s.date = %q{2009-08-
|
13
|
-
s.description = %q{
|
12
|
+
s.date = %q{2009-08-31}
|
13
|
+
s.description = %q{JSON data format mangling tools.}
|
14
14
|
s.email = %q{lnookx@googlemail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
@@ -51,11 +51,10 @@ Gem::Specification.new do |s|
|
|
51
51
|
"lib/json-mangler/json_mangler.rb",
|
52
52
|
"test/test_json_mangler.rb"
|
53
53
|
]
|
54
|
-
s.has_rdoc = true
|
55
54
|
s.homepage = %q{http://github.com/leenookx/json-mangler}
|
56
55
|
s.rdoc_options = ["--charset=UTF-8"]
|
57
56
|
s.require_paths = ["lib"]
|
58
|
-
s.rubygems_version = %q{1.3.
|
57
|
+
s.rubygems_version = %q{1.3.4}
|
59
58
|
s.summary = %q{JSON data format mangling tools.}
|
60
59
|
s.test_files = [
|
61
60
|
"test/test_json_mangler.rb"
|
@@ -63,7 +62,7 @@ Gem::Specification.new do |s|
|
|
63
62
|
|
64
63
|
if s.respond_to? :specification_version then
|
65
64
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
66
|
-
s.specification_version =
|
65
|
+
s.specification_version = 3
|
67
66
|
|
68
67
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
69
68
|
else
|
@@ -59,43 +59,68 @@ class JSONExtractor
|
|
59
59
|
def parse_object
|
60
60
|
if @input.scan(/\{\s*/)
|
61
61
|
output = ""
|
62
|
+
start_pos = @input.pos
|
62
63
|
more_pairs = false
|
63
64
|
capture = false
|
64
65
|
while key = parse_string
|
65
66
|
@input.scan(/\s*:\s*/) or error("Expecting object separator")
|
66
67
|
|
68
|
+
# If we're in object matching mode and we've found an object
|
69
|
+
# that matches the one we're looking for...
|
67
70
|
if @mode == 1 and key == @searchval
|
68
71
|
capture = true
|
69
72
|
end
|
70
73
|
|
71
74
|
res = parse_value
|
72
75
|
|
76
|
+
# If we're in data matching mode and the data associated
|
77
|
+
# will this object matches what we are looking for...
|
73
78
|
if @mode == 2 and res == @searchval
|
74
|
-
@
|
75
|
-
|
76
|
-
|
79
|
+
if @capture_depth > 0
|
80
|
+
# Set the 'upward' depth to be captured.
|
81
|
+
@current_depth = @capture_depth
|
77
82
|
|
78
|
-
|
79
|
-
|
83
|
+
# Force the capturing of this object.
|
84
|
+
@force_capture = true
|
85
|
+
else
|
86
|
+
capture = true
|
87
|
+
end
|
80
88
|
end
|
81
89
|
|
82
|
-
if
|
83
|
-
|
84
|
-
|
85
|
-
|
90
|
+
if !@force_capture and capture
|
91
|
+
@output << key << ": " << res.to_s
|
92
|
+
|
93
|
+
# We've stopped capturing locally...
|
86
94
|
capture = false
|
87
|
-
if @current_depth == 0
|
88
|
-
@output << output
|
89
|
-
@force_capture = false
|
90
|
-
else
|
91
|
-
@current_depth = @current_depth - 1
|
92
|
-
end
|
93
95
|
end
|
94
96
|
|
95
|
-
|
97
|
+
# Handle special case of array match
|
98
|
+
if @force_capture
|
99
|
+
if @current_depth == -1
|
100
|
+
@output << key << ": " << res.to_s
|
101
|
+
@force_capture = false
|
102
|
+
@current_depth = 0
|
103
|
+
end
|
104
|
+
end
|
96
105
|
|
97
|
-
|
106
|
+
more_pairs = @input.scan(/\s*,\s*/)
|
107
|
+
if more_pairs
|
98
108
|
output << ", "
|
109
|
+
else
|
110
|
+
if @force_capture and @current_depth == 0
|
111
|
+
last_pos = @input.pos
|
112
|
+
|
113
|
+
cur_pos = start_pos
|
114
|
+
@input.pos = cur_pos
|
115
|
+
while (cur_pos < last_pos)
|
116
|
+
@output << @input.getch
|
117
|
+
cur_pos = cur_pos + 1
|
118
|
+
end
|
119
|
+
|
120
|
+
@force_capture = false
|
121
|
+
else
|
122
|
+
@current_depth = @current_depth - 1
|
123
|
+
end
|
99
124
|
end
|
100
125
|
end
|
101
126
|
error("Missing object pair") if more_pairs
|
@@ -111,6 +136,10 @@ class JSONExtractor
|
|
111
136
|
more_values = false
|
112
137
|
while contents = parse_value rescue nil
|
113
138
|
array << contents
|
139
|
+
if contents == @searchval
|
140
|
+
@force_capture = true
|
141
|
+
@current_depth = -1
|
142
|
+
end
|
114
143
|
more_values = @input.scan(/\s*,\s*/) or break
|
115
144
|
array << ", "
|
116
145
|
end
|
@@ -130,9 +159,13 @@ class JSONExtractor
|
|
130
159
|
string << contents
|
131
160
|
end
|
132
161
|
@input.scan(/"/) or error("Unclosed string")
|
133
|
-
string
|
134
|
-
|
162
|
+
string << '"'
|
163
|
+
|
164
|
+
# If we're in data matching mode and this matches the
|
165
|
+
# value that we are searching for...
|
166
|
+
if @mode == 2 and string == @searchval and @capture_depth > 0
|
135
167
|
@force_capture = true
|
168
|
+
@current_depth = @capture_depth
|
136
169
|
end
|
137
170
|
string
|
138
171
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: leenookx-json-mangler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lee nookx
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: JSON data format mangling tools.
|
17
17
|
email: lnookx@googlemail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -56,7 +56,7 @@ files:
|
|
56
56
|
- lib/json-mangler/json_extractor.rb
|
57
57
|
- lib/json-mangler/json_mangler.rb
|
58
58
|
- test/test_json_mangler.rb
|
59
|
-
has_rdoc:
|
59
|
+
has_rdoc: false
|
60
60
|
homepage: http://github.com/leenookx/json-mangler
|
61
61
|
licenses:
|
62
62
|
post_install_message:
|
@@ -81,7 +81,7 @@ requirements: []
|
|
81
81
|
rubyforge_project:
|
82
82
|
rubygems_version: 1.3.5
|
83
83
|
signing_key:
|
84
|
-
specification_version:
|
84
|
+
specification_version: 3
|
85
85
|
summary: JSON data format mangling tools.
|
86
86
|
test_files:
|
87
87
|
- test/test_json_mangler.rb
|