Phrogz / NeatJSON

Pretty-print your JSON in Ruby, JS, or Lua with more power than JSON.stringify or JSON.pretty_generate
http://phrogz.net/JS/NeatJSON
MIT License
108 stars 19 forks source link

Neatjson yields [] of object contains an empty Array and {} if object contains an emtpy Hash #12

Closed bwl21 closed 8 years ago

bwl21 commented 8 years ago

The sumary says it all.

Phrogz commented 8 years ago

The summary is unclear. To file a bug report, please include actual code, what you expected as the output, and what you got instead.

I've just spent 15 minutes trying to figure out what bug you might be seeing, and I cannot reproduce anything that looks wrong. The following, for example, seems correct to me:

$ node
x = require('./neatjson.js')
o = [1,[],2,{},[{}],{a:[]},[[]],{o:{}}]
console.log( x.neatJSON( o, { wrap:1, short:true } ) )
// [1,
//  [],
//  2,
//  {},
//  [{}],
//  {"a":[]},
//  [[]],
//  {"o":{}}]
$ irb
require 'neatjson'
#=> true 

o = [ 1, [], 2, {}, [{}], {a:[]}, [[]], {o:{}} ]
puts JSON.neat_generate( o, wrap:1, short:true )
#=> [1,
#=>  [],
#=>  2,
#=>  {},
#=>  [{}],
#=>  {"a":[]},
#=>  [[]],
#=>  {"o":{}}]
bwl21 commented 8 years ago

I tried to add a test but could not make the issue occur. So it seems to be on opal. It is more effort to setup the test with opal. Nevertheless, I propose to reopen the issue until we can clarify the issue.

bwl21 commented 8 years ago

I managed to reproduce the issiue with opal-rpl

install the opal gem:

gem install opal

test> opal-repl ../lib/neatjson.rb 
>> JSON.neat_generate([ 1, [], 2, {}, [{}], {a:[]}, [[]], {o:{}} ])
=> "[]"
>> JSON.neat_generate({a:{b:[]}})
=> "[]"
>> JSON.neat_generate({a:{b:{}}})
=> "{}"
>> JSON.neat_generate({a:{b:{a:1}}})
=> "{\"a\":{\"b\":{\"a\":1}}}"
>> JSON.neat_generate({a:{b:[1]}})
=> "{\"a\":{\"b\":[1]}}"
>> 

I need to work with the opal guys to run the test suite with opal

bwl21 commented 8 years ago

I could boil it down to a pattern which reveals some different behavior between ruby and opal with respect of lambda vs proc (https://github.com/opal/opal/issues/260)

Is there a chance that you accept the PR (see commit 42bff09) such that neatjson also can be used with Opal?

Phrogz commented 8 years ago

Commit 0594535 should fix this problem for you for Opal