getify / JSON.minify

Simple minifier for JSON to remove comments and whitespace
http://web.archive.org/web/20100629021329/http://blog.getify.com/2010/06/json-comments/
410 stars 102 forks source link

This minifier fails when JSON doesn't end with a newline #40

Closed pkrumins closed 8 years ago

pkrumins commented 8 years ago

I was looking for JSON minifier libraries and this came up in my search. I tried it but surprisingly it wasn't working for some trivial JSON such as this:

{"a": 5}

The result it produces is still with a whitespace:

{"a": 5}

I started messing around and added a newline at the end of my trivial example and it started working!

{"a": 5}\n <- newline here

The result it produces now is correct:

{"a":5}

To make sure I wasn't missing something obvious, I edited tests.js and added these two test cases at the beginning of the file:

{
    source: "{\"a\": 5}\n",
    assert: "{\"a\":5}"
},
{
    source: "{\"a\": 5}",
    assert: "{\"a\":5}"
},

When I run the tests, the first example succeeds but the second one fails:

$ cat minify.json.js tests.js | node
Test 1 passed

[stdin]:125
                throw ("Test (" + (idx + 1) + ") failed:\n  " + res);
                                                          ^
Test (2) failed:
  {"a": 5}

I hope this was bug report was useful.

P.Krumins

pradyunsg commented 8 years ago

Thanks for spotting that. Turns out someone did the same a little earlier. :)

See #15