Skip to content

Instantly share code, notes, and snippets.

@gabu
Created December 20, 2013 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabu/8050406 to your computer and use it in GitHub Desktop.
Save gabu/8050406 to your computer and use it in GitHub Desktop.
RubyでJSONのencode/decodeの比較をしてみた結果、ArrayもHashも元に戻る。 さすがにHashのKeyのシンボルはシンボルには戻らなかった。そりゃそうだ感あるけど。
$ irb [~]
irb(main):001:0> require 'json'
=> true
irb(main):002:0> 1.to_json
=> "1"
irb(main):003:0> JSON.load(1.to_json)
=> 1
irb(main):004:0> 1 === JSON.load(1.to_json)
=> true
irb(main):005:0> "hoge" === JSON.load("hoge".to_json)
=> true
irb(main):006:0> [1, 2, 3] === JSON.load([1, 2, 3].to_json)
=> true
irb(main):007:0> {:a => 1, :b => 2} === JSON.load({:a => 1, :b => 2}.to_json)
=> false
irb(main):008:0> JSON.load({:a => 1, :b => 2}.to_json)
=> {"a"=>1, "b"=>2}
irb(main):009:0> {"a" => 1, "b" => 2} === JSON.load({"a" => 1, "b" => 2}.to_json)
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment