Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

String.to_json creates invalid JSON

#6655

'"Now is the time..."'.to_json

should return

'&quote;Now is the time...&quote;'

but instead includes additional escapes

"\&quote;Now is the time...\&quote;"

Reported by Paul Alexander · March 30th, 2011 @ 08:46 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Josh Kalderimis
    Josh Kalderimis
    • Importance changed from to Low

    Hi Paul,

    I have tested this myself and can't seem to replicate the issue, are you able to create a failing test case to confirm the issue.

    Thanks,

    J

    March 30th, 2011 @ 09:57 PM

  2. Paul Alexander
    Paul Alexander

    Sure...but I' not sure how to prep one. I have my own application with specs and tests and I can repeatedly produce this result...is there a standard way of submitting a complete reproduction test?

    March 30th, 2011 @ 10:52 PM

  3. Paul Alexander
    Paul Alexander

    ...worked out a sample

    rails new sample

    cd sample

    rails console

    '"'.to_json # => ""\""" '"'.to_json == '"' # => false

    March 30th, 2011 @ 11:04 PM

  4. Paul Alexander
    Paul Alexander

    Sorry...forgot this was markdown

    # rails new sample
    # cd sample
    # rails console
    
    > '"'.to_json                     # => "\"\\\"\""
    > '"'.to_json == '\"'             # => false
    

    March 30th, 2011 @ 11:06 PM

  5. Paul Alexander
    Paul Alexander

    Rechecking my logic '"'.to_json should == '"\""'. Not sure where the &quote; thing is coming from. Must be from another gem.

    March 30th, 2011 @ 11:08 PM

  6. Josh Kalderimis
    Josh Kalderimis
    • State changed from new to invalid

    Hi Paul,

    I am closing this issue as invalid as I can not replicate it. I think the problem you are having is that you are relying on the console output and not puts. The console/irb uses inspect while puts will escape things correctly. eg.

    '"'.to_json
    => "\"\\\"\""
    
    puts '"'.to_json
    "\""
    

    I have tested encoding and decoding strings using ActiveSupport::JSON and Yajl separately but both work perfectly.

    s = 'here is a famous quote : "I got 99 problems but my json aint one"'
    => "here is a famous quote : \"I got 99 problems but my json aint one\""
    
    puts s
    here is a famous quote : "I got 99 problems but my json aint one"
    
    json = Yajl::Encoder.encode s
    => "\"here is a famous quote : \\\"I got 99 problems but my json aint one\\\"\""
    
    puts json
    "here is a famous quote : \"I got 99 problems but my json aint one\""
    
    Yajl::Parser.parse json
    => "here is a famous quote : \"I got 99 problems but my json aint one\""
    
    puts Yajl::Parser.parse json
    here is a famous quote : "I got 99 problems but my json aint one"
    

    Please reopen this ticket if you can replicate the issue in a failing testcase.

    Thanks,

    Josh

    March 31st, 2011 @ 03:07 PM