neatjson 0.10.1 → 0.10.4
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 +21 -9
- data/lib/neatjson.rb +0 -0
- data/neatjson.gemspec +1 -1
- data/test/large.json +401 -401
- data/test/test_neatjson.lua +83 -83
- data/test/test_neatjson.rb +0 -0
- data/test/tests.js +11 -10
- data/test/tests.lua +12 -0
- data/test/tests.rb +12 -0
- metadata +5 -5
data/test/test_neatjson.lua
CHANGED
@@ -1,83 +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
|
-
))
|
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/test_neatjson.rb
CHANGED
File without changes
|
data/test/tests.js
CHANGED
@@ -211,15 +211,16 @@ exports.tests = [
|
|
211
211
|
{ json:'[\n\t1.0,\n\t2.0,\n\t3.0,\n\t{\n\t\t"bar":[4.0,5.0,6.0],\n\t\t"foo":[7.0,8.0,9.0]\n\t}\n]', opts:{wrap:20, indent:"\t", forceFloats:true} },
|
212
212
|
]},
|
213
213
|
|
214
|
-
//
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
//
|
221
|
-
|
222
|
-
|
223
|
-
|
214
|
+
// Issue #32
|
215
|
+
{value:[1,2], tests:[
|
216
|
+
{ json:'[\n 1,\n 2\n]', opts:{wrap:true, decimals:3} },
|
217
|
+
{ json:'[\n 1,\n 2\n]', opts:{wrap:true, decimals:3, trimTrailingZeros:true} },
|
218
|
+
]},
|
219
|
+
|
220
|
+
// Issue #33
|
221
|
+
{value:[1,2], tests:[
|
222
|
+
{ json:'[\n1,\n2\n]', opts:{wrap:true, indent:'', decimals:3, trimTrailingZeros:true} },
|
223
|
+
{ json:'[1,\n 2]', opts:{wrap:true, indent:'', decimals:3, trimTrailingZeros:true, short:true} },
|
224
|
+
]},
|
224
225
|
]
|
225
226
|
|
data/test/tests.lua
CHANGED
@@ -222,4 +222,16 @@ return {
|
|
222
222
|
{ json='[\n\t1.0,\n\t2.0,\n\t3.0,\n\t{\n\t\t"bar":[4.0,5.0,6.0],\n\t\t"foo":[7.0,8.0,9.0]\n\t}\n]', opts={wrap=30, sort=true, indent="\t", forceFloats=true} },
|
223
223
|
}},
|
224
224
|
|
225
|
+
-- Issue #32
|
226
|
+
{value={1,2}, tests={
|
227
|
+
{ json='[\n 1,\n 2\n]', opts={wrap=true, decimals=3} },
|
228
|
+
{ json='[\n 1,\n 2\n]', opts={wrap=true, decimals=3, trim_trailing_zeros=true} },
|
229
|
+
}},
|
230
|
+
|
231
|
+
-- Issue #33
|
232
|
+
{value={1,2}, tests={
|
233
|
+
{ json='[\n1,\n2\n]', opts={wrap=true, indent='', decimals=3, trim_trailing_zeros=true} },
|
234
|
+
{ json='[1,\n 2]', opts={wrap=true, indent='', decimals=3, trim_trailing_zeros=true, short=true} },
|
235
|
+
}},
|
236
|
+
|
225
237
|
}
|
data/test/tests.rb
CHANGED
@@ -226,5 +226,17 @@ TESTS = [
|
|
226
226
|
{ json:%Q{[\n\t1,\n\t2,\n\t3,\n\t{\n\t\t"bar":[4.0,5.0,6.0],\n\t\t"foo":[7.0,8.0,9.0]\n\t}\n]}, opts:{wrap:20, indent:"\t", force_floats_in:["foo", "bar"]} },
|
227
227
|
{ json:%Q{[\n\t1.0,\n\t2.0,\n\t3.0,\n\t{\n\t\t"bar":[4.0,5.0,6.0],\n\t\t"foo":[7.0,8.0,9.0]\n\t}\n]}, opts:{wrap:20, indent:"\t", force_floats:true} },
|
228
228
|
]},
|
229
|
+
|
230
|
+
# Issue #32
|
231
|
+
{value:[1,2], tests:[
|
232
|
+
{ json:"[\n 1,\n 2\n]", opts:{wrap:true, decimals:3} },
|
233
|
+
{ json:"[\n 1,\n 2\n]", opts:{wrap:true, decimals:3, trim_trailing_zeros:true} },
|
234
|
+
]},
|
235
|
+
|
236
|
+
# Issue #33
|
237
|
+
{value:[1,2], tests:[
|
238
|
+
{ json:"[\n1,\n2\n]", opts:{wrap:true, indent:'', decimals:3, trim_trailing_zeros:true} },
|
239
|
+
{ json:"[1,\n 2]", opts:{wrap:true, indent:'', decimals:3, trim_trailing_zeros:true, short:true} },
|
240
|
+
]},
|
229
241
|
]
|
230
242
|
|
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.10.
|
4
|
+
version: 0.10.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Kistner
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-17 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
|
@@ -35,7 +35,7 @@ homepage: http://github.com/Phrogz/NeatJSON
|
|
35
35
|
licenses:
|
36
36
|
- MIT
|
37
37
|
metadata: {}
|
38
|
-
post_install_message:
|
38
|
+
post_install_message:
|
39
39
|
rdoc_options: []
|
40
40
|
require_paths:
|
41
41
|
- lib
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
53
|
rubygems_version: 3.2.3
|
54
|
-
signing_key:
|
54
|
+
signing_key:
|
55
55
|
specification_version: 4
|
56
56
|
summary: Pretty, powerful, flexible JSON generation.
|
57
57
|
test_files: []
|