Skip to content

Instantly share code, notes, and snippets.

@tamtam180
Created June 3, 2015 14:03
Show Gist options
  • Save tamtam180/29028c011abdd36ae0ef to your computer and use it in GitHub Desktop.
Save tamtam180/29028c011abdd36ae0ef to your computer and use it in GitHub Desktop.

Revisions

  1. tamtam180 created this gist Jun 3, 2015.
    12 changes: 12 additions & 0 deletions hoge.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # coding: utf-8

    require 'json'
    require 'yajl'

    x = eval('{"logType"=>"ads", "abc"=>"\xE3\x82\xA4\xE3\x83\xB3\xE3\x82\xB9\xE3\x83\x88\xE3\x83\xBC\xE3\x83\xAB", "time"=>"2015-05-17T03:00:00Z", "tag"=>"aaa"}')
    puts x["abc"].force_encoding("ASCII-8BIT")
    puts x["abc"].encoding
    puts x

    puts Yajl.dump(x)
    puts JSON.dump(x)
    9 changes: 9 additions & 0 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    インストール
    ASCII-8BIT
    {"logType"=>"ads", "abc"=>"\xE3\x82\xA4\xE3\x83\xB3\xE3\x82\xB9\xE3\x83\x88\xE3\x83\xBC\xE3\x83\xAB", "time"=>"2015-05-17T03:00:00Z", "tag"=>"aaa"}
    {"logType":"ads","abc":"インストール","time":"2015-05-17T03:00:00Z","tag":"aaa"}
    /Library/Ruby/Gems/2.0.0/gems/json-1.8.1/lib/json/common.rb:223:in `encode': "\xE3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
    from /Library/Ruby/Gems/2.0.0/gems/json-1.8.1/lib/json/common.rb:223:in `generate'
    from /Library/Ruby/Gems/2.0.0/gems/json-1.8.1/lib/json/common.rb:223:in `generate'
    from /Library/Ruby/Gems/2.0.0/gems/json-1.8.1/lib/json/common.rb:394:in `dump'
    from hoge.rb:12:in `<main>'