This project is archived and is in readonly mode.

#6655 ✓invalid
Paul Alexander

String.to_json creates invalid JSON

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

'"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;"

Comments and changes to this ticket

  • Josh Kalderimis

    Josh Kalderimis March 30th, 2011 @ 09:57 PM

    • 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

  • Paul Alexander

    Paul Alexander March 30th, 2011 @ 10:52 PM

    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?

  • Paul Alexander

    Paul Alexander March 30th, 2011 @ 11:04 PM

    ...worked out a sample

    rails new sample

    cd sample

    rails console

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

  • Paul Alexander

    Paul Alexander March 30th, 2011 @ 11:06 PM

    Sorry...forgot this was markdown

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

    Paul Alexander March 30th, 2011 @ 11:08 PM

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

  • Josh Kalderimis

    Josh Kalderimis March 31st, 2011 @ 03:07 PM

    • 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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages