Topic: Customized marshalling of objects?

Is there a way to customize the Marshal#dump so that it ignores certain fields in my objects? I know I can implement the methods marshal_dump and marshal_load on my classes. But then I take full responsibility and cannot take any advantage of the default marshalling really. YAML#dump has the nice feature that you can implement the method to_yaml_properties. That makes YAML only dump the specified fields. Is there something similar for Marshal#dump?

My question is Rails-related actually. So perhaps there are other ways to do what I want. I simply want to store some of my own objects in the session. They contain some lazy-evaluated fields that are purely there for performance and 'ease-of-coding' reasons. I want to avoid marshalling those fields because they are not needed. Even worse some of them cannot be marshalled at all (e.g. a reference to a model class). How would you normally deal with this?

Re: Customized marshalling of objects?

Here's a quote from the docs:

If your class has special serialization needs (for example, if you want to serialize in some specific format), or if it contains objects that would otherwise not be serializable, you can implement your own serialization strategy by defining two methods, _dump and _load: The instance method _dump should return a String object containing all the information necessary to reconstitute objects of this class and all referenced objects up to a maximum depth given as an integer parameter (a value of -1 implies that you should disable depth checking). The class method _load should take a String and return an object of this class.

Railscasts - Free Ruby on Rails Screencasts

Re: Customized marshalling of objects?

Yes, that's actualy the pre-1.8 way of doing it. Ruby 1.8 added marshal_dump and marshal_load that take preference over _dump and _load. Strange that the docs make no reference to those. But their semantics are not much different. They just allow you to return any object instead of just strings.

The problem is the same. If I want to customize marshalling, I take over most of the responsibility for both dumping and loading. All I really want is to have Marshal#dump ignore certain fields in the same way as YAM#dump can. But it doesn't look like that's possible. hmm

Re: Customized marshalling of objects?

I probably know less about this than you do, but doesn't serializing store more information than simply attributes? This may be why they don't allow you to simply specify attributes.

If you just need to store the attributes though, how about converting to YAML then serializing that?

Railscasts - Free Ruby on Rails Screencasts

Re: Customized marshalling of objects?

I don't know much - just what I've read. tongue Yes, I did consider YAML. I just thought that the way Rails stored objects in the session demanded Marshal in some way. I haven't found much documentation about how to store your own objects in the session. Currently I have opted to remove those lazy-evaluated fields. It may not hurt performance that much anyway. But it bothers me that I am not free to design my objects they way I'd like.