Skip to content

Commit 8b76833

Browse files
authored
Merge pull request #84 from bdewater/null_serializer
Add NullSerializer for JSON database columns
2 parents c8a225b + 4f30c73 commit 8b76833

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
@@ -42,7 +42,8 @@ The default assumes a `sessions` tables with columns:
4242

4343
* `id` (numeric primary key),
4444
* `session_id` (string, usually varchar; maximum length is 255), and
45-
* `data` (text or longtext; careful if your session data exceeds 65KB).
45+
* `data` (text, longtext, json or jsonb); careful if your session data exceeds
46+
65KB).
4647

4748
The `session_id` column should always be indexed for speedy lookups.
4849
Session data is marshaled to the `data` column in Base64 format.
@@ -64,11 +65,14 @@ having a separate `id` column if you don't want it. However, you must
6465
set `session.model.id = session.session_id` by hand! A before filter
6566
on ApplicationController is a good place.
6667

67-
The serializer may be one of `marshal`, `json`, or `hybrid`. `marshal` is
68-
the default and uses the built-in Marshal methods coupled with Base64
69-
encoding. `json` does what it says on the tin, using the `parse()` and
70-
`generate()` methods of the JSON module. `hybrid` will read either type
71-
but write as JSON.
68+
The serializer may be class responding to `#load(value)` and `#dump(value)`, or
69+
a symbol of `marshal`, `json`, `hybrid` or `null`. `marshal` is the default and
70+
uses the built-in Marshal methods coupled with Base64 encoding. `json` does
71+
what it says on the tin, using the `parse()` and `generate()` methods of the
72+
JSON module. `hybrid` will read either type but write as JSON. `null` will
73+
not perform serialization, leaving that up to the ActiveRecord database
74+
adapter. This allows you to take advantage of the native JSON capabilities of
75+
your database.
7276

7377
Since the default class is a simple Active Record, you get timestamps
7478
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)