csv2json 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +47 -42
- data/Rakefile +1 -2
- data/bin/csv2json +23 -4
- data/bin/json2csv +73 -0
- data/csv2json.gemspec +42 -45
- data/lib/csv2json-version.rb +1 -1
- data/lib/csv2json.rb +18 -6
- data/lib/json2csv.rb +36 -0
- data/test/helper.rb +1 -0
- data/test/json2csv-fixtures/photos.json +122 -0
- data/test/json2csv-fixtures/photos_comma.csv +13 -0
- data/test/json2csv-fixtures/photos_pipe.csv +13 -0
- data/test/json2csv-fixtures/photos_semicolon.csv +13 -0
- data/test/test_csv2json.rb +8 -3
- data/test/test_json2csv.rb +32 -0
- metadata +56 -74
- data/.gitignore +0 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4bd184909e84a50e1c7b8194c9745304cc9452b2
|
4
|
+
data.tar.gz: ae813c982d6423683b30966835e6f99cca001576
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aec64661bf440449ee161b6d1bb526cb5153e0749154a98507219881dc4b7e20bea9da9df5448590c9dfc714d54fec3ad895608aefa685d50188dacc00a06b7d
|
7
|
+
data.tar.gz: e06d877886ecefca9d22f3791ba959ad43cfe2b227243b46826cb51cc45ccbdf9101524a565dcd4349e0d1525a6e49828ea7da1b59ca05c2a03db39e7a878edf
|
data/README.md
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
# csv2json
|
2
2
|
|
3
|
-
Clients were sending me XLS files
|
4
|
-
Tried to google for solution and surprisingly enough nothing solid existed.
|
3
|
+
Clients were sending me XLS files but my webs consume JSONs. So I needed to convert them to JSON easily from command-line...
|
5
4
|
|
6
5
|
## Solution
|
7
6
|
|
8
7
|
- export XLS as a CSV file (I use OpenOffice.org for this)
|
9
|
-
- run `csv2json file.csv > file.json`
|
8
|
+
- run `csv2json file.csv > file.json` (or `json2csv file.json > file.csv`)
|
10
9
|
- there is no step 3
|
11
10
|
|
12
11
|
### Sample
|
13
12
|
|
14
|
-
note: make sure your XLS table has the first row with column names
|
13
|
+
note: make sure your XLS table has the first row with column names, or use the -H option to provide some
|
15
14
|
|
16
15
|
This CSV file:
|
17
16
|
|
@@ -20,44 +19,62 @@ This CSV file:
|
|
20
19
|
thumbnails/,images/,paris_02.jpg,262,350,paris_02.jpg,75,56
|
21
20
|
|
22
21
|
gets turned into this JSON:
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
22
|
+
```json
|
23
|
+
[
|
24
|
+
{
|
25
|
+
"thumbwidth": 75,
|
26
|
+
"imgsrc": "paris_01.jpg",
|
27
|
+
"thumbsrc": "paris_01.jpg",
|
28
|
+
"height": 262,
|
29
|
+
"imgpath": "images/",
|
30
|
+
"thumbheight": 56,
|
31
|
+
"thumbpath": "thumbnails/",
|
32
|
+
"width": 350
|
33
|
+
},
|
34
|
+
{
|
35
|
+
"thumbwidth": 75,
|
36
|
+
"imgsrc": "paris_02.jpg",
|
37
|
+
"thumbsrc": "paris_02.jpg",
|
38
|
+
"height": 350,
|
39
|
+
"imgpath": "images/",
|
40
|
+
"thumbheight": 56,
|
41
|
+
"thumbpath": "thumbnails/",
|
42
|
+
"width": 262
|
43
|
+
}
|
44
|
+
]
|
45
|
+
```
|
46
46
|
|
47
47
|
### Installation
|
48
48
|
|
49
|
-
`sudo gem install csv2json
|
49
|
+
`sudo gem install csv2json`
|
50
50
|
|
51
51
|
### Usage
|
52
52
|
|
53
|
-
|
53
|
+
Usage: csv2json [INPUT] [OPTIONS]
|
54
|
+
|
55
|
+
Specific options:
|
56
|
+
-s, --separator SEP Set separator character surrounded by single quotes (default is ',')
|
57
|
+
-o, --output FILE Write output to a file
|
58
|
+
-p, --pretty Pretty-format JSON output
|
59
|
+
-k, --skip-headers-from-file Ignore the headers (first line) in the file; use with --headers to set your own
|
60
|
+
-H, --headers HEADERS Supply list of headers, where no headers exist in the file, or where you're using -k to ignore them
|
61
|
+
-h, --help Show this message
|
62
|
+
-v, --version Show version
|
63
|
+
|
64
|
+
We also provide `json2csv`, which converts in the opposite direction.
|
65
|
+
|
66
|
+
Usage: json2csv [INPUT] [OPTIONS]
|
54
67
|
|
55
68
|
Specific options:
|
56
69
|
-s, --separator SEP Set separator character surrounded by single quotes (default is ',')
|
57
70
|
-o, --output FILE Write output to a file
|
71
|
+
-H, --headers HEADERS Supply sorted list of headers, by which to order the columns in the CSV. These must match the key names in the JSON.
|
58
72
|
-h, --help Show this message
|
59
73
|
-v, --version Show version
|
60
74
|
|
75
|
+
|
76
|
+
Note: JSON field order is explicitly unsorted, so if you want predictable field order for CSV output, use the -H option.
|
77
|
+
|
61
78
|
### Alternative usage
|
62
79
|
|
63
80
|
common usage is `csv2json file.csv > file.json`
|
@@ -91,16 +108,4 @@ or in-memory
|
|
91
108
|
|
92
109
|
## Authors
|
93
110
|
|
94
|
-
|
95
|
-
* **Rafael Souza** ([http://github.com/rafaels](http://github.com/rafaels))
|
96
|
-
|
97
|
-
---
|
98
|
-
|
99
|
-
## Want to contribute?
|
100
|
-
|
101
|
-
* Fork the project.
|
102
|
-
* Make your feature addition or bug fix.
|
103
|
-
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
104
|
-
* Commit, do not mess with rakefile, version, or history.
|
105
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
106
|
-
* Send me a pull request. Bonus points for topic branches.
|
111
|
+
[https://github.com/darwin/csv2json/graphs/contributors](https://github.com/darwin/csv2json/graphs/contributors)
|
data/Rakefile
CHANGED
@@ -13,7 +13,6 @@ begin
|
|
13
13
|
gem.homepage = "http://github.com/darwin/csv2json"
|
14
14
|
gem.authors = ["Antonin Hildebrand", "Rafael Souza"]
|
15
15
|
gem.add_dependency "json"
|
16
|
-
gem.add_dependency "fastercsv"
|
17
16
|
gem.add_development_dependency "shoulda", ">= 0"
|
18
17
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
18
|
end
|
@@ -46,7 +45,7 @@ task :test => :check_dependencies
|
|
46
45
|
|
47
46
|
task :default => :test
|
48
47
|
|
49
|
-
require '
|
48
|
+
require 'rdoc/task'
|
50
49
|
Rake::RDocTask.new do |rdoc|
|
51
50
|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
52
51
|
|
data/bin/csv2json
CHANGED
@@ -6,7 +6,7 @@ require 'ostruct'
|
|
6
6
|
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'csv2json.rb') # this form is important for local development
|
7
7
|
|
8
8
|
module CSV2JSONRunner
|
9
|
-
|
9
|
+
|
10
10
|
# command-line parsing
|
11
11
|
COMMAND = File.basename($0)
|
12
12
|
USAGE = "Usage: #{COMMAND} [INPUT] [OPTIONS]"
|
@@ -14,6 +14,9 @@ module CSV2JSONRunner
|
|
14
14
|
options = OpenStruct.new
|
15
15
|
options.output = "-"
|
16
16
|
options.separator = ","
|
17
|
+
options.pretty = false
|
18
|
+
options.headers = nil
|
19
|
+
options.skipFirstRow = false
|
17
20
|
|
18
21
|
opts = OptionParser.new do |o|
|
19
22
|
o.banner = USAGE
|
@@ -28,6 +31,22 @@ module CSV2JSONRunner
|
|
28
31
|
options.output = fn
|
29
32
|
end
|
30
33
|
|
34
|
+
o.on("-p", "--pretty", "Pretty-format JSON output") do
|
35
|
+
options.pretty = true
|
36
|
+
end
|
37
|
+
|
38
|
+
o.on("-k", "--skip-headers-from-file", "Ignore the headers (first line) in the file; use with --headers to set your own") do |headers|
|
39
|
+
if headers then
|
40
|
+
options.skipFirstRow = true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
o.on("-H", "--headers HEADERS", "Supply list of headers, where no headers exist in the file, or where you're using -k to ignore them") do |headers|
|
45
|
+
if headers then
|
46
|
+
options.headers = headers.split(",")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
31
50
|
o.on_tail("-h", "--help", "Show this message") do
|
32
51
|
puts o
|
33
52
|
exit
|
@@ -42,7 +61,7 @@ module CSV2JSONRunner
|
|
42
61
|
begin
|
43
62
|
opts.parse!(ARGV)
|
44
63
|
rescue
|
45
|
-
|
64
|
+
raise "Unable to parse options: #{$!}"
|
46
65
|
end
|
47
66
|
|
48
67
|
# initialize output handle
|
@@ -55,11 +74,11 @@ module CSV2JSONRunner
|
|
55
74
|
if ARGV.size > 0
|
56
75
|
IN = File.open(ARGV[0], "r")
|
57
76
|
else
|
58
|
-
IN = StringIO.new($stdin.read) # cannot be just $stdin.clone because
|
77
|
+
IN = StringIO.new($stdin.read) # cannot be just $stdin.clone because CSV is seeking in file :-(
|
59
78
|
end
|
60
79
|
|
61
80
|
# run the command
|
62
|
-
CSV2JSON.parse(IN, OUT,
|
81
|
+
CSV2JSON.parse(IN, OUT, options.headers, {:col_sep => options.separator}, {:pretty => options.pretty, :skipFirstRow => options.skipFirstRow})
|
63
82
|
|
64
83
|
# leave in peace
|
65
84
|
OUT.flush
|
data/bin/json2csv
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require 'optparse'
|
5
|
+
require 'ostruct'
|
6
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'json2csv.rb') # this form is important for local development
|
7
|
+
|
8
|
+
module JSON2CSVRunner
|
9
|
+
|
10
|
+
# command-line parsing
|
11
|
+
COMMAND = File.basename($0)
|
12
|
+
USAGE = "Usage: #{COMMAND} [INPUT] [OPTIONS]"
|
13
|
+
|
14
|
+
options = OpenStruct.new
|
15
|
+
options.output = "-"
|
16
|
+
options.separator = ","
|
17
|
+
options.headers = nil
|
18
|
+
|
19
|
+
opts = OptionParser.new do |o|
|
20
|
+
o.banner = USAGE
|
21
|
+
o.separator ""
|
22
|
+
o.separator "Specific options:"
|
23
|
+
|
24
|
+
o.on("-s", "--separator SEP", "Set separator character surrounded by single quotes (default is ',')") do |sep|
|
25
|
+
options.separator = sep
|
26
|
+
end
|
27
|
+
|
28
|
+
o.on("-o", "--output FILE", "Write output to a file") do |fn|
|
29
|
+
options.output = fn
|
30
|
+
end
|
31
|
+
|
32
|
+
o.on("-H", "--headers HEADERS", "Supply sorted list of headers, by which to order the columns in the CSV. These must match the key names in the JSON.") do |headers|
|
33
|
+
if headers then
|
34
|
+
options.headers = headers.split(",")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
o.on_tail("-h", "--help", "Show this message") do
|
39
|
+
puts o
|
40
|
+
exit
|
41
|
+
end
|
42
|
+
|
43
|
+
o.on_tail("-v", "--version", "Show version") do
|
44
|
+
puts CSV2JSON::VERSION
|
45
|
+
exit
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
begin
|
50
|
+
opts.parse!(ARGV)
|
51
|
+
rescue
|
52
|
+
raise "Unable to parse options: #{$!}"
|
53
|
+
end
|
54
|
+
|
55
|
+
# initialize output handle
|
56
|
+
if options.output == "-"
|
57
|
+
OUT = $stdout.clone
|
58
|
+
else
|
59
|
+
OUT = File.open(options.output, "w")
|
60
|
+
end
|
61
|
+
|
62
|
+
if ARGV.size > 0
|
63
|
+
IN = File.read(ARGV[0])
|
64
|
+
else
|
65
|
+
IN = STDIN.read
|
66
|
+
end
|
67
|
+
|
68
|
+
# run the command
|
69
|
+
JSON2CSV.parse(IN, OUT, options.headers, {:col_sep => options.separator})
|
70
|
+
|
71
|
+
# leave in peace
|
72
|
+
OUT.flush
|
73
|
+
end
|
data/csv2json.gemspec
CHANGED
@@ -1,74 +1,71 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: csv2json 0.3.0 ruby lib
|
5
6
|
|
6
7
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
8
|
+
s.name = "csv2json"
|
9
|
+
s.version = "0.3.0"
|
9
10
|
|
10
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
12
|
s.authors = ["Antonin Hildebrand", "Rafael Souza"]
|
12
|
-
s.date =
|
13
|
-
s.
|
14
|
-
s.
|
15
|
-
s.
|
16
|
-
s.executables = ["csv2json"]
|
13
|
+
s.date = "2014-03-19"
|
14
|
+
s.description = "handy for converting xls files to json"
|
15
|
+
s.email = "antonin@hildebrand.cz"
|
16
|
+
s.executables = ["csv2json", "json2csv"]
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"LICENSE",
|
19
|
-
|
19
|
+
"README.md"
|
20
20
|
]
|
21
21
|
s.files = [
|
22
22
|
".document",
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
"test/test_csv2json.rb"
|
45
|
-
]
|
46
|
-
s.homepage = %q{http://github.com/darwin/csv2json}
|
47
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
48
|
-
s.require_paths = ["lib"]
|
49
|
-
s.rubygems_version = %q{1.3.6}
|
50
|
-
s.summary = %q{.csv to .json converter}
|
51
|
-
s.test_files = [
|
23
|
+
"LICENSE",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"bin/csv2json",
|
27
|
+
"bin/json2csv",
|
28
|
+
"csv2json.gemspec",
|
29
|
+
"lib/csv2json-version.rb",
|
30
|
+
"lib/csv2json.rb",
|
31
|
+
"lib/json2csv.rb",
|
32
|
+
"test/fixtures/addresses.json",
|
33
|
+
"test/fixtures/addresses_comma.csv",
|
34
|
+
"test/fixtures/addresses_pipe.csv",
|
35
|
+
"test/fixtures/addresses_semicolon.csv",
|
36
|
+
"test/fixtures/photos.json",
|
37
|
+
"test/fixtures/photos_comma.csv",
|
38
|
+
"test/fixtures/photos_pipe.csv",
|
39
|
+
"test/fixtures/photos_semicolon.csv",
|
40
|
+
"test/fixtures/population.json",
|
41
|
+
"test/fixtures/population_comma.csv",
|
42
|
+
"test/fixtures/population_pipe.csv",
|
43
|
+
"test/fixtures/population_semicolon.csv",
|
52
44
|
"test/helper.rb",
|
53
|
-
|
45
|
+
"test/json2csv-fixtures/photos.json",
|
46
|
+
"test/json2csv-fixtures/photos_comma.csv",
|
47
|
+
"test/json2csv-fixtures/photos_pipe.csv",
|
48
|
+
"test/json2csv-fixtures/photos_semicolon.csv",
|
49
|
+
"test/test_csv2json.rb",
|
50
|
+
"test/test_json2csv.rb"
|
54
51
|
]
|
52
|
+
s.homepage = "http://github.com/darwin/csv2json"
|
53
|
+
s.require_paths = ["lib"]
|
54
|
+
s.rubygems_version = "2.1.11"
|
55
|
+
s.summary = ".csv to .json converter"
|
55
56
|
|
56
57
|
if s.respond_to? :specification_version then
|
57
|
-
|
58
|
-
s.specification_version = 3
|
58
|
+
s.specification_version = 4
|
59
59
|
|
60
|
-
if Gem::Version.new(Gem::
|
60
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
61
61
|
s.add_runtime_dependency(%q<json>, [">= 0"])
|
62
|
-
s.add_runtime_dependency(%q<fastercsv>, [">= 0"])
|
63
62
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
64
63
|
else
|
65
64
|
s.add_dependency(%q<json>, [">= 0"])
|
66
|
-
s.add_dependency(%q<fastercsv>, [">= 0"])
|
67
65
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
68
66
|
end
|
69
67
|
else
|
70
68
|
s.add_dependency(%q<json>, [">= 0"])
|
71
|
-
s.add_dependency(%q<fastercsv>, [">= 0"])
|
72
69
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
73
70
|
end
|
74
71
|
end
|
data/lib/csv2json-version.rb
CHANGED
data/lib/csv2json.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
require '
|
1
|
+
require 'rubygems'
|
2
|
+
require 'csv'
|
2
3
|
require 'json'
|
3
|
-
require '
|
4
|
+
require 'orderedhash'
|
5
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'csv2json-version.rb')
|
4
6
|
|
5
7
|
module CSV2JSON
|
6
8
|
|
@@ -11,23 +13,33 @@ module CSV2JSON
|
|
11
13
|
end
|
12
14
|
|
13
15
|
# input and output are file objects, you can use StringIO if you want to work in memory
|
14
|
-
def parse(input, output, headers=nil,
|
16
|
+
def parse(input, output, headers=nil, csvOptions={}, gemOptions={})
|
15
17
|
result = Array.new
|
16
18
|
|
17
|
-
|
19
|
+
CSV.new(input, csvOptions).each do |row|
|
18
20
|
# treat first row as headers if the caller didn't provide them
|
19
21
|
unless headers
|
20
22
|
headers = row
|
21
23
|
next
|
22
24
|
end
|
25
|
+
|
26
|
+
if gemOptions[:skipFirstRow] then
|
27
|
+
gemOptions[:skipFirstRow] = false
|
28
|
+
next
|
29
|
+
end
|
23
30
|
|
24
31
|
# build JSON snippet and append it to the result
|
25
|
-
snippet =
|
32
|
+
snippet = OrderedHash.new
|
26
33
|
headers.each_index { |i| snippet[headers[i]] = self.convert(row[i]) }
|
27
34
|
result << snippet
|
28
35
|
end
|
29
36
|
|
30
|
-
|
37
|
+
if gemOptions[:pretty] == true then
|
38
|
+
output << JSON.pretty_generate(result)
|
39
|
+
else
|
40
|
+
output << JSON.generate(result)
|
41
|
+
end
|
42
|
+
|
31
43
|
end
|
32
44
|
|
33
45
|
module_function :parse
|
data/lib/json2csv.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'csv'
|
3
|
+
require 'json'
|
4
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'csv2json-version.rb')
|
5
|
+
|
6
|
+
module JSON2CSV
|
7
|
+
|
8
|
+
def parse(input, output, headers=nil, csvOptions=nil)
|
9
|
+
json = JSON.parse(input)
|
10
|
+
|
11
|
+
unless headers
|
12
|
+
headers = json[0].keys
|
13
|
+
end
|
14
|
+
|
15
|
+
outputCSV = CSV.generate(csvOptions) do |csv|
|
16
|
+
csv << headers
|
17
|
+
|
18
|
+
json.each do |obj|
|
19
|
+
sortedValues = Array.new(headers.length)
|
20
|
+
|
21
|
+
obj.each do |key, val|
|
22
|
+
headers.each_index { |i|
|
23
|
+
sortedValues[i] = val if headers[i] == key
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
csv << sortedValues
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
output << outputCSV
|
32
|
+
end
|
33
|
+
|
34
|
+
module_function :parse
|
35
|
+
|
36
|
+
end
|
data/test/helper.rb
CHANGED
@@ -0,0 +1,122 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"thumbwidth": 75,
|
4
|
+
"imgsrc": "paris_01.jpg",
|
5
|
+
"thumbsrc": "paris_01.jpg",
|
6
|
+
"height": 262,
|
7
|
+
"imgpath": "images/",
|
8
|
+
"thumbheight": 56,
|
9
|
+
"thumbpath": "thumbnails/",
|
10
|
+
"width": 350
|
11
|
+
},
|
12
|
+
{
|
13
|
+
"thumbwidth": 75,
|
14
|
+
"imgsrc": "paris_02.jpg",
|
15
|
+
"thumbsrc": "paris_02.jpg",
|
16
|
+
"height": 350,
|
17
|
+
"imgpath": "images/",
|
18
|
+
"thumbheight": 56,
|
19
|
+
"thumbpath": "thumbnails/",
|
20
|
+
"width": 262
|
21
|
+
},
|
22
|
+
{
|
23
|
+
"thumbwidth": 75,
|
24
|
+
"imgsrc": "paris_03.jpg",
|
25
|
+
"thumbsrc": "paris_03.jpg",
|
26
|
+
"height": 262,
|
27
|
+
"imgpath": "images/",
|
28
|
+
"thumbheight": 56,
|
29
|
+
"thumbpath": "thumbnails/",
|
30
|
+
"width": 350
|
31
|
+
},
|
32
|
+
{
|
33
|
+
"thumbwidth": 56,
|
34
|
+
"imgsrc": "paris_04.jpg",
|
35
|
+
"thumbsrc": "paris_04.jpg",
|
36
|
+
"height": 350,
|
37
|
+
"imgpath": "images/",
|
38
|
+
"thumbheight": 75,
|
39
|
+
"thumbpath": "thumbnails/",
|
40
|
+
"width": 262
|
41
|
+
},
|
42
|
+
{
|
43
|
+
"thumbwidth": 56,
|
44
|
+
"imgsrc": "paris_05.jpg",
|
45
|
+
"thumbsrc": "paris_05.jpg",
|
46
|
+
"height": 350,
|
47
|
+
"imgpath": "images/",
|
48
|
+
"thumbheight": 75,
|
49
|
+
"thumbpath": "thumbnails/",
|
50
|
+
"width": 262
|
51
|
+
},
|
52
|
+
{
|
53
|
+
"thumbwidth": 75,
|
54
|
+
"imgsrc": "paris_06.jpg",
|
55
|
+
"thumbsrc": "paris_06.jpg",
|
56
|
+
"height": 262,
|
57
|
+
"imgpath": "images/",
|
58
|
+
"thumbheight": 56,
|
59
|
+
"thumbpath": "thumbnails/",
|
60
|
+
"width": 350
|
61
|
+
},
|
62
|
+
{
|
63
|
+
"thumbwidth": 56,
|
64
|
+
"imgsrc": "paris_07.jpg",
|
65
|
+
"thumbsrc": "paris_07.jpg",
|
66
|
+
"height": 350,
|
67
|
+
"imgpath": "images/",
|
68
|
+
"thumbheight": 75,
|
69
|
+
"thumbpath": "thumbnails/",
|
70
|
+
"width": 262
|
71
|
+
},
|
72
|
+
{
|
73
|
+
"thumbwidth": 56,
|
74
|
+
"imgsrc": "paris_08.jpg",
|
75
|
+
"thumbsrc": "paris_08.jpg",
|
76
|
+
"height": 350,
|
77
|
+
"imgpath": "images/",
|
78
|
+
"thumbheight": 75,
|
79
|
+
"thumbpath": "thumbnails/",
|
80
|
+
"width": 262
|
81
|
+
},
|
82
|
+
{
|
83
|
+
"thumbwidth": 56,
|
84
|
+
"imgsrc": "paris_09.jpg",
|
85
|
+
"thumbsrc": "paris_09.jpg",
|
86
|
+
"height": 350,
|
87
|
+
"imgpath": "images/",
|
88
|
+
"thumbheight": 75,
|
89
|
+
"thumbpath": "thumbnails/",
|
90
|
+
"width": 262
|
91
|
+
},
|
92
|
+
{
|
93
|
+
"thumbwidth": 75,
|
94
|
+
"imgsrc": "paris_10.jpg",
|
95
|
+
"thumbsrc": "paris_10.jpg",
|
96
|
+
"height": 262,
|
97
|
+
"imgpath": "images/",
|
98
|
+
"thumbheight": 56,
|
99
|
+
"thumbpath": "thumbnails/",
|
100
|
+
"width": 350
|
101
|
+
},
|
102
|
+
{
|
103
|
+
"thumbwidth": 75,
|
104
|
+
"imgsrc": "paris_11.jpg",
|
105
|
+
"thumbsrc": "paris_11.jpg",
|
106
|
+
"height": 262,
|
107
|
+
"imgpath": "images/",
|
108
|
+
"thumbheight": 56,
|
109
|
+
"thumbpath": "thumbnails/",
|
110
|
+
"width": 350
|
111
|
+
},
|
112
|
+
{
|
113
|
+
"thumbwidth": 75,
|
114
|
+
"imgsrc": "paris_12.jpg",
|
115
|
+
"thumbsrc": "paris_12.jpg",
|
116
|
+
"height": 262,
|
117
|
+
"imgpath": "images/",
|
118
|
+
"thumbheight": 56,
|
119
|
+
"thumbpath": "thumbnails/",
|
120
|
+
"width": 350
|
121
|
+
}
|
122
|
+
]
|
@@ -0,0 +1,13 @@
|
|
1
|
+
thumbpath,imgpath,imgsrc,width,height,thumbsrc,thumbwidth,thumbheight
|
2
|
+
thumbnails/,images/,paris_01.jpg,350,262,paris_01.jpg,75,56
|
3
|
+
thumbnails/,images/,paris_02.jpg,262,350,paris_02.jpg,75,56
|
4
|
+
thumbnails/,images/,paris_03.jpg,350,262,paris_03.jpg,75,56
|
5
|
+
thumbnails/,images/,paris_04.jpg,262,350,paris_04.jpg,56,75
|
6
|
+
thumbnails/,images/,paris_05.jpg,262,350,paris_05.jpg,56,75
|
7
|
+
thumbnails/,images/,paris_06.jpg,350,262,paris_06.jpg,75,56
|
8
|
+
thumbnails/,images/,paris_07.jpg,262,350,paris_07.jpg,56,75
|
9
|
+
thumbnails/,images/,paris_08.jpg,262,350,paris_08.jpg,56,75
|
10
|
+
thumbnails/,images/,paris_09.jpg,262,350,paris_09.jpg,56,75
|
11
|
+
thumbnails/,images/,paris_10.jpg,350,262,paris_10.jpg,75,56
|
12
|
+
thumbnails/,images/,paris_11.jpg,350,262,paris_11.jpg,75,56
|
13
|
+
thumbnails/,images/,paris_12.jpg,350,262,paris_12.jpg,75,56
|
@@ -0,0 +1,13 @@
|
|
1
|
+
thumbpath|imgpath|imgsrc|width|height|thumbsrc|thumbwidth|thumbheight
|
2
|
+
thumbnails/|images/|paris_01.jpg|350|262|paris_01.jpg|75|56
|
3
|
+
thumbnails/|images/|paris_02.jpg|262|350|paris_02.jpg|75|56
|
4
|
+
thumbnails/|images/|paris_03.jpg|350|262|paris_03.jpg|75|56
|
5
|
+
thumbnails/|images/|paris_04.jpg|262|350|paris_04.jpg|56|75
|
6
|
+
thumbnails/|images/|paris_05.jpg|262|350|paris_05.jpg|56|75
|
7
|
+
thumbnails/|images/|paris_06.jpg|350|262|paris_06.jpg|75|56
|
8
|
+
thumbnails/|images/|paris_07.jpg|262|350|paris_07.jpg|56|75
|
9
|
+
thumbnails/|images/|paris_08.jpg|262|350|paris_08.jpg|56|75
|
10
|
+
thumbnails/|images/|paris_09.jpg|262|350|paris_09.jpg|56|75
|
11
|
+
thumbnails/|images/|paris_10.jpg|350|262|paris_10.jpg|75|56
|
12
|
+
thumbnails/|images/|paris_11.jpg|350|262|paris_11.jpg|75|56
|
13
|
+
thumbnails/|images/|paris_12.jpg|350|262|paris_12.jpg|75|56
|
@@ -0,0 +1,13 @@
|
|
1
|
+
thumbpath;imgpath;imgsrc;width;height;thumbsrc;thumbwidth;thumbheight
|
2
|
+
thumbnails/;images/;paris_01.jpg;350;262;paris_01.jpg;75;56
|
3
|
+
thumbnails/;images/;paris_02.jpg;262;350;paris_02.jpg;75;56
|
4
|
+
thumbnails/;images/;paris_03.jpg;350;262;paris_03.jpg;75;56
|
5
|
+
thumbnails/;images/;paris_04.jpg;262;350;paris_04.jpg;56;75
|
6
|
+
thumbnails/;images/;paris_05.jpg;262;350;paris_05.jpg;56;75
|
7
|
+
thumbnails/;images/;paris_06.jpg;350;262;paris_06.jpg;75;56
|
8
|
+
thumbnails/;images/;paris_07.jpg;262;350;paris_07.jpg;56;75
|
9
|
+
thumbnails/;images/;paris_08.jpg;262;350;paris_08.jpg;56;75
|
10
|
+
thumbnails/;images/;paris_09.jpg;262;350;paris_09.jpg;56;75
|
11
|
+
thumbnails/;images/;paris_10.jpg;350;262;paris_10.jpg;75;56
|
12
|
+
thumbnails/;images/;paris_11.jpg;350;262;paris_11.jpg;75;56
|
13
|
+
thumbnails/;images/;paris_12.jpg;350;262;paris_12.jpg;75;56
|
data/test/test_csv2json.rb
CHANGED
@@ -1,20 +1,25 @@
|
|
1
1
|
require 'helper'
|
2
|
-
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'csv2json.rb') # this form is important for local development
|
3
2
|
|
4
3
|
class TestCsv2json < Test::Unit::TestCase
|
5
4
|
SEPS = {:comma => ',', :pipe => '|', :semicolon => ';'}
|
5
|
+
|
6
6
|
should "parse some test files" do
|
7
7
|
fixtures_dir = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
|
8
|
+
|
8
9
|
Dir.chdir(fixtures_dir) do
|
9
10
|
Dir.glob('*.csv') do |filename|
|
10
11
|
filename_parts = File.basename(filename, ".csv").split('_')
|
11
12
|
json_template = filename_parts[0] + '.json'
|
13
|
+
|
14
|
+
print "Testing csv2json against #{filename}\n"
|
15
|
+
|
12
16
|
File.open(filename, "r") do |input|
|
13
17
|
output = StringIO.new()
|
14
|
-
CSV2JSON.parse(input, output, nil, :col_sep => SEPS[filename_parts[1].to_sym] )
|
18
|
+
CSV2JSON.parse(input, output, nil, {:col_sep => SEPS[filename_parts[1].to_sym]}, {:pretty => true} )
|
15
19
|
template = File.read(json_template)
|
20
|
+
|
16
21
|
output.pos = 0
|
17
|
-
assert template == output.read
|
22
|
+
assert JSON.parse(template) == JSON.parse(output.read), "Content doesn't match"
|
18
23
|
end
|
19
24
|
end
|
20
25
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestJson2Csv < Test::Unit::TestCase
|
4
|
+
SEPS = {:comma => ',', :pipe => '|', :semicolon => ';'}
|
5
|
+
|
6
|
+
should "parse some test files" do
|
7
|
+
fixtures_dir = File.expand_path(File.join(File.dirname(__FILE__), 'json2csv-fixtures'))
|
8
|
+
|
9
|
+
Dir.chdir(fixtures_dir) do
|
10
|
+
Dir.glob('*.json') do |filename|
|
11
|
+
basename = File.basename(filename, ".json")
|
12
|
+
|
13
|
+
print "Testing json2csv against #{filename}\n"
|
14
|
+
|
15
|
+
SEPS.each do |key, val|
|
16
|
+
template = "#{basename}_#{key.to_sym}.csv"
|
17
|
+
print " ... for comparison with #{template} "
|
18
|
+
headers = ['thumbpath','imgpath','imgsrc','width','height','thumbsrc','thumbwidth','thumbheight']
|
19
|
+
|
20
|
+
input = File.read(filename)
|
21
|
+
output = StringIO.new()
|
22
|
+
JSON2CSV.parse(input, output, headers, {:col_sep => val} )
|
23
|
+
|
24
|
+
output.pos = 0
|
25
|
+
fileToCompareTo = File.read(template)
|
26
|
+
assert_equal output.read.strip, fileToCompareTo.strip
|
27
|
+
print "OK\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,78 +1,64 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv2json
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 2
|
8
|
-
- 0
|
9
|
-
version: 0.2.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Antonin Hildebrand
|
13
8
|
- Rafael Souza
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2014-03-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: json
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
version: "0"
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
31
21
|
type: :runtime
|
32
|
-
version_requirements: *id001
|
33
|
-
- !ruby/object:Gem::Dependency
|
34
|
-
name: fastercsv
|
35
22
|
prerelease: false
|
36
|
-
|
37
|
-
requirements:
|
38
|
-
- -
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
|
41
|
-
|
42
|
-
version: "0"
|
43
|
-
type: :runtime
|
44
|
-
version_requirements: *id002
|
45
|
-
- !ruby/object:Gem::Dependency
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
46
29
|
name: shoulda
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
segments:
|
53
|
-
- 0
|
54
|
-
version: "0"
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
55
35
|
type: :development
|
56
|
-
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
57
42
|
description: handy for converting xls files to json
|
58
43
|
email: antonin@hildebrand.cz
|
59
|
-
executables:
|
44
|
+
executables:
|
60
45
|
- csv2json
|
46
|
+
- json2csv
|
61
47
|
extensions: []
|
62
|
-
|
63
|
-
extra_rdoc_files:
|
48
|
+
extra_rdoc_files:
|
64
49
|
- LICENSE
|
65
50
|
- README.md
|
66
|
-
files:
|
51
|
+
files:
|
67
52
|
- .document
|
68
|
-
- .gitignore
|
69
53
|
- LICENSE
|
70
54
|
- README.md
|
71
55
|
- Rakefile
|
72
56
|
- bin/csv2json
|
57
|
+
- bin/json2csv
|
73
58
|
- csv2json.gemspec
|
74
59
|
- lib/csv2json-version.rb
|
75
60
|
- lib/csv2json.rb
|
61
|
+
- lib/json2csv.rb
|
76
62
|
- test/fixtures/addresses.json
|
77
63
|
- test/fixtures/addresses_comma.csv
|
78
64
|
- test/fixtures/addresses_pipe.csv
|
@@ -86,37 +72,33 @@ files:
|
|
86
72
|
- test/fixtures/population_pipe.csv
|
87
73
|
- test/fixtures/population_semicolon.csv
|
88
74
|
- test/helper.rb
|
75
|
+
- test/json2csv-fixtures/photos.json
|
76
|
+
- test/json2csv-fixtures/photos_comma.csv
|
77
|
+
- test/json2csv-fixtures/photos_pipe.csv
|
78
|
+
- test/json2csv-fixtures/photos_semicolon.csv
|
89
79
|
- test/test_csv2json.rb
|
90
|
-
|
80
|
+
- test/test_json2csv.rb
|
91
81
|
homepage: http://github.com/darwin/csv2json
|
92
82
|
licenses: []
|
93
|
-
|
83
|
+
metadata: {}
|
94
84
|
post_install_message:
|
95
|
-
rdoc_options:
|
96
|
-
|
97
|
-
require_paths:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
98
87
|
- lib
|
99
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- -
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
segments:
|
111
|
-
- 0
|
112
|
-
version: "0"
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
113
98
|
requirements: []
|
114
|
-
|
115
99
|
rubyforge_project:
|
116
|
-
rubygems_version: 1.
|
100
|
+
rubygems_version: 2.1.11
|
117
101
|
signing_key:
|
118
|
-
specification_version:
|
102
|
+
specification_version: 4
|
119
103
|
summary: .csv to .json converter
|
120
|
-
test_files:
|
121
|
-
- test/helper.rb
|
122
|
-
- test/test_csv2json.rb
|
104
|
+
test_files: []
|