Permalink
Browse files

Add JSON cookie coder. To be default after 1.6+

  • Loading branch information...
1 parent b7dcf64 commit 3aaab6bc1886675e4a484b3251009089fbe67834 @raggi raggi committed Feb 6, 2013
Showing with 635 additions and 0 deletions.
  1. +4 −0 lib/rack.rb
  2. +13 −0 lib/rack/session/cookie.rb
  3. +599 −0 lib/rack/utils/okjson.rb
  4. +19 −0 test/spec_session_cookie.rb
View
@@ -80,4 +80,8 @@ module Session
autoload :Pool, "rack/session/pool"
autoload :Memcache, "rack/session/memcache"
end
+
+ module Utils
+ autoload :OkJson, "rack/utils/okjson"
+ end
end
@@ -65,6 +65,19 @@ def decode(str)
::Marshal.load(super(str)) rescue nil
end
end
+
+ # N.B. Unlike other encoding methods, the contained objects must be a
+ # valid JSON composite type, either a Hash or an Array.
+ class JSON < Base64
+ def encode(obj)
+ super(::Rack::Utils::OkJson.encode(obj))
+ end
+
+ def decode(str)
+ return unless str
+ ::Rack::Utils::OkJson.decode(super(str)) rescue nil
+ end
+ end
end
# Use no encoding for session cookies
Oops, something went wrong.

0 comments on commit 3aaab6b

Please sign in to comment.