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

Bug: Strings containing # sign are escaped unecessarily #21

Closed flyinbutrs closed 1 year ago

flyinbutrs commented 8 years ago

Input:

["#{test}"]

Pry log of failure:

[1] pry(main)> require 'neatjson'
true
[2] pry(main)> test = JSON.parse(File.open('test.json').read)
[
  [0] "\#{test}"
]
[3] pry(main)> puts test.to_json
["#{test}"]
nil
[4] pry(main)> puts JSON.neat_generate(test)
["\#{test}"]
nil

When outputting the test file with native to_json, the # isn't escaped. When outputting using neat_generate, it is.

Phrogz commented 8 years ago

Fun bug! Of course the backslash does not affect the functionality, but it is foolish and unnecessary. The offending line is:

when String,Integer       then "#{indent}#{o.inspect}"

I'll have to treat Strings with a better escaping mechanism that does only the minimum necessary. Thanks for the report.

quintuschen commented 5 years ago

If I have a JSON like the following:

{
    "name": "\#channel"
}

I will get an invalid JSON. Refer to https://jsonlint.com/, and such JSON files cannot be parsed if you try to read them.