Skip to content


Subversion checkout URL

You can clone with
Download ZIP


serialize support for json, xml etc. #8223

mrbrdo opened this Issue · 6 comments

3 participants


Is there interest for merging this into core Rails:

I've added support for json, xml and marshal in addition to yaml without introducing any breaking change. I've also added an option to gzip the data. I think it could be an useful feature (especially the json), since yaml is pretty slow anyway. I already did the tests in Test::Unit so it would be pretty easy for me to make a pull request. json, marshal and gzip support are all already built into ruby 1.9.x, so there are no external dependencies for that.

The only potential problem is that I use Ox (gem) for XML serialization. I haven't explored any built-in (in ruby or rails) option, so if there are any I could rewrite it to use that instead, so as to not introduce an external dependency. Any suggestion for this would be welcome, especially in the form of a pull request to my github project. In the worst case we could lazy load it and produce an error if it's not installed and the user tries to use xml format.

I was also thinking of providing a more extensible serialize method implementation so you could pull in new Coders without having to patch serialize (e.g. :format => :message_pack would try to load ActiveRecord::Coders::MessagePackColumn). I think it should already work like this and I don't like the current implementation much. This could potentially be a breaking change because YAMLColumn is uppercase (YAML), in this case it would be easier to just rename it to YamlColumn. Edit: I did this in the gem, special casing yaml for now (upcase).

I should point out that there is not a lot of code for adding this, and the code is really simple, so there is not much complexity added because of this.

Thanks for opinions (esp. from core team)


"Rails already supports serialization of attributes, but it can only do Yaml." Rails can do any serialization you want :metal: Please check out activerecord/lib/active_record/attribute_methods/serialization.rb#L32


I have to be honest it didn't cross my mind to just directly pass the class "JSON" or "Marshal" as the 2nd param to serialize... Well, that's embarrasing. Oh well, at least I needed the gzip bit anyway.

@mrbrdo mrbrdo closed this

By the way, docs suck for this. Really. I don't actually think I've ever seen

serialize :attr, JSON

It's not like really obvious. It's also weird why you can restrict the class when using the default (yaml) but not with others.


The problem with JSON and class restriction is that JSON do not support custom typecasting (e.g. see But you can always write you own serializer with dump and load methods and do any kind of checks.


I still think the docs of the serialize should be more clear. Theres a pile of bloated json serialization solutions floating around while it can be done so easily with serialize, but no one seems to notice.


@mrbrdo we'd welcome some docs improvements ;). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.