neatjson 0.5 → 0.6
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/README.md +10 -3
- data/lib/neatjson.rb +18 -7
- data/neatjson.gemspec +1 -1
- data/test/test_neatjson.js +8 -1
- data/test/test_neatjson.rb +8 -1
- data/test/tests.js +10 -1
- data/test/tests.rb +20 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bea172fb8129d16c9334bd5ffdce64be0841fea2
|
4
|
+
data.tar.gz: 09cb444f5bc31b69a08a89e82a45484865c3d5ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85b9b72bd66a09a29aa5f299d6febce537d103cef6ca023552004730a227f6db65ef05fa5c3a94d81d827f8bb31b4b242166fd4f2f5ef892fb185527bd45609b
|
7
|
+
data.tar.gz: 6fa5bce4bdffda217798aa340b0c273e0abb9d6d628049e1351eac01d52cc0aa77243ec59febaf7e9618e82563d3c06872b9d3dd1392ce6b48d92b197183c855
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# NeatJSON
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/neatjson)
|
4
|
+
[](https://rubygems.org/gems/neatjson)
|
4
5
|
|
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.
|
6
7
|
|
@@ -133,8 +134,12 @@ You may pass any of the following option symbols to `neat_generate`:
|
|
133
134
|
* `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
|
134
135
|
* `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
|
135
136
|
* `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
|
136
|
-
* `:
|
137
|
-
* `:
|
137
|
+
* `:before_colon_1` — Number of spaces before a colon when the object is on one line. Default: `0`
|
138
|
+
* `:after_colon_1` — Number of spaces after a colon when the object is on one line. Default: `0`
|
139
|
+
* `:before_colon_n` — Number of spaces before a colon when the object is on multiple lines. Default: `0`
|
140
|
+
* `:after_colon_n` — Number of spaces after a colon when the object is on multiple lines. Default: `0`
|
141
|
+
* `:before_colon` — Shorthand to set both `:before_colon_1` and `:before_colon_n`. Default: `0`
|
142
|
+
* `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
|
138
143
|
* `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
|
139
144
|
|
140
145
|
|
@@ -150,13 +155,15 @@ For other communication you can [email the author directly](mailto:!@phrogz.net?
|
|
150
155
|
## TODO (aka Known Limitations)
|
151
156
|
|
152
157
|
* 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
158
|
* Detect circular references.
|
155
159
|
* Possibly allow illegal JSON values like `NaN` or `Infinity`.
|
156
160
|
* Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
|
157
161
|
|
158
162
|
## HISTORY
|
159
163
|
|
164
|
+
* **v0.6** - April 26th, 2015
|
165
|
+
* Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.
|
166
|
+
|
160
167
|
* **v0.5** - April 19th, 2015
|
161
168
|
* Do not format integers (or floats that equal their integer) using `decimals` option.
|
162
169
|
* Make `neatJSON()` JavaScript available to Node.js as well as web browsers.
|
data/lib/neatjson.rb
CHANGED
@@ -21,6 +21,12 @@ module JSON
|
|
21
21
|
# @option opts [Integer] :around_colon (0) Number of spaces to put before/after colons (for objects).
|
22
22
|
# @option opts [Integer] :before_colon (0) Number of spaces to put before colons (for objects).
|
23
23
|
# @option opts [Integer] :after_colon (0) Number of spaces to put after colons (for objects).
|
24
|
+
# @option opts [Integer] :around_colon_1 (0) Number of spaces to put before/after colons for single-line objects.
|
25
|
+
# @option opts [Integer] :before_colon_1 (0) Number of spaces to put before colons for single-line objects.
|
26
|
+
# @option opts [Integer] :after_colon_1 (0) Number of spaces to put after colons for single-line objects.
|
27
|
+
# @option opts [Integer] :around_colon_n (0) Number of spaces to put before/after colons for multi-line objects.
|
28
|
+
# @option opts [Integer] :before_colon_n (0) Number of spaces to put before colons for multi-line objects.
|
29
|
+
# @option opts [Integer] :after_colon_n (0) Number of spaces to put after colons for multi-line objects.
|
24
30
|
# @return [String] the JSON representation of the object.
|
25
31
|
def self.neat_generate(object,opts={})
|
26
32
|
opts[:wrap] = 80 unless opts.key?(:wrap)
|
@@ -32,12 +38,17 @@ module JSON
|
|
32
38
|
opts[:before_comma] ||= opts[:around_comma] || 0
|
33
39
|
opts[:before_colon] ||= opts[:around_colon] || 0
|
34
40
|
opts[:after_colon] ||= opts[:around_colon] || 0
|
41
|
+
opts[:before_colon_1] ||= opts[:around_colon_1] || opts[:before_colon] || 0
|
42
|
+
opts[:after_colon_1] ||= opts[:around_colon_1] || opts[:after_colon] || 0
|
43
|
+
opts[:before_colon_n] ||= opts[:around_colon_n] || opts[:before_colon] || 0
|
44
|
+
opts[:after_colon_n] ||= opts[:around_colon_n] || opts[:after_colon] || 0
|
35
45
|
raise ":indent option must only be whitespace" if opts[:indent]=~/\S/
|
36
46
|
|
37
47
|
apad = " " * opts[:array_padding]
|
38
48
|
opad = " " * opts[:object_padding]
|
39
49
|
comma = "#{' '*opts[:before_comma]},#{' '*opts[:after_comma]}"
|
40
|
-
|
50
|
+
colon1= "#{' '*opts[:before_colon_1]}:#{' '*opts[:after_colon_1]}"
|
51
|
+
colonn= "#{' '*opts[:before_colon_n]}:#{' '*opts[:after_colon_n]}"
|
41
52
|
|
42
53
|
build = ->(o,indent) do
|
43
54
|
case o
|
@@ -73,7 +84,7 @@ module JSON
|
|
73
84
|
when Hash
|
74
85
|
keyvals = o.map{ |k,v| [ k.to_s.inspect, build[v,''] ] }
|
75
86
|
keyvals = keyvals.sort_by(&:first) if opts[:sorted]
|
76
|
-
keyvals = keyvals.map{ |kv| kv.join(
|
87
|
+
keyvals = keyvals.map{ |kv| kv.join(colon1) }.join(comma)
|
77
88
|
one_line = "#{indent}{#{opad}#{keyvals}#{opad}}"
|
78
89
|
if !opts[:wrap] || (one_line.length <= opts[:wrap])
|
79
90
|
one_line
|
@@ -87,10 +98,10 @@ module JSON
|
|
87
98
|
keyvals.each{ |k,v| k.replace( "%-#{longest}s" % k ) }
|
88
99
|
end
|
89
100
|
keyvals.map! do |k,v|
|
90
|
-
indent2 = " "*"#{k}#{
|
91
|
-
one_line = "#{k}#{
|
101
|
+
indent2 = " "*"#{k}#{colonn}".length
|
102
|
+
one_line = "#{k}#{colonn}#{build[v,'']}"
|
92
103
|
if opts[:wrap] && (one_line.length > opts[:wrap]) && (v.is_a?(Array) || v.is_a?(Hash))
|
93
|
-
"#{k}#{
|
104
|
+
"#{k}#{colonn}#{build[v,indent2].lstrip}"
|
94
105
|
else
|
95
106
|
one_line
|
96
107
|
end
|
@@ -105,9 +116,9 @@ module JSON
|
|
105
116
|
end
|
106
117
|
indent2 = "#{indent}#{opts[:indent]}"
|
107
118
|
keyvals.map! do |k,v|
|
108
|
-
one_line = "#{k}#{
|
119
|
+
one_line = "#{k}#{colonn}#{build[v,'']}"
|
109
120
|
if opts[:wrap] && (one_line.length > opts[:wrap]) && (v.is_a?(Array) || v.is_a?(Hash))
|
110
|
-
"#{k}#{
|
121
|
+
"#{k}#{colonn}#{build[v,indent2].lstrip}"
|
111
122
|
else
|
112
123
|
one_line
|
113
124
|
end
|
data/neatjson.gemspec
CHANGED
data/test/test_neatjson.js
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
var neatJSON = require('../javascript/neatjson.js').neatJSON;
|
2
|
+
var startTime = new Date;
|
3
|
+
var count=0, pass=0;
|
2
4
|
require('./tests.js').tests.forEach(function(valTest){
|
3
5
|
var value = valTest.value;
|
4
6
|
valTest.tests.forEach(function(test){
|
@@ -12,12 +14,17 @@ require('./tests.js').tests.forEach(function(valTest){
|
|
12
14
|
var json = neatJSON(value);
|
13
15
|
}
|
14
16
|
var success = (test.json.constructor==RegExp) ? test.json.test(json) : json==test.json;
|
15
|
-
if (
|
17
|
+
if (success) pass+=1;
|
18
|
+
else{
|
16
19
|
console.log(mesg+")");
|
17
20
|
console.log('EXPECTED');
|
18
21
|
console.log(test.json);
|
19
22
|
console.log('ACTUAL');
|
20
23
|
console.log(json,"\n");
|
21
24
|
}
|
25
|
+
count+=1;
|
22
26
|
});
|
23
27
|
});
|
28
|
+
var elapsed = (new Date)-startTime;
|
29
|
+
console.log(pass+"/"+count+" test"+(count==1 ? '' : 's')+" passed in "+elapsed.toFixed(2)+"ms ("+(count/elapsed*1000).toFixed(0)+" tests per second)");
|
30
|
+
|
data/test/test_neatjson.rb
CHANGED
@@ -1,15 +1,22 @@
|
|
1
1
|
require '../lib/neatjson'
|
2
2
|
require './tests'
|
3
3
|
|
4
|
+
start = Time.now
|
5
|
+
pass = 0
|
6
|
+
count = 0
|
4
7
|
TESTS.each do |value_tests|
|
5
8
|
val, tests = value_tests[:value], value_tests[:tests]
|
6
9
|
tests.each do |test|
|
7
10
|
begin
|
11
|
+
count += 1
|
8
12
|
json = test[:opts] ? JSON.neat_generate(val,test[:opts].dup) : JSON.neat_generate(val)
|
9
13
|
mesg = test[:opts] ? "JSON.neat_generate(#{val.inspect},#{test[:opts].inspect})" : "JSON.neat_generate(#{val.inspect})"
|
10
14
|
raise "#{mesg}\nEXPECTED:\n#{test[:json]}\nACTUAL:\n#{json}\n\n" unless test[:json]===json
|
15
|
+
pass += 1
|
11
16
|
rescue StandardError => e
|
12
17
|
puts e
|
13
18
|
end
|
14
19
|
end
|
15
|
-
end
|
20
|
+
end
|
21
|
+
elapsed = Time.now-start
|
22
|
+
puts "%d/%d test#{:s if count!=1} passed in %.2fms (%d tests per second)" % [pass, count, elapsed*1000, count/elapsed]
|
data/test/tests.js
CHANGED
@@ -67,7 +67,16 @@ exports.tests = [
|
|
67
67
|
{ json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:1,afterComma:1,objectPadding:1} },
|
68
68
|
{ json:'{"a" : 2, "b" : 1}', opts:{sorted:true,beforeColon:1,afterColon:1,afterComma:1,arrayPadding:1} },
|
69
69
|
{ json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,aroundColon:2,afterComma:1,padding:2} },
|
70
|
-
{ json:'{ "a":2, "b":1 }', opts:{sorted:true,afterComma:1,padding:2} }
|
70
|
+
{ json:'{ "a":2, "b":1 }', opts:{sorted:true,afterComma:1,padding:2} },
|
71
|
+
{ json:'{"b": 1,"a": 2}', opts:{afterColon1:2} },
|
72
|
+
{ json:'{"b" : 1,"a" : 2}', opts:{aroundColon1:2} },
|
73
|
+
{ json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,aroundColon1:2} },
|
74
|
+
{ json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColon:1} },
|
75
|
+
{ json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,afterColonN:1} },
|
76
|
+
{ json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
|
77
|
+
{ json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColon:1} },
|
78
|
+
{ json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,afterColonN:1} },
|
79
|
+
{ json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,afterColon1:1} }
|
71
80
|
]},
|
72
81
|
|
73
82
|
{value:{b:1,aaa:2,cc:3}, tests:[
|
data/test/tests.rb
CHANGED
@@ -55,17 +55,26 @@ TESTS = [
|
|
55
55
|
|
56
56
|
{value:{b:1,a:2}, tests:[
|
57
57
|
{ json:'{"b":1,"a":2}' },
|
58
|
-
{ json:'{"a":2,"b":1}',
|
59
|
-
{ json:'{"a":2, "b":1}',
|
60
|
-
{ json:'{"a" :2,"b" :1}',
|
61
|
-
{ json:'{"a": 2,"b": 1}',
|
62
|
-
{ json:'{"a" : 2,"b" : 1}',
|
63
|
-
{ json:'{"a" : 2, "b" : 1}',
|
64
|
-
{ json:'{ "a" : 2, "b" : 1 }',
|
65
|
-
{ json:'{ "a" : 2, "b" : 1 }',
|
66
|
-
{ json:'{"a" : 2, "b" : 1}',
|
67
|
-
{ json:'{ "a" : 2, "b" : 1 }',
|
68
|
-
{ json:'{ "a":2, "b":1 }',
|
58
|
+
{ json:'{"a":2,"b":1}', opts:{sorted:true} },
|
59
|
+
{ json:'{"a":2, "b":1}', opts:{sorted:true,after_comma:1} },
|
60
|
+
{ json:'{"a" :2,"b" :1}', opts:{sorted:true,before_colon:1} },
|
61
|
+
{ json:'{"a": 2,"b": 1}', opts:{sorted:true,after_colon:1} },
|
62
|
+
{ json:'{"a" : 2,"b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1} },
|
63
|
+
{ json:'{"a" : 2, "b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1} },
|
64
|
+
{ json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1,padding:1} },
|
65
|
+
{ json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,around_colon:1,after_comma:1,object_padding:1} },
|
66
|
+
{ json:'{"a" : 2, "b" : 1}', opts:{sorted:true,before_colon:1,after_colon:1,after_comma:1,array_padding:1} },
|
67
|
+
{ json:'{ "a" : 2, "b" : 1 }', opts:{sorted:true,around_colon:2,after_comma:1,padding:2} },
|
68
|
+
{ json:'{ "a":2, "b":1 }', opts:{sorted:true,after_comma:1,padding:2} },
|
69
|
+
{ json:'{"b": 1,"a": 2}', opts:{after_colon_1:2} },
|
70
|
+
{ json:'{"b" : 1,"a" : 2}', opts:{around_colon_1:2} },
|
71
|
+
{ json:"{\n \"b\":1,\n \"a\":2\n}", opts:{wrap:true,around_colon_1:2} },
|
72
|
+
{ json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,after_colon:1} },
|
73
|
+
{ json:"{\n \"b\": 1,\n \"a\": 2\n}", opts:{wrap:true,after_colon_n:1} },
|
74
|
+
{ json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true} },
|
75
|
+
{ json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,after_colon:1} },
|
76
|
+
{ json:"{\"b\": 1,\n \"a\": 2}", opts:{wrap:true,short:true,after_colon_n:1} },
|
77
|
+
{ json:"{\"b\":1,\n \"a\":2}", opts:{wrap:true,short:true,after_colon_1:1} },
|
69
78
|
]},
|
70
79
|
|
71
80
|
{value:{b:1,aaa:2,cc:3}, tests:[
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: neatjson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.6'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Kistner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Generate JSON strings from Ruby objects with flexible formatting options.
|
14
14
|
Key features: keep arrays and objects on a single line when they fit; format floats
|