views:

67

answers:

3

I have two applications talking to each other using a queue, as of now they run exactly the same version of ruby (1.8.7), so I'm just marshaling objects back and forth; only objects from the standard lib mostly hashes, strings, time and date objects.

Right now I'm moving to Ruby 1.9.1, one app at the time, which means I'll be running one app with 1.8.7 and the other with 1.9.1 for a while. By running my tests I know Marshal will not be reliable across versions, I could use YAML, but it is much slower, JSON seems to be faster but it does not deal directly with the date/time objects.

Is there a reliable and fast way to serialize ruby objects across different versions?

A: 

I haven't tried it on ruby, but you could look at protocol buffers? Designed as a fast but portable binary format, it has a ruby port here. You would probably have to treat the generated types as a separate DTO layer, though (i.e. you map your existing data into the new types, rather than serialize your existing objects). Note that there is no inbuilt date-time support, but you could just use ticks in an epoch etc.

Marc Gravell
Thanks, checking them out now, I liked the transparency of Marshal, not having to deal with datatypes myself, but I guess there is no way around it across different versions.
Camilo
A: 

The key here is finding a common data type that you know will be represented the same across Ruby versions. The obvious choices here are storing data in an external database (the DB interface libraries will handle all the conversions) or writing the data out in a structured text format. If there's not a ton of data to work with (and the data is mostly standard types), I usually just store it as text; it takes longer to export/import but it's usually faster to write.

bta
Well, I'm sending very small messages to a queueing server back and forth, and avoiding writing to disk is very important, it does not really matter if I lost a couple of messages as far as I can process most of them quickly. Ideally a JSON based implementation that would recognize date-times would be ideal.
Camilo
Ah, in that case then writing a file would indeed be too much. Is lack of date-time support the only think keeping you from using JSON? If so, you should be able to convert Ruby `DateTime` objects into an alternate representation (and vice versa) rather quickly. I have stored Ruby date/time objects using strings (`DateTime.to_s` and `DateTime.strptime`) and as integers (`Time.to_i` and `Time.at`) when I have had to funnel them through an intermediate.
bta
A: 

Protobufs are good, but require you to pre-define your data structures, if I recall. Thrift is similar to protobufs, but has some decent code generation features.

Apple's binary property list format sounds close to what you need. It's similar to JSON in behavior, but is more compact and supports a few extra types, including datetime and unencoded binary. There are a couple ruby implementations on github.

Your best bet may be BERT. BERT is based on Erlang's binary term serialization format. It's compact, includes datatime serialization and is implemented in a dozen or so languages, including ruby.

dgrijalva