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.

encoding problem in json format response

#5210

My app (using 3.0.0.rc) has 'utf-8' as default encoding, when I use @model.to_json, it return invalid character(\ufffd) for all foreign characters in database (mysql). But @model.to_xml work fine with the same data.

BTW, I have confirmed that this problem doesn't exist in 3.0.0.beta3

Reported by Kelvin Xiong · July 27th, 2010 @ 09:03 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. rorra
    rorra

    I had the same problem with mysql and a utf-8 database

    ruby-1.9.2-rc2 > p = Project.create(:name => 'áéíóú')
     => #<Project id: 2, name: "áéíóú", title: nil, description: nil, start_on: nil, end_on: nil, created_at: "2010-07-27 20:41:06", updated_at: "2010-07-27 20:41:06"> 
    ruby-1.9.2-rc2 > p.to_json
     => "{\"project\":{\"created_at\":\"2010-07-27T20:41:06Z\",\"description\":null,\"end_on\":null,\"id\":2,\"name\":\"\\u00e1\\u00e9\\u00ed\\u00f3\\u00fa\",\"start_on\":null,\"title\":null,\"updated_at\":\"2010-07-27T20:41:06Z\"}}" 
    ruby-1.9.2-rc2 > Project.first.to_json
     => "{\"project\":{\"created_at\":\"2010-07-27T20:41:06Z\",\"description\":null,\"end_on\":null,\"id\":2,\"name\":\"\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\",\"start_on\":null,\"title\":null,\"updated_at\":\"2010-07-27T20:41:06Z\"}}" 
    ruby-1.9.2-rc2 >
    

    July 27th, 2010 @ 09:42 PM

  2. rorra
    rorra

    Now that I check better, my problem was not with the to_json method, but rather with mysql :P

    Being p the record before being saved, and q the record after saving

    ruby-1.9.2-rc2 > p.name
     => "á" 
    ruby-1.9.2-rc2 > q.name
     => "\xC3\xA1"
    

    July 27th, 2010 @ 09:54 PM

  3. Kelvin Xiong
    Kelvin Xiong

    rorra, did you figure out a work around for this issue?

    July 27th, 2010 @ 10:04 PM

  4. Sebastian A. Espindola
    Sebastian A. Espindola

    Everyone running on Ruby 1.9.x and using MySQL, should use the mysql2 gem.
    It forces the use of UTF-8 and converts to/from UTF-8 automatically when needed.

    July 27th, 2010 @ 10:28 PM

  5. Alex MacCaw
    Alex MacCaw
    • Tag changed from 3.0.0.rc, encoding, to_json to 3.0, 3.0.0.rc, encoding, to_json

    Switching to the mysql2 gem doesn't completely solve the problem.

    For example:

    "".to_json.encoding #<Encoding:UTF-8> [].to_json.encoding #<Encoding:US-ASCII>

    Attached is a patch which fixes the issue.

    August 30th, 2010 @ 11:31 AM

  6. Margaret
    Margaret

    Alex,
    If I have older versions for ruby and for rails, I have to update both, install the mysql2 gem and update the database data?

    The path you are talking about ... i font understand :(

    Tnk.

    Margaret

    November 29th, 2010 @ 11:23 PM

  7. rails
    rails
    • Tag changed from 3.0, 3.0.0.rc, encoding, to_json to 30, 300rc, encoding, to_json
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 1st, 2011 @ 12:00 AM

  8. rails
    rails
    • State changed from open to stale

    March 1st, 2011 @ 12:00 AM

  9. bingbing
  10. kalyan
    kalyan

    I had the same problem with rendering response as json. I'm uploading images to the database, while rendering it as json

    Eg: render :json => material.to_json

    I'm getting following error

    ArgumentError in ApiController#download_object

    redundant UTF-8 sequence

    But the same thing is working fine with to_xml. Please any one let me know what is the issue here.

    Thanks..

    May 7th, 2011 @ 11:11 AM