normalize-json 0.0.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/bin/normalize-json +107 -0
- data/lib/normalize_json.rb +32 -0
- metadata +47 -0
data/bin/normalize-json
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright 2012 Victor Penso, Dennis Klein
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'getoptlong'
|
20
|
+
require 'JSON'
|
21
|
+
require 'normalize_json'
|
22
|
+
|
23
|
+
exec_name = File.split(__FILE__)[-1]
|
24
|
+
HELP = <<EOF
|
25
|
+
Synopsis
|
26
|
+
========
|
27
|
+
|
28
|
+
#{exec_name}: Normalize redundant relational data by given keys. Input JSON Array, Output JSON Object.
|
29
|
+
|
30
|
+
Usage
|
31
|
+
-----
|
32
|
+
|
33
|
+
#{exec_name} KEY1 [KEY2 KEY3 ...] [OPTIONS]
|
34
|
+
|
35
|
+
KEY1 [KEY2 KEY3 ...]:
|
36
|
+
|
37
|
+
|
38
|
+
Options
|
39
|
+
-------
|
40
|
+
|
41
|
+
--file, -f FILE
|
42
|
+
Read input from FILE instead of STDIN.
|
43
|
+
--leaf-not-unique, -l
|
44
|
+
Leafs get encapsulated in a JSON Array.
|
45
|
+
--help, -h
|
46
|
+
Show this help information.
|
47
|
+
--debug, -d
|
48
|
+
Show stacktraces in case of errors.
|
49
|
+
EOF
|
50
|
+
|
51
|
+
begin
|
52
|
+
|
53
|
+
options = Hash.new
|
54
|
+
options[:debug] = false
|
55
|
+
options[:file] = false
|
56
|
+
options[:leaf_not_unique] = false
|
57
|
+
|
58
|
+
GetoptLong.new(
|
59
|
+
['--file','-f',GetoptLong::REQUIRED_ARGUMENT],
|
60
|
+
['--leaf-not-unique','-l',GetoptLong::NO_ARGUMENT],
|
61
|
+
['--debug','-d',GetoptLong::NO_ARGUMENT],
|
62
|
+
['--help','-h',GetoptLong::NO_ARGUMENT]
|
63
|
+
).each do |opt,arg|
|
64
|
+
case opt
|
65
|
+
when '--file'
|
66
|
+
options[:file] = arg
|
67
|
+
when '--leaf-not-unique'
|
68
|
+
options[:leaf_not_unique] = true
|
69
|
+
when '--debug'
|
70
|
+
options[:debug] = true
|
71
|
+
when '--help'
|
72
|
+
$stdout.puts HELP
|
73
|
+
exit 0
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
keys = ARGV
|
78
|
+
(keys.size > 0) || raise('You need to provide at least one key!')
|
79
|
+
|
80
|
+
input = String.new
|
81
|
+
if options[:file] then
|
82
|
+
input = IO.read(options[:file])
|
83
|
+
else
|
84
|
+
while line = STDIN.gets do
|
85
|
+
input << line
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
input = JSON.parse(input)
|
90
|
+
output = JSON.generate(input.normalize(keys, {:encapsulate_leafs_in_array => options[:leaf_not_unique]}))
|
91
|
+
|
92
|
+
puts output
|
93
|
+
|
94
|
+
rescue => exc
|
95
|
+
$stderr.puts "ERROR: #{exc.message}"
|
96
|
+
$stderr.puts " use -h for detailed instructions"
|
97
|
+
if options[:debug]
|
98
|
+
$stderr.puts '-- Stack Trace --'
|
99
|
+
$stderr.puts exc.backtrace
|
100
|
+
else
|
101
|
+
$stderr.puts 'You may want run this in debug mode with \'-d\''
|
102
|
+
end
|
103
|
+
exit 1
|
104
|
+
end
|
105
|
+
|
106
|
+
exit 0
|
107
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
Array.send(:define_method, 'normalize') do |path, options={:encapsulate_leafs_in_array => false}|
|
2
|
+
result = Hash.new
|
3
|
+
each do |element|
|
4
|
+
cur = result
|
5
|
+
path.size.times do |i|
|
6
|
+
if element[path[i]].nil? then
|
7
|
+
cur = nil
|
8
|
+
break
|
9
|
+
end
|
10
|
+
if (((path.size-1) == i) && options[:encapsulate_leafs_in_array]) then
|
11
|
+
cur[element[path[i]]] = Array.new unless cur.has_key? element[path[i]]
|
12
|
+
else
|
13
|
+
cur[element[path[i]]] = Hash.new unless cur.has_key? element[path[i]]
|
14
|
+
end
|
15
|
+
cur = cur[element[path[i]]]
|
16
|
+
end
|
17
|
+
unless cur.nil? then
|
18
|
+
if options[:encapsulate_leafs_in_array] then
|
19
|
+
leaf = Hash.new
|
20
|
+
(element.keys - path).each do |key|
|
21
|
+
leaf[key] = element[key]
|
22
|
+
end
|
23
|
+
cur << leaf
|
24
|
+
else
|
25
|
+
(element.keys - path).each do |key|
|
26
|
+
cur[key] = element[key]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
result
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: normalize-json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dennis Klein
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Normalizes redundand relational data by given keys
|
15
|
+
email: d.klein@gsi.de
|
16
|
+
executables:
|
17
|
+
- normalize-json
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/normalize_json.rb
|
22
|
+
- bin/normalize-json
|
23
|
+
homepage: http://github.com/Reverand221/normalize-json
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.24
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Normalizes redundand relational data by given keys
|
47
|
+
test_files: []
|