mercredi 1 juillet 2015

YAML encoding my subclass of string as string

I'm using ruby 1.9.3

I'm having difficulty with using psych (1.3.4) when serializing a custom subclass of string.

Basically I have:

class BinaryData < String
  def my_method
    ...
  end
end

In my model I have:

class MyModel < ActiveRecord::Base
  serialize :parameters

  def set_stuff
    data = BinaryData.new('some string here that needs to be treated specially later')
    self.parameters = {'foo' => 'bar', 'data' => data}
    seld.save
   end
end

When I was using syck it would serialize something like this:

---
  foo: bar
  data: !ruby/string:BinaryData "some string here that needs to be treated specially later"

But with psych I get:

---
  foo: bar
  data: some string here that needs to be treated specially later

How can I force psych to encode it as my object?

Aucun commentaire:

Enregistrer un commentaire