neatjson 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b69e23425f7658f7d0c5c0ec2c4a4b53992b9120
4
- data.tar.gz: 1df4af5d7e34063d68a0321e4d5dfa30a1f3fbfa
3
+ metadata.gz: 2f7545da8e38d691cb0449295f34cdd8ea014d84
4
+ data.tar.gz: 3e10ff207247a397dc9c2efcea6375056995468e
5
5
  SHA512:
6
- metadata.gz: ef1109ed0d1348e7e196e92129d12bc714918804e36b9ba754598ae1f543b9b6f3c6438aaa59baa56251d98d1989cb94fb9b6f8ecbe257a410800bb2ad82cc32
7
- data.tar.gz: 9a4425fb526aabe6614b054cf9fd08f8ea9b7e6a9e18386386479aa85727d62000ba9f42321427e31652ba30df2e27b3514a2f963f0ec461c725f62c61dbebc2
6
+ metadata.gz: 2f9665339a7d3e524e798f40f555e76fa63eb1fd3daa443f76f66928d39165351fb77ee21ac0472950f5d5722b8f567a56d0b9529573f4772302bcdde5a52e19
7
+ data.tar.gz: b6a833def30e44660707afe2a3616340051c32775b460b4cea85fc5c1d9b373b868f71218ab473459ab87b40e067eff8d1a6ef53cd7415c251f1b4106ba15859
@@ -0,0 +1 @@
1
+ --markup markdown
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 Gavin Kistner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,175 @@
1
+ # NeatJSON
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
4
+
5
+ Pretty-print your JSON in Ruby with more power than is provided by `JSON.pretty_generate`. In particular, like Ruby's `pp` (pretty print), NeatJSON will keep objects on one line if they fit, but break them over multiple lines if needed.
6
+
7
+ Here's an excerpt (from a much larger JSON):
8
+
9
+ ~~~ json
10
+ {
11
+ "navigation.createroute.poi":[
12
+ {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
13
+ {"text":"Take me to the airport","params":{"poi":"airport"}},
14
+ {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
15
+ {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
16
+ {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
17
+ {
18
+ "text":"Go to the Hilton by the Airport",
19
+ "params":{"poi":"Hilton","location":"Airport"}
20
+ },
21
+ {
22
+ "text":"Take me to the Fry's in Fresno",
23
+ "params":{"poi":"Fry's","location":"Fresno"}
24
+ }
25
+ ],
26
+ "navigation.eta":[
27
+ {"text":"When will we get there?"},
28
+ {"text":"When will I arrive?"},
29
+ {"text":"What time will I get to the destination?"},
30
+ {"text":"What time will I reach the destination?"},
31
+ {"text":"What time will it be when I arrive?"}
32
+ ]
33
+ }
34
+ ~~~
35
+
36
+ ## Installation
37
+
38
+ `gem install neatjson`
39
+
40
+ ## Examples
41
+
42
+ ~~~ ruby
43
+ require 'neatjson'
44
+
45
+ o = { b:42.005, a:[42,17], longer:true, str:"yes
46
+ please" }
47
+
48
+ puts JSON.neat_generate(o)
49
+ #=> {"b":42.005,"a":[42,17],"longer":true,"str":"yes\nplease"}
50
+
51
+ puts JSON.neat_generate(o,sorted:true)
52
+ #=> {"a":[42,17],"b":42.005,"longer":true,"str":"yes\nplease"}
53
+
54
+ puts JSON.neat_generate(o,sorted:true,padding:1,after_comma:1)
55
+ #=> { "a":[ 42, 17 ], "b":42.005, "longer":true, "str":"yes\nplease" }
56
+
57
+ puts JSON.neat_generate(o,sorted:true,wrap:40)
58
+ #=> {
59
+ #=> "a":[42,17],
60
+ #=> "b":42.005,
61
+ #=> "longer":true,
62
+ #=> "str":"yes\nplease"
63
+ #=> }
64
+
65
+ puts JSON.neat_generate(o,sorted:true,wrap:40,decimals:1)
66
+ #=> {
67
+ #=> "a":[42.0,17.0],
68
+ #=> "b":42.0,
69
+ #=> "longer":true,
70
+ #=> "str":"yes\nplease"
71
+ #=> }
72
+
73
+ puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true)
74
+ #=> {
75
+ #=> "a" :[42,17],
76
+ #=> "b" :42.005,
77
+ #=> "longer":true,
78
+ #=> "str" :"yes\nplease"
79
+ #=> }
80
+
81
+ puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1)
82
+ #=> {
83
+ #=> "a" : [42,17],
84
+ #=> "b" : 42.005,
85
+ #=> "longer" : true,
86
+ #=> "str" : "yes\nplease"
87
+ #=> }
88
+
89
+ puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1,short:true)
90
+ #=> {"a" : [42,17],
91
+ #=> "b" : 42.005,
92
+ #=> "longer" : true,
93
+ #=> "str" : "yes\nplease"}
94
+
95
+ a = [1,2,[3,4,[5]]]
96
+ puts JSON.neat_generate(a)
97
+ #=> [1,2,[3,4,[5]]]
98
+
99
+ puts JSON.pretty_generate(a) # oof!
100
+ #=> [
101
+ #=> 1,
102
+ #=> 2,
103
+ #=> [
104
+ #=> 3,
105
+ #=> 4,
106
+ #=> [
107
+ #=> 5
108
+ #=> ]
109
+ #=> ]
110
+ #=> ]
111
+
112
+ puts JSON.neat_generate( a, wrap:true, short:true )
113
+ #=> [1,
114
+ #=> 2,
115
+ #=> [3,
116
+ #=> 4,
117
+ #=> [5]]]
118
+ ~~~
119
+
120
+ ## Options
121
+ You may pass any of the following option symbols to `neat_generate`:
122
+
123
+ * `:wrap` — The maximum line width before wrapping. Use `false` to never wrap, or `true` (or `-1`) to always wrap. Default: `80`
124
+ * `:indent` — Whitespace used to indent each level when wrapping. Default: `" "` (two spaces)
125
+ * `:short` — Keep the output 'short' when wrapping? This puts opening brackets on the same line as the first value, and closing brackets on the same line as the last. Default: `false`
126
+ * _This causes the `:indent` option to be ignored, instead basing indentation on array and object padding._
127
+ * `:sorted` — Sort the keys for objects to be in alphabetical order? Default: `false`
128
+ * `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
129
+ * `:decimals` — Decimal precision to use for numbers; use `false` to keep numeric values precise. Default: `false`
130
+ * `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
131
+ * `:object_padding` — Number of spaces to put inside braces for objects. Default: `0`
132
+ * `:padding` — Shorthand to set both `:array_padding` and `:object_padding`. Default: `0`
133
+ * `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
134
+ * `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
135
+ * `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
136
+ * `:before_colon` — Number of spaces to put before colons. Default: `0`
137
+ * `:after_colon` — Number of spaces to put after colons. Default: `0`
138
+ * `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
139
+
140
+
141
+ ## License & Contact
142
+
143
+ NeatJSON is copyright ©2015 by Gavin Kistner and is released under
144
+ the [MIT License](http://www.opensource.org/licenses/mit-license.php).
145
+ See the LICENSE.txt file for more details.
146
+
147
+ For bugs or feature requests please open [issues on GitHub][1].
148
+ For other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).
149
+
150
+ ## TODO (aka Known Limitations)
151
+
152
+ * Figure out the best way to play with custom objects that use `to_json` for their representation.
153
+ * Option for `around_colon` to only apply to multi-line objects.
154
+ * Detect circular references.
155
+ * Possibly allow illegal JSON values like `NaN` or `Infinity`.
156
+ * Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
157
+
158
+ ## HISTORY
159
+
160
+ * **v0.3.2** - April 16th, 2015
161
+ * Force YARD to use Markdown for documentation.
162
+
163
+ * **v0.3.1** - April 16th, 2015
164
+ * Remove some debugging code accidentally left in.
165
+
166
+ * **v0.3** - April 16th, 2015
167
+ * Fix another bug with `short:true` and wrapping array values inside objects.
168
+
169
+ * **v0.2** - April 16th, 2015
170
+ * Fix bug with `short:true` and wrapping values inside objects.
171
+
172
+ * **v0.1** - April 15th, 2015
173
+ * Initial release.
174
+
175
+ [1]: https://github.com/Phrogz/NeatJSON/issues
File without changes
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+ require 'date'
3
+ Gem::Specification.new do |s|
4
+ s.name = "neatjson"
5
+ s.version = "0.3.3"
6
+ s.date = Date.today.iso8601
7
+ s.authors = ["Gavin Kistner"]
8
+ s.email = "gavin@phrogz.net"
9
+ s.homepage = "http://github.com/Phrogz/NeatJSON"
10
+ s.summary = "Pretty, powerful, flexible JSON generation."
11
+ s.description = "Generate JSON strings from Ruby objects with flexible formatting options. The most important feature is that it can behave like pp, keeping arrays and objects on a single line when they fit."
12
+ s.license = "MIT license (MIT)"
13
+ s.files = Dir.glob("{lib,test}/**/*") + ['LICENSE.txt', 'README.md', '.yardopts', __FILE__]
14
+ s.has_rdoc = 'yard'
15
+ end
@@ -0,0 +1,138 @@
1
+ require 'minitest/autorun'
2
+ require '../lib/neatjson'
3
+
4
+ class Dummy
5
+ def to_json(*a)
6
+ {a:1}.to_json(*a)
7
+ end
8
+ end
9
+
10
+ class Flexi
11
+ def to_json(*a)
12
+ JSON.neat_generate({a:1},*a)
13
+ end
14
+ end
15
+
16
+ class TestFlexiJSON < Minitest::Unit::TestCase
17
+ def test_booleans
18
+ assert_equal "true", JSON.neat_generate(true)
19
+ assert_equal "false", JSON.neat_generate(false)
20
+ assert_equal "null", JSON.neat_generate(nil)
21
+ end
22
+
23
+ def test_numbers
24
+ assert_equal "5", JSON.neat_generate(5)
25
+ assert_equal "4.2", JSON.neat_generate(4.2)
26
+ assert_equal "4.20", JSON.neat_generate(4.2, decimals:2)
27
+ assert_equal "4.20", JSON.neat_generate(4.199,decimals:2)
28
+ assert_equal "4.20", JSON.neat_generate(4.204,decimals:2)
29
+
30
+ assert_equal "4", JSON.neat_generate(4.2, decimals:0)
31
+ assert_equal "5", JSON.neat_generate(4.9, decimals:0)
32
+ assert_equal "-2", JSON.neat_generate(-1.9,decimals:0)
33
+ assert_equal "-2", JSON.neat_generate(-2.4,decimals:0)
34
+
35
+ assert_equal "1.0e+23", JSON.neat_generate(1e23)
36
+ assert_equal "1.0e-09", JSON.neat_generate(1e-9)
37
+ end
38
+
39
+ def test_strings
40
+ assert_equal '"foo"', JSON.neat_generate('foo')
41
+ assert_equal '"foo"', JSON.neat_generate(:foo)
42
+ assert_equal '"foo\nbar"', JSON.neat_generate("foo\nbar")
43
+ end
44
+
45
+ def test_arrays
46
+ @a1 = [1,2,3,4,[5,6,7,[8,9,10],11,12]]
47
+ assert_equal "[1,2,3,4,[5,6,7,[8,9,10],11,12]]", JSON.neat_generate(@a1)
48
+ assert_equal "[\n 1,\n 2,\n 3,\n 4,\n [5,6,7,[8,9,10],11,12]\n]", JSON.neat_generate(@a1,wrap:30)
49
+ assert_equal "[\n 1,\n 2,\n 3,\n 4,\n [\n 5,\n 6,\n 7,\n [8,9,10],\n 11,\n 12\n ]\n]", JSON.neat_generate(@a1,wrap:20)
50
+ assert_equal "[\n 1,\n 2,\n 3,\n 4,\n [\n 5,\n 6,\n 7,\n [\n 8,\n 9,\n 10\n ],\n 11,\n 12\n ]\n]", JSON.neat_generate(@a1,wrap:true)
51
+ assert_equal "[\n\t1,\n\t2,\n\t3,\n\t4,\n\t[\n\t\t5,\n\t\t6,\n\t\t7,\n\t\t[\n\t\t\t8,\n\t\t\t9,\n\t\t\t10\n\t\t],\n\t\t11,\n\t\t12\n\t]\n]", JSON.neat_generate(@a1,wrap:true,indent:"\t")
52
+ assert_equal "[1,2,3,4,[5,6,7,[8,9,10],11,12]]", JSON.neat_generate(@a1,array_padding:0)
53
+ assert_equal "[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", JSON.neat_generate(@a1,array_padding:1)
54
+ assert_equal "[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", JSON.neat_generate(@a1,array_padding:2)
55
+ assert_equal "[1, 2, 3, 4, [5, 6, 7, [8, 9, 10], 11, 12]]", JSON.neat_generate(@a1,after_comma:1)
56
+ assert_equal "[ 1, 2, 3, 4, [ 5, 6, 7, [ 8, 9, 10 ], 11, 12 ] ]", JSON.neat_generate(@a1,after_comma:1,array_padding:1)
57
+ assert_equal "[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", JSON.neat_generate(@a1,short:true,wrap:true)
58
+ assert_equal "[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", JSON.neat_generate(@a1,short:true,wrap:true,after_comma:1)
59
+ assert_equal "[ 1,\n 2,\n 3,\n 4,\n [ 5,\n 6,\n 7,\n [ 8,\n 9,\n 10 ],\n 11,\n 12 ] ]", JSON.neat_generate(@a1,short:true,wrap:true,array_padding:1)
60
+
61
+ @a2 = [1,2,3]
62
+ assert_equal "[1,2,3]", JSON.neat_generate(@a2)
63
+ assert_equal "[1 ,2 ,3]", JSON.neat_generate(@a2,before_comma:1)
64
+ assert_equal "[1 , 2 , 3]", JSON.neat_generate(@a2,around_comma:1)
65
+ end
66
+
67
+ def test_hashes
68
+ @h1 = {b:1,a:2}
69
+ assert_equal '{"b":1,"a":2}', JSON.neat_generate(@h1)
70
+ assert_equal '{"a":2,"b":1}', JSON.neat_generate(@h1,sorted:true)
71
+
72
+ assert_equal '{"a":2, "b":1}', JSON.neat_generate(@h1,sorted:true,after_comma:1)
73
+ assert_equal '{"a" :2,"b" :1}', JSON.neat_generate(@h1,sorted:true,before_colon:1)
74
+ assert_equal '{"a": 2,"b": 1}', JSON.neat_generate(@h1,sorted:true,after_colon:1)
75
+ assert_equal '{"a" : 2,"b" : 1}', JSON.neat_generate(@h1,sorted:true,before_colon:1,after_colon:1)
76
+ assert_equal '{"a" : 2, "b" : 1}', JSON.neat_generate(@h1,sorted:true,before_colon:1,after_colon:1,after_comma:1)
77
+ assert_equal '{ "a" : 2, "b" : 1 }', JSON.neat_generate(@h1,sorted:true,before_colon:1,after_colon:1,after_comma:1,padding:1)
78
+ assert_equal '{ "a" : 2, "b" : 1 }', JSON.neat_generate(@h1,sorted:true,around_colon:1,after_comma:1,object_padding:1)
79
+ assert_equal '{"a" : 2, "b" : 1}', JSON.neat_generate(@h1,sorted:true,before_colon:1,after_colon:1,after_comma:1,array_padding:1)
80
+ assert_equal '{ "a" : 2, "b" : 1 }', JSON.neat_generate(@h1,sorted:true,around_colon:2,after_comma:1,padding:2)
81
+ assert_equal '{ "a":2, "b":1 }', JSON.neat_generate(@h1,sorted:true,after_comma:1,padding:2)
82
+
83
+ @h2 = {b:1,aaa:2,cc:3}
84
+ assert_equal %Q{{\n "b":1,\n "aaa":2,\n "cc":3\n}}, JSON.neat_generate(@h2,wrap:true)
85
+ assert_equal %Q{{\n "b" :1,\n "aaa":2,\n "cc" :3\n}}, JSON.neat_generate(@h2,wrap:true,aligned:true)
86
+ assert_equal %Q{{"b":1,"aaa":2,"cc":3}}, JSON.neat_generate(@h2,aligned:true)
87
+ assert_equal %Q{{\n "aaa":2,\n "b" :1,\n "cc" :3\n}}, JSON.neat_generate(@h2,wrap:true,aligned:true,sorted:true)
88
+
89
+ @h3 = {a:1}
90
+ assert_equal '{"a":1}', JSON.neat_generate(@h3)
91
+ assert_equal "{\n \"a\":1\n}", JSON.neat_generate(@h3,wrap:true)
92
+
93
+ @h4 = { b:17, a:42 }
94
+ assert_equal "{\"a\":42,\n \"b\":17}", JSON.neat_generate(@h4,wrap:10,sorted:true,short:true)
95
+ end
96
+
97
+ def test_mixed
98
+ a = [1,{a:2},3]
99
+ assert_equal '[1,{"a":2},3]', JSON.neat_generate(a)
100
+ assert_equal '[ 1,{ "a":2 },3 ]', JSON.neat_generate(a,padding:1)
101
+ assert_equal '[ 1, { "a":2 }, 3 ]', JSON.neat_generate(a,padding:1,after_comma:1)
102
+ assert_equal %Q{[\n 1,\n {\n "a":2\n },\n 3\n]}, JSON.neat_generate(a,wrap:true)
103
+ assert_equal %Q{[\n 1,\n {"a":2},\n 3\n]}, JSON.neat_generate(a,wrap:10)
104
+
105
+ a = [1,{a:2,b:3},4]
106
+ assert_equal "[1,\n {\"a\":2,\n \"b\":3},\n 4]", JSON.neat_generate(a,wrap:0,short:true)
107
+
108
+ h = {a:1,b:[2,3,4],c:3}
109
+ assert_equal '{"a":1,"b":[2,3,4],"c":3}', JSON.neat_generate(h)
110
+ assert_equal %Q{{\n "a":1,\n "b":[2,3,4],\n "c":3\n}}, JSON.neat_generate(h,wrap:10)
111
+ assert_equal %Q{{\n "a":1,\n "b":[\n 2,\n 3,\n 4\n ],\n "c":3\n}}, JSON.neat_generate(h,wrap:true)
112
+
113
+ h = {hooo:42,whee:%w[yaaa oooo booy],zoop:"whoop"}
114
+ assert_equal <<-ENDFORMAT.chomp, JSON.neat_generate(h,wrap:20,short:true)
115
+ {"hooo":42,
116
+ "whee":["yaaa",
117
+ "oooo",
118
+ "booy"],
119
+ "zoop":"whoop"}
120
+ ENDFORMAT
121
+
122
+ h = { a:[ {x:"foo",y:"jim"}, {x:"bar",y:"jam"} ] }
123
+ assert_equal <<-ENDFORMAT.chomp, JSON.neat_generate(h,wrap:true,short:true)
124
+ {"a":[{"x":"foo",
125
+ "y":"jim"},
126
+ {"x":"bar",
127
+ "y":"jam"}]}
128
+ ENDFORMAT
129
+ end
130
+
131
+ def test_custom
132
+ assert_equal '{ "a":1}', JSON.neat_generate(Dummy.new)
133
+ assert_equal '{ "a":1}', JSON.neat_generate(Dummy.new,wrap:true)
134
+ assert_equal '{"a":1}', JSON.neat_generate(Dummy.new,indent:'')
135
+ assert_equal '{"a":1}', JSON.neat_generate(Flexi.new)
136
+ assert_equal "{\n \"a\":1\n}", JSON.neat_generate(Flexi.new,wrap:true)
137
+ end
138
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neatjson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Kistner
@@ -10,13 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-04-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description: Generate JSON strings from Ruby objects with flexible formatting options.
14
+ The most important feature is that it can behave like pp, keeping arrays and objects
15
+ on a single line when they fit.
14
16
  email: gavin@phrogz.net
15
17
  executables: []
16
18
  extensions: []
17
19
  extra_rdoc_files: []
18
20
  files:
19
21
  - lib/neatjson.rb
22
+ - test/test_neatjson.rb
23
+ - LICENSE.txt
24
+ - README.md
25
+ - .yardopts
26
+ - neatjson.gemspec
20
27
  homepage: http://github.com/Phrogz/NeatJSON
21
28
  licenses:
22
29
  - MIT license (MIT)
@@ -37,9 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
44
  version: '0'
38
45
  requirements: []
39
46
  rubyforge_project:
40
- rubygems_version: 2.4.5
47
+ rubygems_version: 2.0.14
41
48
  signing_key:
42
49
  specification_version: 4
43
50
  summary: Pretty, powerful, flexible JSON generation.
44
51
  test_files: []
45
- has_rdoc: yard