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

can't get the wrap option to work ? #5

Closed thechile closed 9 years ago

thechile commented 9 years ago

Hi,

Thanks for great gem but I'm failing to get the wrap option working. For me it doesn't seem to do anything. Maybe i miss-understood what it does.

Lets say i have:

{
      "header_timestamp" : "2015-05-14 11:05:23",
      "user"             : "Mr Who",
      "work_notes"       : "Some super super super super super super super super super super super super super super super super super super super super super line",
      "changed"          : "blah blah"
}

And i use the wrap:40 option, that it would output the formatting like below.. so on the 40th character it would wrap the long line. ( ideally on the word boundary and not chop a word in half :) )

      "work_notes"       : "Some super super super super super super super sup
                            er super super super super super super super super
                            super super super super super line",

Reading the options description, maximum line width before wrapping, isn't this what it does ?

If not, can you explain what it does and maybe this would be a good feature to add please.

Thanks again.

Phrogz commented 9 years ago

That would be nice if it was possible, but it is not. The JSON format does not allow line breaks inside a string.

Wrapping in NeatJSON can force values onto their own line, but in some cases they may exceed the wrap width. For example:

JSON.neat_generate([17,1234567890],wrap:9)

currently generates this:

[
  17,
  1234567890
]

I could make it try to honor the wrap a little better by doing something ugly like:

[
  17,
1234567890
]

but even that is more than 9 characters. There's simply no way to break 1234567890 into less than 9 characters. Though one would think it should not be the case, there's similarly no way to break a string across multiple lines in JSON.

Feel free to re-open if you can find a JSON output that is valid and meets the desire.