Skip to content

Commit 4f30c73

Browse files
committed
Add NullSerializer to defer serialization to the ActiveRecord database adapter
1 parent 7eebb44 commit 4f30c73

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ The default assumes a `sessions` tables with columns:
3131

3232
* `id` (numeric primary key),
3333
* `session_id` (string, usually varchar; maximum length is 255), and
34-
* `data` (text or longtext; careful if your session data exceeds 65KB).
34+
* `data` (text, longtext, json or jsonb); careful if your session data exceeds
35+
65KB).
3536

3637
The `session_id` column should always be indexed for speedy lookups.
3738
Session data is marshaled to the `data` column in Base64 format.
@@ -53,11 +54,14 @@ having a separate `id` column if you don't want it. However, you must
5354
set `session.model.id = session.session_id` by hand! A before filter
5455
on ApplicationController is a good place.
5556

56-
The serializer may be one of `marshal`, `json`, or `hybrid`. `marshal` is
57-
the default and uses the built-in Marshal methods coupled with Base64
58-
encoding. `json` does what it says on the tin, using the `parse()` and
59-
`generate()` methods of the JSON module. `hybrid` will read either type
60-
but write as JSON.
57+
The serializer may be class responding to `#load(value)` and `#dump(value)`, or
58+
a symbol of `marshal`, `json`, `hybrid` or `null`. `marshal` is the default and
59+
uses the built-in Marshal methods coupled with Base64 encoding. `json` does
60+
what it says on the tin, using the `parse()` and `generate()` methods of the
61+
JSON module. `hybrid` will read either type but write as JSON. `null` will
62+
not perform serialization, leaving that up to the ActiveRecord database
63+
adapter. This allows you to take advantage of the native JSON capabilities of
64+
your database.
6165

6266
Since the default class is a simple Active Record, you get timestamps
6367
for free if you add `created_at` and `updated_at` datetime columns to

lib/active_record/session_store.rb

+13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def serializer_class
4747
JsonSerializer
4848
when :hybrid then
4949
HybridSerializer
50+
when :null then
51+
NullSerializer
5052
else
5153
self.serializer
5254
end
@@ -91,6 +93,17 @@ def self.needs_migration?(value)
9193
value.start_with?(MARSHAL_SIGNATURE)
9294
end
9395
end
96+
97+
# Defer serialization to the ActiveRecord database adapter
98+
class NullSerializer
99+
def self.load(value)
100+
value
101+
end
102+
103+
def self.dump(value)
104+
value
105+
end
106+
end
94107
end
95108
end
96109
end

0 commit comments

Comments
 (0)