JSON woes

This is all kinds of annoying. JSON will apparently encode a String but not decode it? :

      [9] pry(main)> "foo".to_json
      => "\"foo\""

      [10] pry(main)> JSON.parse "\"foo\""
      JSON::ParserError: 784: unexpected token at '"foo"'
      from /home/jonea/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/json/common.rb:156:in `parse'

      [11] pry(main)> JSON.parse "foo".to_json
      JSON::ParserError: 784: unexpected token at '"foo"'
      from /home/jonea/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/json/common.rb:156:in `parse'

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Try to use quirks_mode

  JSON.parse "\"foo\"", quirks_mode: true
  => "foo"

···

On Thu, Dec 8, 2016 at 5:49 PM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

This is all kinds of annoying. JSON will apparently encode a String but
not decode it? :

[9] pry(main)> "foo".to_json
=> "\"foo\""

[10] pry(main)> JSON.parse "\"foo\""
JSON::ParserError: 784: unexpected token at '"foo"'
from /home/jonea/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/json/common.rb:156:in
`parse'

[11] pry(main)> JSON.parse "foo".to_json
JSON::ParserError: 784: unexpected token at '"foo"'
from /home/jonea/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/json/common.rb:156:in
`parse'

*Click here to view Company Information and Confidentiality Notice.*
<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

ri JSON

one may read the terse manual at least 2x

best regards
--botp

···

On Thu, Dec 8, 2016 at 10:49 PM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

This is all kinds of annoying. JSON will apparently encode a String but not
decode it? :
...
[11] pry(main)> JSON.parse "foo".to_json
JSON::ParserError: 784: unexpected token at '"foo"'

ri JSON

one may read the terse manual at least 2x

First thing I did.

I'm aware that it's documented. I said it was *annoying*. Which is a different thing :wink:

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

Try to use quirks_mode

  JSON.parse "\"foo\"", quirks_mode: true
  => "foo"

WORKS! That's brilliant. It even works in jRuby!

And, also .... not documented in ri :wink:

which is annoying. Can anyone tell me when quirks_mode was introduced? This is for a Gem, and while it literally has one (1) user -- me -- I like to be comforted by the fantasy that someone else other than me might find it useful one day; for all I know they will demand Ruby 1.9 support...

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

i see : )
you can chose bw generate and to_json, depending on use case.

.. snippet from the manual..

···

On Thu, Dec 8, 2016 at 11:31 PM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

I'm aware that it's documented. I said it was *annoying*. Which is a different thing :wink:

#--------------------------------------
JSON.generate only allows objects or arrays to be converted to JSON
syntax. to_json, however, accepts many Ruby classes even though it acts
only as a method for serialization:

  require 'json'

  1.to_json => "1"
#---------------------------------------

JSON.generate 'foo'

JSON::GeneratorError: only generation of JSON objects or arrays allowed

best regards
--botp

which is annoying. Can anyone tell me when quirks_mode was introduced? This is for a Gem, and while it literally has one (1) user -- me -- I like to be comforted by the fantasy that someone else other than me might find it useful one day; for all I know they will demand Ruby 1.9 support...

: )
playing devils adv here
fwiw, i am not a big fan of quirks mode

try

JSON.dump

JSON.dump "foo"

=> "\"foo\""

JSON.dump "foo".to_json

=> "\"\\\"foo\\\"\""

only the following four methods are good enough for me. but ymmv.
check my crude analogy. like i said, use case : )

JSON.parse <--> JSON.generate
JSON.dump <--> #to_json

best regards
--botp

···

On Thu, Dec 8, 2016 at 11:57 PM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote: