tidy_json 0.2.2 → 0.5.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/.yardopts +3 -1
- data/Gemfile +7 -0
- data/LICENSE +1 -1
- data/README.md +84 -38
- data/bin/jtidy +143 -0
- data/bin/jtidy_info.rb +34 -0
- data/lib/tidy_json/dedication.rb +8 -2
- data/lib/tidy_json/formatter.rb +238 -0
- data/lib/tidy_json/serializer.rb +118 -0
- data/lib/tidy_json/version.rb +1 -1
- data/lib/tidy_json.rb +28 -376
- data/test/JsonableObject.json +1 -0
- data/test/codecov_runner.rb +16 -0
- data/test/test_tidy_json.rb +61 -31
- data/tidy_json.gemspec +8 -4
- metadata +28 -12
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
if ENV['COVERAGE']
|
|
4
|
+
begin
|
|
5
|
+
require 'simplecov'
|
|
6
|
+
SimpleCov.start
|
|
7
|
+
SimpleCov.command_name 'Unit Tests'
|
|
8
|
+
|
|
9
|
+
if ENV['CI']
|
|
10
|
+
require 'simplecov-cobertura'
|
|
11
|
+
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
|
|
12
|
+
end
|
|
13
|
+
rescue LoadError
|
|
14
|
+
warn "Can't locate coverage drivers! Try running: `bundle install` first."
|
|
15
|
+
end
|
|
16
|
+
end
|
data/test/test_tidy_json.rb
CHANGED
|
@@ -1,37 +1,59 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative 'codecov_runner'
|
|
3
4
|
require 'test/unit'
|
|
4
5
|
require 'tidy_json'
|
|
5
6
|
|
|
6
7
|
##
|
|
7
8
|
# Tests.
|
|
8
9
|
#
|
|
10
|
+
class Nested
|
|
11
|
+
attr_accessor :a, :b, :c, :d, :e
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@a = [[], [[5, 6, 7]], [1, 2, { 'three': 3, 'four': [5, 6] }, [4]], { 'inner': 1 }]
|
|
15
|
+
@b = { 'a': [5], 'b': [1, 2, 3, [4]], 'c': [5], 'd': { 'inner': 1 }, 'e': [6] }
|
|
16
|
+
@c = [[1, 2, 3, [4]], { 'inner': 1 }, {}]
|
|
17
|
+
@d = [{}, [], [1, 2, 3, [4]], { 'inner': 1 }, []]
|
|
18
|
+
@e = [[1, 2, 3, [4]], { 'inner': 1 }, {}]
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
9
22
|
class JsonableObject
|
|
10
|
-
attr_reader
|
|
23
|
+
attr_reader :a, :b, :c, :d, :e, :f, :h
|
|
11
24
|
|
|
12
25
|
def initialize
|
|
13
26
|
@h = { one: 'uno', two: 'dos', three: %w[eine zwei drei], cuatro: ['I', 'II', 'III', ['i.', 'ii.', 'iii.', 'iv.']] }
|
|
14
27
|
@a = ['k', 'l', %w[M N O P], 'q', 'r', 's', [10, 456, ['<abbr title="Reel 2, Dialog Track 2">R2D2</abbr>', 'R', 2, 'D', ['two']]], 'u', 'v', 'x', 'y', %w[Z AB]]
|
|
28
|
+
@b = [{ 'uno': Nested.new }, [Nested.new, [Nested.new, Nested.new], [Nested.new]]]
|
|
29
|
+
@c = [[Nested.new, [Nested.new], [Nested.new]]]
|
|
30
|
+
@d = []
|
|
31
|
+
@f = {}
|
|
15
32
|
end
|
|
16
33
|
end
|
|
17
34
|
|
|
18
35
|
class TidyJsonTest < Test::Unit::TestCase
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
def setup
|
|
37
|
+
@t = JsonableObject.new
|
|
38
|
+
t2 = JsonableObject.new
|
|
39
|
+
t3 = JsonableObject.new
|
|
40
|
+
|
|
41
|
+
30.times { |_| t3.d << JsonableObject.new }
|
|
42
|
+
t2.h[:five] = t3
|
|
43
|
+
|
|
44
|
+
@t.h[:cinque] = { 'ichi' => "\u{4e00}", 'ni' => "\u{4e8c}", 'san' => "\u{4e09}", 'yon' => "\u{56db}" }
|
|
45
|
+
@t.h[:sei] = t2
|
|
46
|
+
@t.a.unshift([t2, 13, 14, 15, 5.6])
|
|
47
|
+
end
|
|
26
48
|
|
|
27
49
|
def test_version_number
|
|
28
50
|
refute_nil ::TidyJson::VERSION
|
|
29
51
|
end
|
|
30
52
|
|
|
31
53
|
def test_tidy_static
|
|
32
|
-
assert_equal("{\n \"a\": \"one\"
|
|
54
|
+
assert_equal("{\n \"a\": \"one\",\n \"A\": \"ONE\",\n \"b\": null\n}\n",
|
|
33
55
|
TidyJson.tidy(a: 'one', A: 'ONE', b: nil))
|
|
34
|
-
assert_equal(
|
|
56
|
+
assert_equal(3, TidyJson.tidy({}).length)
|
|
35
57
|
end
|
|
36
58
|
|
|
37
59
|
def test_sort_keys_static
|
|
@@ -55,35 +77,39 @@ class TidyJsonTest < Test::Unit::TestCase
|
|
|
55
77
|
def test_sort_keys_instance
|
|
56
78
|
flat_hash_array = [{ c: 3 }, { a: 1 }, { b: 2 }]
|
|
57
79
|
nested_hash_array = [{ c: 3, d: { i: '34', ii: '35', f: 56, a: 9 } }, { a: 1 }, { b: 2 }]
|
|
58
|
-
assert_equal("[\n {\n \"a\": 1\n }
|
|
80
|
+
assert_equal("[\n {\n \"a\": 1\n },\n {\n \"b\": 2\n },\n {\n \"c\": 3\n }\n]\n",
|
|
59
81
|
flat_hash_array.to_tidy_json(sort: true))
|
|
60
|
-
assert_equal("[\n {\n \"a\": 1\n }
|
|
82
|
+
assert_equal("[\n {\n \"a\": 1\n },\n {\n \"b\": 2\n },\n {\n \"c\": 3,\n \"d\": {\n \"a\": 9,\n \"f\": 56,\n \"i\": \"34\",\n \"ii\": \"35\"\n }\n }\n]\n",
|
|
61
83
|
nested_hash_array.to_tidy_json(indent: 8, sort: true))
|
|
62
|
-
assert_equal("{\n \"a\": \"one\"
|
|
84
|
+
assert_equal("{\n \"a\": \"one\",\n \"b\": \"two\",\n \"c\": 3\n}\n",
|
|
63
85
|
{ 'b': 'two', 'c': 3, 'a': 'one' }.to_tidy_json(indent: 6, sort: true))
|
|
64
|
-
assert_equal("[
|
|
65
|
-
assert_equal("{
|
|
66
|
-
assert_equal("[\n 3
|
|
86
|
+
assert_equal("[]\n", [].to_tidy_json(sort: true))
|
|
87
|
+
assert_equal("{}\n", {}.to_tidy_json(sort: true))
|
|
88
|
+
assert_equal("[\n 3,\n 2,\n 1\n]\n",
|
|
67
89
|
[3, 2, 1].to_tidy_json(indent: 8, sort: true))
|
|
68
|
-
assert_equal("[\n {\n \"b\": \"two\"\n }
|
|
90
|
+
assert_equal("[\n {\n \"b\": \"two\"\n },\n \"one\"\n]\n",
|
|
69
91
|
[{ 'b': 'two' }, 'one'].to_tidy_json(indent: 4, sort: true))
|
|
70
92
|
end
|
|
71
93
|
|
|
72
94
|
def test_tidy_instance
|
|
73
|
-
assert_equal({}.to_tidy_json, "{
|
|
74
|
-
assert_equal([].to_tidy_json, "[
|
|
75
|
-
assert_equal(
|
|
76
|
-
assert_equal(JsonableObject.new.to_tidy_json.length,
|
|
95
|
+
assert_equal({}.to_tidy_json, "{}\n")
|
|
96
|
+
assert_equal([].to_tidy_json, "[]\n")
|
|
97
|
+
assert_equal(String.new.to_tidy_json, "\"\"\n")
|
|
98
|
+
assert_equal(JsonableObject.new.to_tidy_json.length, 13_410)
|
|
77
99
|
end
|
|
78
100
|
|
|
79
101
|
def test_stringify_instance
|
|
80
|
-
|
|
102
|
+
File.open("#{__dir__}/JsonableObject.json", 'r') do |json|
|
|
103
|
+
assert_equal(@t.stringify, json.read.strip)
|
|
104
|
+
end
|
|
105
|
+
rescue Errno::ENOENT, Errno::EACCES, IOError => e
|
|
106
|
+
flunk "#{__FILE__}.#{__LINE__}: #{e.message}"
|
|
81
107
|
end
|
|
82
108
|
|
|
83
109
|
def test_writers
|
|
84
110
|
json_array = []
|
|
85
111
|
assert_nothing_thrown '#stringify returns valid JSON' do
|
|
86
|
-
3.times { |_| json_array << JSON.parse(
|
|
112
|
+
3.times { |_| json_array << JSON.parse(@t.stringify) }
|
|
87
113
|
end
|
|
88
114
|
|
|
89
115
|
output = json_array.write_json
|
|
@@ -93,24 +119,28 @@ class TidyJsonTest < Test::Unit::TestCase
|
|
|
93
119
|
end
|
|
94
120
|
|
|
95
121
|
pretty_output = \
|
|
96
|
-
json_array.write_json('prettified', tidy: true, sort: true, indent:
|
|
122
|
+
json_array.write_json('prettified', tidy: true, sort: true, indent: 4)
|
|
123
|
+
|
|
97
124
|
assert(File.exist?(pretty_output))
|
|
125
|
+
|
|
98
126
|
assert_nothing_thrown 'Formatted JSON should be valid' do
|
|
99
127
|
File.open(pretty_output, 'r') { |f| JSON.parse(f.read) }
|
|
100
128
|
end
|
|
129
|
+
|
|
130
|
+
assert_nil json_array.write_json('/invalid/file/name/')
|
|
101
131
|
end
|
|
102
132
|
|
|
103
133
|
def test_indent_bounds_checking
|
|
104
|
-
assert_equal("{\n \"a\": \"one\"
|
|
134
|
+
assert_equal("{\n \"a\": \"one\",\n \"b\": \"two\",\n \"c\": 3\n}\n",
|
|
105
135
|
{ 'b': 'two', 'c': 3, 'a': 'one' }.to_tidy_json(indent: 5, sort: true),
|
|
106
136
|
'odd values should fall back to default of 2')
|
|
107
|
-
assert_equal([].to_tidy_json(indent: '16'), "[
|
|
137
|
+
assert_equal([].to_tidy_json(indent: '16'), "[]\n",
|
|
108
138
|
'values > 12 should fall back to default of 2')
|
|
109
|
-
assert_equal('Object'.to_tidy_json(indent: []),
|
|
110
|
-
assert_equal(0.to_tidy_json(indent: -89),
|
|
111
|
-
assert_equal(3.1425.to_tidy_json(indent: 3.1425),
|
|
112
|
-
assert_equal(''.to_tidy_json(indent: +0),
|
|
113
|
-
assert_equal([].to_tidy_json(indent: -8.00009), "[
|
|
139
|
+
assert_equal('Object'.to_tidy_json(indent: []), "\"Object\"\n")
|
|
140
|
+
assert_equal(0.to_tidy_json(indent: -89), "0\n")
|
|
141
|
+
assert_equal(3.1425.to_tidy_json(indent: 3.1425), "3.1425\n")
|
|
142
|
+
assert_equal(''.to_tidy_json(indent: +0), "\"\"\n")
|
|
143
|
+
assert_equal([].to_tidy_json(indent: -8.00009), "[]\n")
|
|
114
144
|
assert_nothing_thrown '#stringify should return valid JSON even when ' \
|
|
115
145
|
'format options are invalid' do
|
|
116
146
|
assert_equal(JSON.parse(Object.new.stringify).to_tidy_json(indent: nil),
|
data/tidy_json.gemspec
CHANGED
|
@@ -12,16 +12,20 @@ Gem::Specification.new do |spec|
|
|
|
12
12
|
spec.authors = ['Robert Di Pardo']
|
|
13
13
|
spec.email = 'dipardo.r@gmail.com'
|
|
14
14
|
spec.homepage = 'https://github.com/rdipardo/tidy_json'
|
|
15
|
-
spec.metadata = { 'documentation_uri' => 'https://rubydoc.org/github/rdipardo/tidy_json/master' }
|
|
16
15
|
spec.license = 'MIT'
|
|
16
|
+
spec.metadata = {
|
|
17
|
+
'documentation_uri' => 'https://rubydoc.org/github/rdipardo/tidy_json',
|
|
18
|
+
'bug_tracker_uri' => 'https://github.com/rdipardo/tidy_json/issues'
|
|
19
|
+
}
|
|
17
20
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
18
|
-
['.yardopts'].concat(`git ls-files -z`.split("\x0").reject { |f| f.match(/^(\.[\w+\.]+|test|spec|features)/) })
|
|
21
|
+
['.yardopts'].concat(`git ls-files -z`.split("\x0").reject { |f| f.match(/^(\.[\w+\.]+|test|spec|features|codecov)/) })
|
|
19
22
|
end
|
|
20
23
|
spec.test_files = Dir['test/*']
|
|
21
24
|
spec.require_paths = ['lib']
|
|
25
|
+
spec.executables = ['jtidy']
|
|
22
26
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.3')
|
|
23
|
-
spec.add_runtime_dependency 'json', '~> 2.
|
|
24
|
-
spec.add_development_dependency 'test-unit', '~> 3.
|
|
27
|
+
spec.add_runtime_dependency 'json', '~> 2.5'
|
|
28
|
+
spec.add_development_dependency 'test-unit', '~> 3.4'
|
|
25
29
|
spec.add_development_dependency 'yard', '~> 0.9'
|
|
26
30
|
spec.rdoc_options = ['-x test/*']
|
|
27
31
|
spec.post_install_message = TidyJson::DEDICATION
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tidy_json
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robert Di Pardo
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-12-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: json
|
|
@@ -16,28 +16,28 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '2.
|
|
19
|
+
version: '2.5'
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '2.
|
|
26
|
+
version: '2.5'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: test-unit
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - "~>"
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '3.
|
|
33
|
+
version: '3.4'
|
|
34
34
|
type: :development
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '3.
|
|
40
|
+
version: '3.4'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: yard
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -54,7 +54,8 @@ dependencies:
|
|
|
54
54
|
version: '0.9'
|
|
55
55
|
description: A mixin providing (recursive) JSON serialization and pretty printing.
|
|
56
56
|
email: dipardo.r@gmail.com
|
|
57
|
-
executables:
|
|
57
|
+
executables:
|
|
58
|
+
- jtidy
|
|
58
59
|
extensions: []
|
|
59
60
|
extra_rdoc_files: []
|
|
60
61
|
files:
|
|
@@ -63,16 +64,23 @@ files:
|
|
|
63
64
|
- LICENSE
|
|
64
65
|
- README.md
|
|
65
66
|
- Rakefile
|
|
67
|
+
- bin/jtidy
|
|
68
|
+
- bin/jtidy_info.rb
|
|
66
69
|
- lib/tidy_json.rb
|
|
67
70
|
- lib/tidy_json/dedication.rb
|
|
71
|
+
- lib/tidy_json/formatter.rb
|
|
72
|
+
- lib/tidy_json/serializer.rb
|
|
68
73
|
- lib/tidy_json/version.rb
|
|
74
|
+
- test/JsonableObject.json
|
|
75
|
+
- test/codecov_runner.rb
|
|
69
76
|
- test/test_tidy_json.rb
|
|
70
77
|
- tidy_json.gemspec
|
|
71
78
|
homepage: https://github.com/rdipardo/tidy_json
|
|
72
79
|
licenses:
|
|
73
80
|
- MIT
|
|
74
81
|
metadata:
|
|
75
|
-
documentation_uri: https://rubydoc.org/github/rdipardo/tidy_json
|
|
82
|
+
documentation_uri: https://rubydoc.org/github/rdipardo/tidy_json
|
|
83
|
+
bug_tracker_uri: https://github.com/rdipardo/tidy_json/issues
|
|
76
84
|
post_install_message: |2+
|
|
77
85
|
|
|
78
86
|
....................................................
|
|
@@ -82,9 +90,14 @@ post_install_message: |2+
|
|
|
82
90
|
................. MICHAEL DI PARDO .................
|
|
83
91
|
....................................................
|
|
84
92
|
............ Please consider supporting ............
|
|
85
|
-
|
|
93
|
+
........... multiple sclerosis research: ...........
|
|
86
94
|
....................................................
|
|
95
|
+
....................... (US) .......................
|
|
96
|
+
..... https://www.nationalmssociety.org/Donate .....
|
|
97
|
+
..................... (Canada) .....................
|
|
87
98
|
........ https://mssociety.ca/get-involved .........
|
|
99
|
+
....................... (UK) .......................
|
|
100
|
+
.... https://www.mssociety.org.uk/get-involved .....
|
|
88
101
|
....................................................
|
|
89
102
|
|
|
90
103
|
rdoc_options:
|
|
@@ -102,9 +115,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
102
115
|
- !ruby/object:Gem::Version
|
|
103
116
|
version: '0'
|
|
104
117
|
requirements: []
|
|
105
|
-
rubygems_version: 3.
|
|
106
|
-
signing_key:
|
|
118
|
+
rubygems_version: 3.2.22
|
|
119
|
+
signing_key:
|
|
107
120
|
specification_version: 4
|
|
108
121
|
summary: Serialize any Ruby object as readable JSON
|
|
109
122
|
test_files:
|
|
123
|
+
- test/JsonableObject.json
|
|
124
|
+
- test/codecov_runner.rb
|
|
110
125
|
- test/test_tidy_json.rb
|
|
126
|
+
...
|