neatjson 0.8.4 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -6
- data/lib/neatjson.rb +5 -1
- data/neatjson.gemspec +1 -1
- data/test/test_neatjson.lua +83 -0
- data/test/tests.js +5 -0
- data/test/tests.lua +184 -0
- data/test/tests.rb +5 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 993364bb8a9a2afda35c7a298954472e96bb3e0d
|
4
|
+
data.tar.gz: 781ace75559493a25110d01b51471bfee5304cca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f65b6adc2a60f76b2ab0b18824d9f5586cf411db7efe2a59004df1848168af390f3572e35bcc41b4df276e35d970591860eeb7b5f3f9f62ccbdc0f92e6edb15c
|
7
|
+
data.tar.gz: e3de0825b58bbf313414e64508df9113c174ca5cfd9c9f9e73b5dc007eba0429b222d82c18ec7d6f53c248e0ac96a3e8c92ced94de1884605b5337ae418a2a8d
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
|
4
4
|
[![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
|
5
5
|
|
6
|
-
Pretty-print your JSON in Ruby or JavaScript with more power than is provided by `JSON.pretty_generate` (Ruby) or `JSON.stringify` (JS). For example, like Ruby's `pp` (pretty print), NeatJSON can keep objects on one line if they fit, but break them over multiple lines if needed.
|
6
|
+
Pretty-print your JSON in Ruby or JavaScript or Lua with more power than is provided by `JSON.pretty_generate` (Ruby) or `JSON.stringify` (JS). For example, like Ruby's `pp` (pretty print), NeatJSON can keep objects on one line if they fit, but break them over multiple lines if needed.
|
7
7
|
|
8
8
|
**Features (all optional):**
|
9
9
|
|
@@ -17,6 +17,8 @@ Pretty-print your JSON in Ruby or JavaScript with more power than is provided by
|
|
17
17
|
* Adjust number of spaces before/after commas and colons (both for single- vs. multi-line).
|
18
18
|
* Line up the values for an object across lines.
|
19
19
|
* [Online webpage](http://phrogz.net/JS/NeatJSON) for conversions and experimenting with options.
|
20
|
+
* [Lua only] Produce Lua table serialization.
|
21
|
+
|
20
22
|
|
21
23
|
## Table of Contents
|
22
24
|
|
@@ -28,6 +30,7 @@ Pretty-print your JSON in Ruby or JavaScript with more power than is provided by
|
|
28
30
|
* [TODO/Known Limitations](#todo-aka-known-limitations)
|
29
31
|
* [History](#history)
|
30
32
|
|
33
|
+
|
31
34
|
## Installation
|
32
35
|
|
33
36
|
* Ruby: `gem install neatjson`
|
@@ -44,6 +47,7 @@ require 'neatjson'
|
|
44
47
|
json = JSON.neat_generate( value, options )
|
45
48
|
~~~
|
46
49
|
|
50
|
+
|
47
51
|
**JavaScript (web)**:
|
48
52
|
|
49
53
|
~~~ html
|
@@ -61,9 +65,17 @@ const { neatJSON } = require('neatjson');
|
|
61
65
|
var json = neatJSON( value, options );
|
62
66
|
~~~
|
63
67
|
|
68
|
+
|
69
|
+
**Lua**:
|
70
|
+
|
71
|
+
~~~ lua
|
72
|
+
local neatJSON = require'neatjson'
|
73
|
+
local json = neatJSON(value, options)
|
74
|
+
~~~
|
75
|
+
|
64
76
|
## Examples
|
65
77
|
|
66
|
-
_The following are all in Ruby, but similar options apply in JavaScript._
|
78
|
+
_The following are all in Ruby, but similar options apply in JavaScript and Lua._
|
67
79
|
|
68
80
|
~~~ ruby
|
69
81
|
require 'neatjson'
|
@@ -160,7 +172,7 @@ puts JSON.neat_generate( data, opts )
|
|
160
172
|
|
161
173
|
|
162
174
|
## Options
|
163
|
-
You may pass any of the following options to `neat_generate` (Ruby) or `neatJSON` (JavaScript). **Note**: option names with underscores below use camelCase in JavaScript. For example:
|
175
|
+
You may pass any of the following options to `neat_generate` (Ruby) or `neatJSON` (JavaScript/Lua). **Note**: option names with underscores below use camelCase in JavaScript and Lua. For example:
|
164
176
|
|
165
177
|
~~~ ruby
|
166
178
|
# Ruby
|
@@ -172,6 +184,11 @@ json = JSON.neat_generate my_value, array_padding:1, after_comma:1, before_colon
|
|
172
184
|
var json = neatJSON( myValue, { arrayPadding:1, afterComma:1, beforeColonN:2, indentLast:true } );
|
173
185
|
~~~
|
174
186
|
|
187
|
+
~~~ lua
|
188
|
+
-- Lua
|
189
|
+
local json = neatJSON( myValue, { arrayPadding=1, afterComma=1, beforeColonN=2, indentLast=true } )
|
190
|
+
~~~
|
191
|
+
|
175
192
|
* `wrap` — Maximum line width before wrapping. Use `false` to never wrap, `true` to always wrap. default:`80`
|
176
193
|
* `indent` — Whitespace used to indent each level when wrapping. default:`" "` (two spaces)
|
177
194
|
* `indent_last` — Indent the closing bracket/brace for arrays and objects? default:`false`
|
@@ -194,6 +211,8 @@ var json = neatJSON( myValue, { arrayPadding:1, afterComma:1, beforeColonN:2, in
|
|
194
211
|
* `before_colon` — Shorthand to set both `before_colon_1` and `before_colon_n`. default:`0`
|
195
212
|
* `after_colon` — Shorthand to set both `after_colon_1` and `after_colon_n`. default:`0`
|
196
213
|
* `around_colon` — Shorthand to set both `before_colon` and `after_colon`. default:`0`
|
214
|
+
* `lua` — (Lua only) Output a Lua table literal instead of JSON? default:`false`
|
215
|
+
* `emptyTablesAreObjects` — (Lua only) Should `{}` in Lua become a JSON object (`{}`) or JSON array (`[]`)? default:`false` (array)
|
197
216
|
|
198
217
|
You may omit the 'value' and/or 'object' parameters in your `sort` lambda if desired. For example:
|
199
218
|
|
@@ -233,12 +252,12 @@ neatJSON( obj, { sort:function(k,v){ return countByValue[v] } } ); // so
|
|
233
252
|
// {"d":1,"a":2,"b":2,"e":3,"c":3,"f":3}
|
234
253
|
~~~
|
235
254
|
|
236
|
-
_Note that the JavaScript
|
255
|
+
_Note that the JavaScript and Lua versions of NeatJSON do not provide a mechanism for cascading sort in the same manner as Ruby._
|
237
256
|
|
238
257
|
|
239
258
|
## License & Contact
|
240
259
|
|
241
|
-
NeatJSON is copyright ©2015–
|
260
|
+
NeatJSON is copyright ©2015–2019 by Gavin Kistner and is released under
|
242
261
|
the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
243
262
|
See the LICENSE.txt file for more details.
|
244
263
|
|
@@ -250,12 +269,16 @@ For other communication you can [email the author directly](mailto:!@phrogz.net?
|
|
250
269
|
|
251
270
|
* Figure out the best way to play with custom objects that use `to_json` for their representation.
|
252
271
|
* Detect circular references.
|
253
|
-
* Possibly allow illegal JSON values like `NaN` or `Infinity`.
|
254
272
|
* Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
|
255
273
|
|
256
274
|
|
257
275
|
## HISTORY
|
258
276
|
|
277
|
+
* **v0.9** — July 29, 2019
|
278
|
+
* Add Lua version, serializing to both JSON and Lua table literals
|
279
|
+
* All languages serialize Infinity/-Infinity to JSON as `9e9999` and `-9e9999`
|
280
|
+
* All languages serialize NaN to JSON as `"NaN"`
|
281
|
+
|
259
282
|
* **v0.8.4** — May 3, 2018
|
260
283
|
* Fix issue #27: Default sorting fails with on objects with mixed keys [Ruby only]
|
261
284
|
* _Thanks Reid Beels_
|
data/lib/neatjson.rb
CHANGED
@@ -63,7 +63,11 @@ module JSON
|
|
63
63
|
when TrueClass,FalseClass then "#{indent}#{o}"
|
64
64
|
when NilClass then "#{indent}null"
|
65
65
|
when Float
|
66
|
-
if
|
66
|
+
if o.infinite?
|
67
|
+
"#{indent}#{o<0 ? "-9e9999" : "9e9999"}"
|
68
|
+
elsif o.nan?
|
69
|
+
"#{indent}\"NaN\""
|
70
|
+
elsif (o==o.to_i) && (o.to_s !~ /e/)
|
67
71
|
build[o.to_i,indent]
|
68
72
|
elsif opts[:decimals]
|
69
73
|
"#{indent}%.#{opts[:decimals]}f" % o
|
data/neatjson.gemspec
CHANGED
@@ -0,0 +1,83 @@
|
|
1
|
+
local function dump(v)
|
2
|
+
local reserved = {["and"]=1,["break"]=1,["do"]=1,["else"]=1,["elseif"]=1,["end"]=1,["false"]=1,["for"]=1,["function"]=1,["goto"]=1,["if"]=1,["in"]=1,["local"]=1,["nil"]=1,["not"]=1,["or"]=1,["repeat"]=1,["return"]=1,["then"]=1,["true"]=1,["until"]=1,["while"]=1}
|
3
|
+
local t=type(v)
|
4
|
+
if t=='table' then
|
5
|
+
local s,r={},{}
|
6
|
+
for i,v2 in ipairs(v) do s[i]=true table.insert(r,dump(v2)) end
|
7
|
+
for k,v2 in pairs(v) do
|
8
|
+
if not s[k] then
|
9
|
+
if type(k)=='string' and not reserved[k] and string.match(k,'^[%a_][%w_]*$') then
|
10
|
+
table.insert(r,k..'='..dump(v2))
|
11
|
+
else
|
12
|
+
table.insert(r,'['..dump(k)..']='..dump(v2))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
return '{'..table.concat(r,', ')..'}'
|
17
|
+
elseif t=='string' then
|
18
|
+
return string.format('%q',v)
|
19
|
+
else
|
20
|
+
return tostring(v)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
package.path = '?.lua;../lua/?.lua'
|
26
|
+
local tests = require'tests'
|
27
|
+
local neatJSON = require'neatjson'
|
28
|
+
local startTime = os.clock()
|
29
|
+
local count,pass=0,0
|
30
|
+
for _,valtest in ipairs(tests) do
|
31
|
+
local value = valtest.value
|
32
|
+
for _,test in ipairs(valtest.tests) do
|
33
|
+
local cmd = "neatJSON("..dump(value)..(test.opts and (", "..dump(test.opts)) or '')..")"
|
34
|
+
local ok,err = pcall(function()
|
35
|
+
local json, success
|
36
|
+
if test.opts then
|
37
|
+
local opts = {}
|
38
|
+
for k,v in pairs(test.opts) do opts[k]=v end
|
39
|
+
json = neatJSON(value,opts)
|
40
|
+
else
|
41
|
+
json = neatJSON(value)
|
42
|
+
end
|
43
|
+
|
44
|
+
if type(test.json)=='string' then
|
45
|
+
success = json==test.json
|
46
|
+
else
|
47
|
+
-- If it's not a string, assume it's an array of acceptable string patterns
|
48
|
+
success = false
|
49
|
+
for _,testPattern in ipairs(test.json) do
|
50
|
+
if json:match(testPattern) then
|
51
|
+
success = true
|
52
|
+
break
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
if success then
|
58
|
+
pass = pass + 1
|
59
|
+
else
|
60
|
+
local expected = type(test.json)=='string' and test.json or table.concat(test.json, ' or ')
|
61
|
+
print(cmd)
|
62
|
+
print('EXPECTED')
|
63
|
+
print(expected)
|
64
|
+
print('ACTUAL')
|
65
|
+
print(json==nil and '(nil)' or #json==0 and '(empty string)' or json)
|
66
|
+
print('')
|
67
|
+
end
|
68
|
+
end)
|
69
|
+
if not ok then
|
70
|
+
print('Error running '..cmd)
|
71
|
+
print(err)
|
72
|
+
print('')
|
73
|
+
end
|
74
|
+
count = count + 1
|
75
|
+
end
|
76
|
+
end
|
77
|
+
local elapsed = os.clock()-startTime
|
78
|
+
print(("%d/%d test%s passed in %.2fms (%.0f tests per second)"):format(
|
79
|
+
pass, count,
|
80
|
+
count==1 and '' or 's',
|
81
|
+
elapsed,
|
82
|
+
1000 * count/elapsed
|
83
|
+
))
|
data/test/tests.js
CHANGED
@@ -169,6 +169,11 @@ exports.tests = [
|
|
169
169
|
{ json:"{\n \"a\":{\n \"b\":{\n \"c\":{\n \"d\":{\n \"e\":{\n \"f\":{\n \"g\":{\n \"h\":{\n \"i\":{\n \"j\":{\n \"k\":{\n \"l\":{\n \"m\":1\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n}", opts:{wrap:1} },
|
170
170
|
]},
|
171
171
|
|
172
|
+
{value:{inf:1/0, neginf:-1/0, nan:0/0}, tests:[
|
173
|
+
{ json:'{"inf":9e9999,"nan":"NaN","neginf":-9e9999}', opts:{sort:true} },
|
174
|
+
]},
|
175
|
+
|
176
|
+
|
172
177
|
// {value:Class.new{ def to_json(*a); {a:1}.to_json(*a); end }.new, tests:[
|
173
178
|
// { json:'{ "a":1}' },
|
174
179
|
// { json:'{ "a":1}', opts:{wrap:true} },
|
data/test/tests.lua
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
return {
|
2
|
+
{value=true, tests={{json="true" }}},
|
3
|
+
{value=false, tests={{json="false" }}},
|
4
|
+
{value=nil, tests={{json="null" }}},
|
5
|
+
{value=5, tests={
|
6
|
+
{json="5"},
|
7
|
+
{json="5", opts={decimals=3}},
|
8
|
+
}},
|
9
|
+
{value=5.0, tests={
|
10
|
+
{json="5"},
|
11
|
+
{json="5", opts={lua=true}},
|
12
|
+
{json="5", opts={decimals=3}},
|
13
|
+
}},
|
14
|
+
{value=5.0001, tests={
|
15
|
+
{json="5.0001"},
|
16
|
+
{json="5.000", opts={decimals=3}},
|
17
|
+
}},
|
18
|
+
{value=4.2, tests={
|
19
|
+
{json="4.2"},
|
20
|
+
{json="4", opts={decimals=0}},
|
21
|
+
{json="4.20",opts={decimals=2}},
|
22
|
+
}},
|
23
|
+
{value=4.199, tests={{json="4.20", opts={decimals=2}}}},
|
24
|
+
{value=4.204, tests={{json="4.20", opts={decimals=2}}}},
|
25
|
+
{value=-1.9, tests={{json="-2", opts={decimals=0}}}},
|
26
|
+
{value=-2.4, tests={{json="-2", opts={decimals=0}}}},
|
27
|
+
{value=1e23, tests={{json={'^1%.0+e%+0*23$', '^1e%+0*23$'}}}},
|
28
|
+
{value=1e-9, tests={{json={'^1%.0+e%-0*9$', '^1e%-0*9$'}}}},
|
29
|
+
{value=-2.4, tests={{json="-2", opts={decimals=0}}}},
|
30
|
+
|
31
|
+
{value="foo", tests={{json="\"foo\""}}},
|
32
|
+
-- {value= :foo, tests={{json="\"foo\""}}},
|
33
|
+
{value="foo\nbar", tests={{json="\"foo\\nbar\""}}},
|
34
|
+
|
35
|
+
{value={1,2,3,4,{5,6,7,{8,9,10},11,12}}, tests={
|
36
|
+
{ json="[1,2,3,4,[5,6,7,[8,9,10],11,12]]" },
|
37
|
+
{ json="{1,2,3,4,{5,6,7,{8,9,10},11,12}}", opts={lua=true} },
|
38
|
+
{ json="[\n 1,\n 2,\n 3,\n 4,\n [5,6,7,[8,9,10],11,12]\n]", opts={wrap=30} },
|
39
|
+
{ json="[\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]", opts={wrap=20} },
|
40
|
+
{ json="[\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]", opts={wrap=true} },
|
41
|
+
{ json="[\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]", opts={wrap=true,indent="\t"} },
|
42
|
+
{ json="[1,2,3,4,[5,6,7,[8,9,10],11,12]]", opts={arrayPadding=0} },
|
43
|
+
{ json="[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts={arrayPadding=1} },
|
44
|
+
{ json="[ 1,2,3,4,[ 5,6,7,[ 8,9,10 ],11,12 ] ]", opts={arrayPadding=2} },
|
45
|
+
{ json="[1, 2, 3, 4, [5, 6, 7, [8, 9, 10], 11, 12]]", opts={afterComma=1} },
|
46
|
+
{ json="[ 1, 2, 3, 4, [ 5, 6, 7, [ 8, 9, 10 ], 11, 12 ] ]", opts={afterComma=1,arrayPadding=1} },
|
47
|
+
{ json="[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", opts={short=true,wrap=true} },
|
48
|
+
{ json="[1,\n 2,\n 3,\n 4,\n [5,\n 6,\n 7,\n [8,\n 9,\n 10],\n 11,\n 12]]", opts={short=true,wrap=true,afterComma=1} },
|
49
|
+
{ json="[ 1,\n 2,\n 3,\n 4,\n [ 5,\n 6,\n 7,\n [ 8,\n 9,\n 10 ],\n 11,\n 12 ] ]", opts={short=true,wrap=true,arrayPadding=1} },
|
50
|
+
}},
|
51
|
+
|
52
|
+
{value={1,2,3}, tests={
|
53
|
+
{ json="[1,2,3]" },
|
54
|
+
{ json="[1 ,2 ,3]", opts={beforeComma=1} },
|
55
|
+
{ json="[1 , 2 , 3]", opts={aroundComma=1} },
|
56
|
+
{ json="[\n\t1,\n\t2,\n\t3\n]", opts={wrap=true,indent="\t"} },
|
57
|
+
{ json="[\n\t1,\n\t2,\n\t3\n\t]", opts={wrap=true,indent="\t",indentLast=true} },
|
58
|
+
}},
|
59
|
+
|
60
|
+
{value={b=1,a=2}, tests={
|
61
|
+
{ json={'{"b":1,"a":2}','{"a":2,"b":1}'} },
|
62
|
+
{ json={'{b=1,a=2}','{a=2,b=1}'}, opts={lua=true} },
|
63
|
+
{ json='{"a":2,"b":1}', opts={sorted=true} },
|
64
|
+
{ json='{"a":2,"b":1}', opts={sort=true} },
|
65
|
+
{ json='{"a":2, "b":1}', opts={sorted=true,afterComma=1} },
|
66
|
+
{ json='{"a" :2,"b" :1}', opts={sorted=true,beforeColon=1} },
|
67
|
+
{ json='{"a": 2,"b": 1}', opts={sorted=true,afterColon=1} },
|
68
|
+
{ json='{"a" : 2,"b" : 1}', opts={sorted=true,beforeColon=1,afterColon=1} },
|
69
|
+
{ json='{"a" : 2, "b" : 1}', opts={sorted=true,beforeColon=1,afterColon=1,afterComma=1} },
|
70
|
+
{ json='{ "a" : 2, "b" : 1 }', opts={sorted=true,beforeColon=1,afterColon=1,afterComma=1,padding=1} },
|
71
|
+
{ json='{ "a" : 2, "b" : 1 }', opts={sorted=true,aroundColon=1,afterComma=1,objectPadding=1} },
|
72
|
+
{ json='{"a" : 2, "b" : 1}', opts={sorted=true,beforeColon=1,afterColon=1,afterComma=1,arrayPadding=1} },
|
73
|
+
{ json='{ "a" : 2, "b" : 1 }', opts={sorted=true,aroundColon=2,afterComma=1,padding=2} },
|
74
|
+
{ json='{ "a":2, "b":1 }', opts={sorted=true,afterComma=1,padding=2} },
|
75
|
+
{ json={'{"b": 1,"a": 2}','{"a": 2,"b": 1}'}, opts={afterColon1=2} },
|
76
|
+
{ json={'{"b" : 1,"a" : 2}','{"a" : 2,"b" : 1}'}, opts={aroundColon1=2} },
|
77
|
+
{ json={"{\n \"b\":1,\n \"a\":2\n}","{\n \"a\":2,\n \"b\":1\n}"}, opts={wrap=true,aroundColon1=2} },
|
78
|
+
{ json={"{\n \"b\": 1,\n \"a\": 2\n}","{\n \"a\": 2,\n \"b\": 1\n}"}, opts={wrap=true,afterColon=1} },
|
79
|
+
{ json={"{\n \"b\": 1,\n \"a\": 2\n}","{\n \"a\": 2,\n \"b\": 1\n}"}, opts={wrap=true,afterColonN=1} },
|
80
|
+
{ json="{\"a\":2,\n \"b\":1}", opts={sort=true,wrap=true,short=true} },
|
81
|
+
{ json="{\"a\": 2,\n \"b\": 1}", opts={sort=true,wrap=true,short=true,afterColon=1} },
|
82
|
+
{ json="{\"a\": 2,\n \"b\": 1}", opts={sort=true,wrap=true,short=true,afterColonN=1} },
|
83
|
+
{ json="{\"a\":2,\n \"b\":1}", opts={sort=true,wrap=true,short=true,afterColon1=1} },
|
84
|
+
}},
|
85
|
+
|
86
|
+
{value={b=1,aaa=2,cc=3}, tests={
|
87
|
+
{ json="{\n \"aaa\":2,\n \"b\":1,\n \"cc\":3\n}", opts={sort=true,wrap=true} },
|
88
|
+
{ json="{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts={sort=true,wrap=true,aligned=true} },
|
89
|
+
{ json='{"aaa":2,"b":1,"cc":3}', opts={sort=true,aligned=true} },
|
90
|
+
{ json="{\n \"aaa\":2,\n \"b\" :1,\n \"cc\" :3\n}", opts={wrap=true,aligned=true,sorted=true} },
|
91
|
+
}},
|
92
|
+
|
93
|
+
{value={a=1}, tests={
|
94
|
+
{ json='{"a":1}' },
|
95
|
+
{ json="{\n \"a\":1\n}", opts={wrap=true} },
|
96
|
+
{ json="{\n \"a\":1\n }", opts={wrap=true, indentLast=true} },
|
97
|
+
{ json="{\n \"a\":1\n }", opts={wrap=true, indentLast=true, indent=" " } },
|
98
|
+
}},
|
99
|
+
|
100
|
+
{value={b=17, a=42}, tests={
|
101
|
+
{ json="{\"a\":42,\n \"b\":17}", opts={wrap=10,sorted=true,short=true} },
|
102
|
+
{ json="{\"a\":42,\n \"b\":17}", opts={wrap=10,sort=true, short=true} },
|
103
|
+
{ json="{\n \"a\":42,\n \"b\":17\n}", opts={wrap=1,sorted=true} },
|
104
|
+
{ json="{\n \"a\":42,\n \"b\":17\n}", opts={wrap=1,sort=true} },
|
105
|
+
{ json="{\"a\":42,\"b\":17}", opts={wrap=false, sort=function(k) return k end } },
|
106
|
+
{ json="{\"b\":17,\"a\":42}", opts={wrap=false, sort=function(k,v) return v end } },
|
107
|
+
{ json="{\"a\":42,\"b\":17}", opts={wrap=false, sort=function(k,v) return -v end } },
|
108
|
+
{ json="{\"a\":42,\"b\":17}", opts={wrap=false, sort=function(k,v,o) return v==o.a and 0 or 1 end } },
|
109
|
+
{ json="{\n\"b\":17,\n\"a\":42\n}", opts={wrap=1,indent="",sort=function(k) return k=="a" and 1 or 0 end } },
|
110
|
+
{ json="{\n\"a\":42,\n\"b\":17\n}", opts={wrap=1,indent="",sort=function(k) return k=="a" and 0 or 1 end } },
|
111
|
+
}},
|
112
|
+
|
113
|
+
{value={1,{a=2},3}, tests={
|
114
|
+
{ json='[1,{"a":2},3]' },
|
115
|
+
{ json='[ 1,{ "a":2 },3 ]', opts={padding=1} },
|
116
|
+
{ json='[ 1, { "a":2 }, 3 ]', opts={padding=1,afterComma=1} },
|
117
|
+
{ json="[\n 1,\n {\n \"a\":2\n },\n 3\n]", opts={wrap=true} },
|
118
|
+
{ json="[\n 1,\n {\"a\":2},\n 3\n]", opts={wrap=10} },
|
119
|
+
{ json="[\n 1,\n {\n \"a\":2\n },\n 3\n ]", opts={wrap=true,indentLast=true} },
|
120
|
+
}},
|
121
|
+
|
122
|
+
{value={1,{a=2,b=3},4}, tests={
|
123
|
+
{ json={"[1,\n {\"a\":2,\n \"b\":3},\n 4]","[1,\n {\"b\":3,\n \"a\":2},\n 4]"}, opts={wrap=0,short=true} },
|
124
|
+
}},
|
125
|
+
|
126
|
+
{value={a=1,b={2,3,4},c=3}, tests={
|
127
|
+
{ json='{"a":1,"b":[2,3,4],"c":3}', opts={sort=true} },
|
128
|
+
{ json="{\n \"a\":1,\n \"b\":[2,3,4],\n \"c\":3\n}", opts={sort=true,wrap=10} },
|
129
|
+
{ json="{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n}", opts={sort=true,wrap=true} },
|
130
|
+
{ json="{\n \"a\":1,\n \"b\":[\n 2,\n 3,\n 4\n ],\n \"c\":3\n }", opts={sort=true,wrap=true,indentLast=true} },
|
131
|
+
}},
|
132
|
+
|
133
|
+
{value={hooo=42,whee={'yaaa','oooo','booy'},zoop="whoop"}, tests={
|
134
|
+
{ json="{\"hooo\":42,\n \"whee\":[\"yaaa\",\n \"oooo\",\n \"booy\"],\n \"zoop\":\"whoop\"}", opts={sort=true,wrap=20,short=true} },
|
135
|
+
}},
|
136
|
+
|
137
|
+
{value={ a={ {x="foo",y="jim"}, {x="bar",y="jam"} } }, tests={
|
138
|
+
{ json="{\"a\":[{\"x\":\"foo\",\n \"y\":\"jim\"},\n {\"x\":\"bar\",\n \"y\":\"jam\"}]}", opts={sort=true,wrap=true,short=true} },
|
139
|
+
}},
|
140
|
+
|
141
|
+
{value={abcdefghij={{abcdefghijklmnop={}}}}, tests={
|
142
|
+
{ json='{"abcdefghij":[{"abcdefghijklmnop":[]}]}' },
|
143
|
+
{ json='{"abcdefghij":[{"abcdefghijklmnop":{}}]}', opts={emptyTablesAreObjects=true} },
|
144
|
+
{ json='{"abcdefghij" : [{"abcdefghijklmnop" : []}]}', opts={wrap=1, short=true, aroundColonN=1} },
|
145
|
+
}},
|
146
|
+
|
147
|
+
{value={foo={}}, tests={
|
148
|
+
{ json='{"foo":[]}' },
|
149
|
+
{ json='{"foo":[]}', opts={wrap=false} },
|
150
|
+
{ json='{\n "foo":[]\n}', opts={wrap=5} },
|
151
|
+
{ json='{"foo":[]}', opts={wrap=1, short=true} },
|
152
|
+
}},
|
153
|
+
|
154
|
+
{value={"foo",{},"bar"}, tests={
|
155
|
+
{ json='[\n "foo",\n {},\n "bar"\n]', opts={wrap=1, emptyTablesAreObjects=true} },
|
156
|
+
{ json='[\n "foo",\n [],\n "bar"\n]', opts={wrap=1} },
|
157
|
+
{ json='["foo",\n {},\n "bar"]', opts={wrap=1, short=true, emptyTablesAreObjects=true} },
|
158
|
+
{ json='["foo",\n [],\n "bar"]', opts={wrap=1, short=true} },
|
159
|
+
}},
|
160
|
+
|
161
|
+
{value={"foo",{},"bar"}, tests={
|
162
|
+
{ json='[\n "foo",\n [],\n "bar"\n]', opts={wrap=1} },
|
163
|
+
{ json='["foo",\n [],\n "bar"]', opts={wrap=1, short=true} },
|
164
|
+
}},
|
165
|
+
|
166
|
+
{value={"foo",{{},{{foo={}},42}},"bar"}, tests={
|
167
|
+
{ json='["foo",\n [[],\n [{"foo":[]},\n 42]],\n "bar"]', opts={wrap=1, short=true} },
|
168
|
+
}},
|
169
|
+
|
170
|
+
{value={a={b={c={d={e={f={g={h={i={j={k={l={m=1}}}}}}}}}}}}}, tests={
|
171
|
+
{ json='{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts={wrap=false} },
|
172
|
+
{ json='{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts={wrap=1,short=true} },
|
173
|
+
{ json="{\n \"a\":{\n \"b\":{\n \"c\":{\n \"d\":{\n \"e\":{\n \"f\":{\n \"g\":{\n \"h\":{\n \"i\":{\n \"j\":{\n \"k\":{\n \"l\":{\n \"m\":1\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n}", opts={wrap=1} },
|
174
|
+
}},
|
175
|
+
|
176
|
+
{value={1,2,3,a=4,['for']=5}, tests={
|
177
|
+
{ json='{[1]=1,[2]=2,[3]=3,a=4,["for"]=5}', opts={lua=true, sort=true} }
|
178
|
+
}},
|
179
|
+
|
180
|
+
{value={inf=1/0, neginf=-1/0, nan=0/0}, tests={
|
181
|
+
{ json='{"inf":9e9999,"nan":"NaN","neginf":-9e9999}', opts={sort=true} },
|
182
|
+
{ json='{inf=1/0,nan=0/0,neginf=-1/0}', opts={sort=true, lua=true} },
|
183
|
+
}}
|
184
|
+
}
|
data/test/tests.rb
CHANGED
@@ -174,7 +174,7 @@ TESTS = [
|
|
174
174
|
{ json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:false} },
|
175
175
|
{ json:'{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"k":{"l":{"m":1}}}}}}}}}}}}}', opts:{wrap:1,short:true} },
|
176
176
|
{ json:"{\n \"a\":{\n \"b\":{\n \"c\":{\n \"d\":{\n \"e\":{\n \"f\":{\n \"g\":{\n \"h\":{\n \"i\":{\n \"j\":{\n \"k\":{\n \"l\":{\n \"m\":1\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n}", opts:{wrap:1} },
|
177
|
-
]},
|
177
|
+
]},
|
178
178
|
|
179
179
|
# Issue #27
|
180
180
|
{value:{'b'=>2, a:1}, tests:[
|
@@ -184,5 +184,9 @@ TESTS = [
|
|
184
184
|
{json:'{"a":1,"b":2}', opts:{wrap:false, sort:->(k,v,o){k.to_s}}},
|
185
185
|
{json:'{"a":1,"b":2}', opts:{wrap:false, sort:true}},
|
186
186
|
]},
|
187
|
+
|
188
|
+
{value:{inf:1.0/0, neginf:-1.0/0, nan:0.0/0}, tests:[
|
189
|
+
{ json:'{"inf":9e9999,"nan":"NaN","neginf":-9e9999}', opts:{sort:true} },
|
190
|
+
]},
|
187
191
|
]
|
188
192
|
|
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.9'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Kistner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-29 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
|
@@ -26,8 +26,10 @@ files:
|
|
26
26
|
- neatjson.gemspec
|
27
27
|
- test/large.json
|
28
28
|
- test/test_neatjson.js
|
29
|
+
- test/test_neatjson.lua
|
29
30
|
- test/test_neatjson.rb
|
30
31
|
- test/tests.js
|
32
|
+
- test/tests.lua
|
31
33
|
- test/tests.rb
|
32
34
|
homepage: http://github.com/Phrogz/NeatJSON
|
33
35
|
licenses:
|