On Mon, Mar 8, 2010 at 5:39 AM, Brian Candler <b.candler / pobox.com> wrote:
> Aaron D. Gifford wrote:
>> irb(main):002:0> JSON.parse(JSON.generate('\\'))
>> JSON::ParserError: 598: unexpected token at '"\\"'
>
> This is simply because JSON.parse only parses JSON *objects* and
> *arrays*, not strings or numbers.
>
>>> JSON.parse('{"foo":"bar"}')
> => {"foo"=>"bar"}
>>> JSON.parse('["foo","bar"]')
> => ["foo", "bar"]
>>> JSON.parse('"bar"')
> JSON::ParserError: 574: unexpected token at '"bar"'
>
> I use the following workaround:
>
>>> json = JSON.generate('\\')
> => "\"\\\\\""
>>> JSON.parse("[" + json + "]").first
> => "\\"
>
> HTH,
>
> Brian.

Thanks for the note, Brian.

The JSON documentation does NOT make that clear.

And that limitation is a terrible limitation.  Ugly!

I ended up rolling my own pure Ruby JSON parser that doesn't have that
work-around requirement.  It looks like I could have avoided that with
your work-around (even though it's sad that the JSON library requires
such an ugly kludge).

Aaron out.