neatjson 0.7 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +199 -199
- data/lib/neatjson.rb +58 -52
- data/neatjson.gemspec +17 -17
- data/test/test_neatjson.rb +2 -2
- data/test/tests.js +173 -173
- data/test/tests.rb +169 -169
- data/test/tmp.js +27768 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ba694978273218e8577ae10c6efb53a2d386681
|
4
|
+
data.tar.gz: 33fac207c68fa536dcfe7f21f604e4a9a8e4968d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f124cf9575cda404bbc35053cb0a4433ac8e42ce0836c4549558fa8246b8e59691c3e58503e59e73bc0d19d957124593459dac351bdd6b1f2d1b2d3079c1a762
|
7
|
+
data.tar.gz: ba9d138fd53c0506154053e222694c59936168e018f4bcc7527f8abaf262da2e96d2325e26536b79d89056a246ffb0a50a346d70efa2863bda9414773c72f983
|
data/README.md
CHANGED
@@ -1,199 +1,199 @@
|
|
1
|
-
# NeatJSON
|
2
|
-
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
|
4
|
-
[![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
|
5
|
-
|
6
|
-
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.
|
7
|
-
|
8
|
-
Here's an excerpt (from a much larger JSON):
|
9
|
-
|
10
|
-
~~~ json
|
11
|
-
{
|
12
|
-
"navigation.createroute.poi":[
|
13
|
-
{"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
|
14
|
-
{"text":"Take me to the airport","params":{"poi":"airport"}},
|
15
|
-
{"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
|
16
|
-
{"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
|
17
|
-
{"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
|
18
|
-
{
|
19
|
-
"text":"Go to the Hilton by the Airport",
|
20
|
-
"params":{"poi":"Hilton","location":"Airport"}
|
21
|
-
},
|
22
|
-
{
|
23
|
-
"text":"Take me to the Fry's in Fresno",
|
24
|
-
"params":{"poi":"Fry's","location":"Fresno"}
|
25
|
-
}
|
26
|
-
],
|
27
|
-
"navigation.eta":[
|
28
|
-
{"text":"When will we get there?"},
|
29
|
-
{"text":"When will I arrive?"},
|
30
|
-
{"text":"What time will I get to the destination?"},
|
31
|
-
{"text":"What time will I reach the destination?"},
|
32
|
-
{"text":"What time will it be when I arrive?"}
|
33
|
-
]
|
34
|
-
}
|
35
|
-
~~~
|
36
|
-
|
37
|
-
## Installation
|
38
|
-
|
39
|
-
`gem install neatjson`
|
40
|
-
|
41
|
-
## Examples
|
42
|
-
|
43
|
-
~~~ ruby
|
44
|
-
require 'neatjson'
|
45
|
-
|
46
|
-
o = { b:42.005, a:[42,17], longer:true, str:"yes
|
47
|
-
please" }
|
48
|
-
|
49
|
-
puts JSON.neat_generate(o)
|
50
|
-
#=> {"b":42.005,"a":[42,17],"longer":true,"str":"yes\nplease"}
|
51
|
-
|
52
|
-
puts JSON.neat_generate(o,sorted:true)
|
53
|
-
#=> {"a":[42,17],"b":42.005,"longer":true,"str":"yes\nplease"}
|
54
|
-
|
55
|
-
puts JSON.neat_generate(o,sorted:true,padding:1,after_comma:1)
|
56
|
-
#=> { "a":[ 42, 17 ], "b":42.005, "longer":true, "str":"yes\nplease" }
|
57
|
-
|
58
|
-
puts JSON.neat_generate(o,sorted:true,wrap:40)
|
59
|
-
#=> {
|
60
|
-
#=> "a":[42,17],
|
61
|
-
#=> "b":42.005,
|
62
|
-
#=> "longer":true,
|
63
|
-
#=> "str":"yes\nplease"
|
64
|
-
#=> }
|
65
|
-
|
66
|
-
puts JSON.neat_generate(o,sorted:true,wrap:40,decimals:2)
|
67
|
-
#=> {
|
68
|
-
#=> "a":[42,17],
|
69
|
-
#=> "b":42.01,
|
70
|
-
#=> "longer":true,
|
71
|
-
#=> "str":"yes\nplease"
|
72
|
-
#=> }
|
73
|
-
|
74
|
-
puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true)
|
75
|
-
#=> {
|
76
|
-
#=> "a" :[42,17],
|
77
|
-
#=> "b" :42.005,
|
78
|
-
#=> "longer":true,
|
79
|
-
#=> "str" :"yes\nplease"
|
80
|
-
#=> }
|
81
|
-
|
82
|
-
puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1)
|
83
|
-
#=> {
|
84
|
-
#=> "a" : [42,17],
|
85
|
-
#=> "b" : 42.005,
|
86
|
-
#=> "longer" : true,
|
87
|
-
#=> "str" : "yes\nplease"
|
88
|
-
#=> }
|
89
|
-
|
90
|
-
puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1,short:true)
|
91
|
-
#=> {"a" : [42,17],
|
92
|
-
#=> "b" : 42.005,
|
93
|
-
#=> "longer" : true,
|
94
|
-
#=> "str" : "yes\nplease"}
|
95
|
-
|
96
|
-
a = [1,2,[3,4,[5]]]
|
97
|
-
puts JSON.neat_generate(a)
|
98
|
-
#=> [1,2,[3,4,[5]]]
|
99
|
-
|
100
|
-
puts JSON.pretty_generate(a) # oof!
|
101
|
-
#=> [
|
102
|
-
#=> 1,
|
103
|
-
#=> 2,
|
104
|
-
#=> [
|
105
|
-
#=> 3,
|
106
|
-
#=> 4,
|
107
|
-
#=> [
|
108
|
-
#=> 5
|
109
|
-
#=> ]
|
110
|
-
#=> ]
|
111
|
-
#=> ]
|
112
|
-
|
113
|
-
puts JSON.neat_generate( a, wrap:true, short:true )
|
114
|
-
#=> [1,
|
115
|
-
#=> 2,
|
116
|
-
#=> [3,
|
117
|
-
#=> 4,
|
118
|
-
#=> [5]]]
|
119
|
-
~~~
|
120
|
-
|
121
|
-
## Options
|
122
|
-
You may pass any of the following option symbols to `neat_generate`:
|
123
|
-
|
124
|
-
* `:wrap` — The maximum line width before wrapping. Use `false` to never wrap, or `true` (or `-1`) to always wrap. Default: `80`
|
125
|
-
* `:indent` — Whitespace used to indent each level when wrapping. Default: `" "` (two spaces)
|
126
|
-
* `:indent_last` — Indent the closing bracket/brace for arrays and objects? Default: `false`
|
127
|
-
* `: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`
|
128
|
-
* _This causes the `:indent` and `:indent_last` options to be ignored, instead basing indentation on array and object padding._
|
129
|
-
* `:sorted` — Sort the keys for objects to be in alphabetical order? Default: `false`
|
130
|
-
* `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
|
131
|
-
* `:decimals` — Decimal precision to use for non-integer numbers; use `false` to keep float values precise. Default: `false`
|
132
|
-
* `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
|
133
|
-
* `:object_padding` — Number of spaces to put inside braces for objects. Default: `0`
|
134
|
-
* `:padding` — Shorthand to set both `:array_padding` and `:object_padding`. Default: `0`
|
135
|
-
* `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
|
136
|
-
* `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
|
137
|
-
* `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
|
138
|
-
* `:before_colon_1` — Number of spaces before a colon when the object is on one line. Default: `0`
|
139
|
-
* `:after_colon_1` — Number of spaces after a colon when the object is on one line. Default: `0`
|
140
|
-
* `:before_colon_n` — Number of spaces before a colon when the object is on multiple lines. Default: `0`
|
141
|
-
* `:after_colon_n` — Number of spaces after a colon when the object is on multiple lines. Default: `0`
|
142
|
-
* `:before_colon` — Shorthand to set both `:before_colon_1` and `:before_colon_n`. Default: `0`
|
143
|
-
* `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
|
144
|
-
* `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
|
145
|
-
|
146
|
-
|
147
|
-
## License & Contact
|
148
|
-
|
149
|
-
NeatJSON is copyright ©2015 by Gavin Kistner and is released under
|
150
|
-
the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
151
|
-
See the LICENSE.txt file for more details.
|
152
|
-
|
153
|
-
For bugs or feature requests please open [issues on GitHub][1].
|
154
|
-
For other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).
|
155
|
-
|
156
|
-
## TODO (aka Known Limitations)
|
157
|
-
|
158
|
-
* Figure out the best way to play with custom objects that use `to_json` for their representation.
|
159
|
-
* Detect circular references.
|
160
|
-
* Possibly allow illegal JSON values like `NaN` or `Infinity`.
|
161
|
-
* Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
|
162
|
-
|
163
|
-
## HISTORY
|
164
|
-
|
165
|
-
* **v0.6.2** - February 8th, 2016
|
166
|
-
* Use memoization to avoid performance stalls when wrapping deeply-nested objects/arrays.
|
167
|
-
_Thanks @chroche_
|
168
|
-
|
169
|
-
* **v0.6.1** - October 12th, 2015
|
170
|
-
* Fix handling of nested empty objects and arrays. (Would cause a runtime error in many cases.)
|
171
|
-
* _This change causes empty arrays in a tight wrapping scenario to appear on a single line where they would previously take up three lines._
|
172
|
-
|
173
|
-
* **v0.6** - April 26th, 2015
|
174
|
-
* Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.
|
175
|
-
|
176
|
-
* **v0.5** - April 19th, 2015
|
177
|
-
* Do not format integers (or floats that equal their integer) using `decimals` option.
|
178
|
-
* Make `neatJSON()` JavaScript available to Node.js as well as web browsers.
|
179
|
-
* Add (Node-based) testing for the JavaScript version.
|
180
|
-
|
181
|
-
* **v0.4** - April 18th, 2015
|
182
|
-
* Add JavaScript version with online runner.
|
183
|
-
|
184
|
-
* **v0.3.2** - April 16th, 2015
|
185
|
-
* Force YARD to use Markdown for documentation.
|
186
|
-
|
187
|
-
* **v0.3.1** - April 16th, 2015
|
188
|
-
* Remove some debugging code accidentally left in.
|
189
|
-
|
190
|
-
* **v0.3** - April 16th, 2015
|
191
|
-
* Fix another bug with `short:true` and wrapping array values inside objects.
|
192
|
-
|
193
|
-
* **v0.2** - April 16th, 2015
|
194
|
-
* Fix bug with `short:true` and wrapping values inside objects.
|
195
|
-
|
196
|
-
* **v0.1** - April 15th, 2015
|
197
|
-
* Initial release.
|
198
|
-
|
199
|
-
[1]: https://github.com/Phrogz/NeatJSON/issues
|
1
|
+
# NeatJSON
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
|
4
|
+
[![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
|
5
|
+
|
6
|
+
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.
|
7
|
+
|
8
|
+
Here's an excerpt (from a much larger JSON):
|
9
|
+
|
10
|
+
~~~ json
|
11
|
+
{
|
12
|
+
"navigation.createroute.poi":[
|
13
|
+
{"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
|
14
|
+
{"text":"Take me to the airport","params":{"poi":"airport"}},
|
15
|
+
{"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
|
16
|
+
{"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
|
17
|
+
{"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
|
18
|
+
{
|
19
|
+
"text":"Go to the Hilton by the Airport",
|
20
|
+
"params":{"poi":"Hilton","location":"Airport"}
|
21
|
+
},
|
22
|
+
{
|
23
|
+
"text":"Take me to the Fry's in Fresno",
|
24
|
+
"params":{"poi":"Fry's","location":"Fresno"}
|
25
|
+
}
|
26
|
+
],
|
27
|
+
"navigation.eta":[
|
28
|
+
{"text":"When will we get there?"},
|
29
|
+
{"text":"When will I arrive?"},
|
30
|
+
{"text":"What time will I get to the destination?"},
|
31
|
+
{"text":"What time will I reach the destination?"},
|
32
|
+
{"text":"What time will it be when I arrive?"}
|
33
|
+
]
|
34
|
+
}
|
35
|
+
~~~
|
36
|
+
|
37
|
+
## Installation
|
38
|
+
|
39
|
+
`gem install neatjson`
|
40
|
+
|
41
|
+
## Examples
|
42
|
+
|
43
|
+
~~~ ruby
|
44
|
+
require 'neatjson'
|
45
|
+
|
46
|
+
o = { b:42.005, a:[42,17], longer:true, str:"yes
|
47
|
+
please" }
|
48
|
+
|
49
|
+
puts JSON.neat_generate(o)
|
50
|
+
#=> {"b":42.005,"a":[42,17],"longer":true,"str":"yes\nplease"}
|
51
|
+
|
52
|
+
puts JSON.neat_generate(o,sorted:true)
|
53
|
+
#=> {"a":[42,17],"b":42.005,"longer":true,"str":"yes\nplease"}
|
54
|
+
|
55
|
+
puts JSON.neat_generate(o,sorted:true,padding:1,after_comma:1)
|
56
|
+
#=> { "a":[ 42, 17 ], "b":42.005, "longer":true, "str":"yes\nplease" }
|
57
|
+
|
58
|
+
puts JSON.neat_generate(o,sorted:true,wrap:40)
|
59
|
+
#=> {
|
60
|
+
#=> "a":[42,17],
|
61
|
+
#=> "b":42.005,
|
62
|
+
#=> "longer":true,
|
63
|
+
#=> "str":"yes\nplease"
|
64
|
+
#=> }
|
65
|
+
|
66
|
+
puts JSON.neat_generate(o,sorted:true,wrap:40,decimals:2)
|
67
|
+
#=> {
|
68
|
+
#=> "a":[42,17],
|
69
|
+
#=> "b":42.01,
|
70
|
+
#=> "longer":true,
|
71
|
+
#=> "str":"yes\nplease"
|
72
|
+
#=> }
|
73
|
+
|
74
|
+
puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true)
|
75
|
+
#=> {
|
76
|
+
#=> "a" :[42,17],
|
77
|
+
#=> "b" :42.005,
|
78
|
+
#=> "longer":true,
|
79
|
+
#=> "str" :"yes\nplease"
|
80
|
+
#=> }
|
81
|
+
|
82
|
+
puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1)
|
83
|
+
#=> {
|
84
|
+
#=> "a" : [42,17],
|
85
|
+
#=> "b" : 42.005,
|
86
|
+
#=> "longer" : true,
|
87
|
+
#=> "str" : "yes\nplease"
|
88
|
+
#=> }
|
89
|
+
|
90
|
+
puts JSON.neat_generate(o,sorted:true,wrap:40,aligned:true,around_colon:1,short:true)
|
91
|
+
#=> {"a" : [42,17],
|
92
|
+
#=> "b" : 42.005,
|
93
|
+
#=> "longer" : true,
|
94
|
+
#=> "str" : "yes\nplease"}
|
95
|
+
|
96
|
+
a = [1,2,[3,4,[5]]]
|
97
|
+
puts JSON.neat_generate(a)
|
98
|
+
#=> [1,2,[3,4,[5]]]
|
99
|
+
|
100
|
+
puts JSON.pretty_generate(a) # oof!
|
101
|
+
#=> [
|
102
|
+
#=> 1,
|
103
|
+
#=> 2,
|
104
|
+
#=> [
|
105
|
+
#=> 3,
|
106
|
+
#=> 4,
|
107
|
+
#=> [
|
108
|
+
#=> 5
|
109
|
+
#=> ]
|
110
|
+
#=> ]
|
111
|
+
#=> ]
|
112
|
+
|
113
|
+
puts JSON.neat_generate( a, wrap:true, short:true )
|
114
|
+
#=> [1,
|
115
|
+
#=> 2,
|
116
|
+
#=> [3,
|
117
|
+
#=> 4,
|
118
|
+
#=> [5]]]
|
119
|
+
~~~
|
120
|
+
|
121
|
+
## Options
|
122
|
+
You may pass any of the following option symbols to `neat_generate`:
|
123
|
+
|
124
|
+
* `:wrap` — The maximum line width before wrapping. Use `false` to never wrap, or `true` (or `-1`) to always wrap. Default: `80`
|
125
|
+
* `:indent` — Whitespace used to indent each level when wrapping. Default: `" "` (two spaces)
|
126
|
+
* `:indent_last` — Indent the closing bracket/brace for arrays and objects? Default: `false`
|
127
|
+
* `: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`
|
128
|
+
* _This causes the `:indent` and `:indent_last` options to be ignored, instead basing indentation on array and object padding._
|
129
|
+
* `:sorted` — Sort the keys for objects to be in alphabetical order? Default: `false`
|
130
|
+
* `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
|
131
|
+
* `:decimals` — Decimal precision to use for non-integer numbers; use `false` to keep float values precise. Default: `false`
|
132
|
+
* `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
|
133
|
+
* `:object_padding` — Number of spaces to put inside braces for objects. Default: `0`
|
134
|
+
* `:padding` — Shorthand to set both `:array_padding` and `:object_padding`. Default: `0`
|
135
|
+
* `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
|
136
|
+
* `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
|
137
|
+
* `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
|
138
|
+
* `:before_colon_1` — Number of spaces before a colon when the object is on one line. Default: `0`
|
139
|
+
* `:after_colon_1` — Number of spaces after a colon when the object is on one line. Default: `0`
|
140
|
+
* `:before_colon_n` — Number of spaces before a colon when the object is on multiple lines. Default: `0`
|
141
|
+
* `:after_colon_n` — Number of spaces after a colon when the object is on multiple lines. Default: `0`
|
142
|
+
* `:before_colon` — Shorthand to set both `:before_colon_1` and `:before_colon_n`. Default: `0`
|
143
|
+
* `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
|
144
|
+
* `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
|
145
|
+
|
146
|
+
|
147
|
+
## License & Contact
|
148
|
+
|
149
|
+
NeatJSON is copyright ©2015 by Gavin Kistner and is released under
|
150
|
+
the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
151
|
+
See the LICENSE.txt file for more details.
|
152
|
+
|
153
|
+
For bugs or feature requests please open [issues on GitHub][1].
|
154
|
+
For other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).
|
155
|
+
|
156
|
+
## TODO (aka Known Limitations)
|
157
|
+
|
158
|
+
* Figure out the best way to play with custom objects that use `to_json` for their representation.
|
159
|
+
* Detect circular references.
|
160
|
+
* Possibly allow illegal JSON values like `NaN` or `Infinity`.
|
161
|
+
* Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
|
162
|
+
|
163
|
+
## HISTORY
|
164
|
+
|
165
|
+
* **v0.6.2** - February 8th, 2016
|
166
|
+
* Use memoization to avoid performance stalls when wrapping deeply-nested objects/arrays.
|
167
|
+
_Thanks @chroche_
|
168
|
+
|
169
|
+
* **v0.6.1** - October 12th, 2015
|
170
|
+
* Fix handling of nested empty objects and arrays. (Would cause a runtime error in many cases.)
|
171
|
+
* _This change causes empty arrays in a tight wrapping scenario to appear on a single line where they would previously take up three lines._
|
172
|
+
|
173
|
+
* **v0.6** - April 26th, 2015
|
174
|
+
* Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.
|
175
|
+
|
176
|
+
* **v0.5** - April 19th, 2015
|
177
|
+
* Do not format integers (or floats that equal their integer) using `decimals` option.
|
178
|
+
* Make `neatJSON()` JavaScript available to Node.js as well as web browsers.
|
179
|
+
* Add (Node-based) testing for the JavaScript version.
|
180
|
+
|
181
|
+
* **v0.4** - April 18th, 2015
|
182
|
+
* Add JavaScript version with online runner.
|
183
|
+
|
184
|
+
* **v0.3.2** - April 16th, 2015
|
185
|
+
* Force YARD to use Markdown for documentation.
|
186
|
+
|
187
|
+
* **v0.3.1** - April 16th, 2015
|
188
|
+
* Remove some debugging code accidentally left in.
|
189
|
+
|
190
|
+
* **v0.3** - April 16th, 2015
|
191
|
+
* Fix another bug with `short:true` and wrapping array values inside objects.
|
192
|
+
|
193
|
+
* **v0.2** - April 16th, 2015
|
194
|
+
* Fix bug with `short:true` and wrapping values inside objects.
|
195
|
+
|
196
|
+
* **v0.1** - April 15th, 2015
|
197
|
+
* Initial release.
|
198
|
+
|
199
|
+
[1]: https://github.com/Phrogz/NeatJSON/issues
|
data/lib/neatjson.rb
CHANGED
@@ -68,66 +68,72 @@ module JSON
|
|
68
68
|
end
|
69
69
|
|
70
70
|
when Array
|
71
|
-
|
72
|
-
|
73
|
-
one_line = "#{indent}[#{apad}#{pieces.join comma}#{apad}]"
|
74
|
-
if !opts[:wrap] || (one_line.length <= opts[:wrap])
|
75
|
-
one_line
|
76
|
-
elsif opts[:short]
|
77
|
-
indent2 = "#{indent} #{apad}"
|
78
|
-
pieces = o.map{ |v| build[ v,indent2 ] }
|
79
|
-
pieces[0].sub! indent2, "#{indent}[#{apad}"
|
80
|
-
pieces.last << apad << "]"
|
81
|
-
pieces.join ",\n"
|
71
|
+
if o.empty?
|
72
|
+
"#{indent}[]"
|
82
73
|
else
|
83
|
-
|
84
|
-
"#{indent}[
|
74
|
+
pieces = o.map{ |v| build[v,''] }
|
75
|
+
one_line = "#{indent}[#{apad}#{pieces.join comma}#{apad}]"
|
76
|
+
if !opts[:wrap] || (one_line.length <= opts[:wrap])
|
77
|
+
one_line
|
78
|
+
elsif opts[:short]
|
79
|
+
indent2 = "#{indent} #{apad}"
|
80
|
+
pieces = o.map{ |v| build[ v,indent2 ] }
|
81
|
+
pieces[0].sub! indent2, "#{indent}[#{apad}"
|
82
|
+
pieces.last << apad << "]"
|
83
|
+
pieces.join ",\n"
|
84
|
+
else
|
85
|
+
indent2 = "#{indent}#{opts[:indent]}"
|
86
|
+
"#{indent}[\n#{o.map{ |v| build[ v, indent2 ] }.join ",\n"}\n#{opts[:indent_last] ? indent2 : indent}]"
|
87
|
+
end
|
85
88
|
end
|
86
89
|
|
87
90
|
when Hash
|
88
|
-
|
89
|
-
|
90
|
-
keyvals = keyvals.sort_by(&:first) if opts[:sorted]
|
91
|
-
keyvals = keyvals.map{ |kv| kv.join(colon1) }.join(comma)
|
92
|
-
one_line = "#{indent}{#{opad}#{keyvals}#{opad}}"
|
93
|
-
if !opts[:wrap] || (one_line.length <= opts[:wrap])
|
94
|
-
one_line
|
91
|
+
if o.empty?
|
92
|
+
"#{indent}{}"
|
95
93
|
else
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
keyvals.each{ |k,v| k.replace( "%-#{longest}s" % k ) }
|
103
|
-
end
|
104
|
-
keyvals.map! do |k,v|
|
105
|
-
indent2 = " "*"#{k}#{colonn}".length
|
106
|
-
one_line = "#{k}#{colonn}#{build[v,'']}"
|
107
|
-
if opts[:wrap] && (one_line.length > opts[:wrap]) && (v.is_a?(Array) || v.is_a?(Hash))
|
108
|
-
"#{k}#{colonn}#{build[v,indent2].lstrip}"
|
109
|
-
else
|
110
|
-
one_line
|
111
|
-
end
|
112
|
-
end
|
113
|
-
keyvals.join(",\n") << opad << "}"
|
94
|
+
keyvals = o.map{ |k,v| [ k.to_s.inspect, build[v,''] ] }
|
95
|
+
keyvals = keyvals.sort_by(&:first) if opts[:sorted]
|
96
|
+
keyvals = keyvals.map{ |kv| kv.join(colon1) }.join(comma)
|
97
|
+
one_line = "#{indent}{#{opad}#{keyvals}#{opad}}"
|
98
|
+
if !opts[:wrap] || (one_line.length <= opts[:wrap])
|
99
|
+
one_line
|
114
100
|
else
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
"#{k}#{colonn}#{build[v,
|
126
|
-
|
127
|
-
|
101
|
+
if opts[:short]
|
102
|
+
keyvals = o.map{ |k,v| ["#{indent} #{opad}#{k.to_s.inspect}",v] }
|
103
|
+
keyvals = keyvals.sort_by(&:first) if opts[:sorted]
|
104
|
+
keyvals[0][0].sub! "#{indent} ", "#{indent}{"
|
105
|
+
if opts[:aligned]
|
106
|
+
longest = keyvals.map(&:first).map(&:length).max
|
107
|
+
keyvals.each{ |k,v| k.replace( "%-#{longest}s" % k ) }
|
108
|
+
end
|
109
|
+
keyvals.map! do |k,v|
|
110
|
+
indent2 = " "*"#{k}#{colonn}".length
|
111
|
+
one_line = "#{k}#{colonn}#{build[v,'']}"
|
112
|
+
if opts[:wrap] && (one_line.length > opts[:wrap]) && (v.is_a?(Array) || v.is_a?(Hash))
|
113
|
+
"#{k}#{colonn}#{build[v,indent2].lstrip}"
|
114
|
+
else
|
115
|
+
one_line
|
116
|
+
end
|
117
|
+
end
|
118
|
+
keyvals.join(",\n") << opad << "}"
|
119
|
+
else
|
120
|
+
keyvals = o.map{ |k,v| ["#{indent}#{opts[:indent]}#{k.to_s.inspect}",v] }
|
121
|
+
keyvals = keyvals.sort_by(&:first) if opts[:sorted]
|
122
|
+
if opts[:aligned]
|
123
|
+
longest = keyvals.map(&:first).map(&:length).max
|
124
|
+
keyvals.each{ |k,v| k.replace( "%-#{longest}s" % k ) }
|
125
|
+
end
|
126
|
+
indent2 = "#{indent}#{opts[:indent]}"
|
127
|
+
keyvals.map! do |k,v|
|
128
|
+
one_line = "#{k}#{colonn}#{build[v,'']}"
|
129
|
+
if opts[:wrap] && (one_line.length > opts[:wrap]) && (v.is_a?(Array) || v.is_a?(Hash))
|
130
|
+
"#{k}#{colonn}#{build[v,indent2].lstrip}"
|
131
|
+
else
|
132
|
+
one_line
|
133
|
+
end
|
128
134
|
end
|
135
|
+
"#{indent}{\n#{keyvals.join(",\n")}\n#{opts[:indent_last] ? indent2 : indent}}"
|
129
136
|
end
|
130
|
-
"#{indent}{\n#{keyvals.join(",\n")}\n#{opts[:indent_last] ? indent2 : indent}}"
|
131
137
|
end
|
132
138
|
end
|
133
139
|
|