Skip to content

Commit

Permalink
Add note about JSON/JSONB serialization changes
Browse files Browse the repository at this point in the history
The initial commit (efaa6e4) that changed this
behavior was intended to be a minor change, but ended up becoming a
large-ish breaking change within Active Record.

This is because instead of only JSON encoding `Hash`es or `Array`s in `#serialize`,
we now encode all values passed in. This is an issue if you're passing in a `String`,
that has already been transformed from a `Hash` to a `String`, since your data
is now being double encoded.

Unfortunately, the change was included in one of the v5.0.0 beta
releases, and it is too late to revert without huge ripple effects.
Thus, all we can do is update the documentation (via this commit), and
add some test coverage (coming soon in a PR) for the new behavior.

Please note that in the documentation I talk about deserialization, not
about serialization, where the actual change occurred. This is because
you won't notice any changes in serialized data until you try and
deserialize it. Also to make the change itself (confusing until you
read through everything multiple times) easier to understand.

Related rails#27788, rails#25594, rails#26101, rails#24234, rails#28292, rails#28285, rails#28285, and
probably others.

[ci skip]
  • Loading branch information
maclover7 committed Aug 17, 2017
1 parent 25ccf03 commit 835246e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions guides/source/upgrading_ruby_on_rails.md
Expand Up @@ -397,6 +397,15 @@ When using Ruby 2.4, you can preserve the timezone of the receiver when calling

ActiveSupport.to_time_preserves_timezone = false

### Changes with JSON/JSONB serialization

In Rails 5.0, how JSON/JSONB attributes are serialized and deserialized changed. Now, if
you set a column equal to a `String`, Active Record will no longer turn that string
into a `Hash`, and will instead only return the string. This is not limited to code
interacting with models, but also affects `:default` column settings in `db/schema.rb`.
It is recommended that you do not set columns equal to a `String`, but pass a `Hash`
instead, which will be converted to and from a JSON string automatically.

Upgrading from Rails 4.1 to Rails 4.2
-------------------------------------

Expand Down

0 comments on commit 835246e

Please sign in to comment.