This project is archived and is in readonly mode.

#5210 ✓stale
Kelvin Xiong

encoding problem in json format response

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

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

Comments and changes to this ticket

  • rorra

    rorra July 27th, 2010 @ 09:42 PM

    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 >
  • rorra

    rorra July 27th, 2010 @ 09:54 PM

    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 >
     => "á" 
    ruby-1.9.2-rc2 >
     => "\xC3\xA1"
  • Kelvin Xiong

    Kelvin Xiong July 27th, 2010 @ 10:04 PM

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

  • Sebastian A. Espindola

    Sebastian A. Espindola July 27th, 2010 @ 10:28 PM

    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.

  • Alex MacCaw

    Alex MacCaw August 30th, 2010 @ 11:31 AM

    • 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.

  • Margaret

    Margaret November 29th, 2010 @ 11:23 PM

    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 :(



  • rails

    rails March 1st, 2011 @ 12:00 AM

    • 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.

  • rails

    rails March 1st, 2011 @ 12:00 AM

    • State changed from “open” to “stale”
  • bingbing
  • kalyan

    kalyan May 7th, 2011 @ 11:11 AM

    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.


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=""></a>

People watching this ticket