Supporting serialization of arbitrary objects #10

wants to merge 1 commit into


None yet
2 participants

bendiken commented Oct 1, 2010

I've implemented a super-simple (just one line of code) yet significant improvement in my fork:

This makes BERT.encode rather more useful by allowing it to serialize any Ruby object as long as that object responds to the #to_bert method, which should return the BERT representation of the object.

This is a feature we very much need for RDF::BERT. With this patch, it now becomes possible to do things like the following:

require 'rdf'
require 'bert'

class RDF::Literal
  def to_bert
    BERT::Tuple[:literal, value.to_s, {:language => language.to_sym}]

p BERT.encode(RDF::Literal("Hello!", :language => :en))

Much appreciated if this could be merged, it's a pretty trivial and non-invasive change but opens the door to new use cases such as the above.

Implemented #to_bert support in the BERT encoder.
With this minor change, objects of other types than just Symbol, Integer, Float, Tuple, Array, or String can be serialized as long as they provide a #to_bert method that returns the BERT representation of the object.

While waiting for this to be merged, here's a monkey-patch on top of 1.1.2 for anyone who needs this functionality right now:

# Extensions for the BERT encoder.
# @see
class BERT::Encode
  alias_method :write_any_raw_without_to_bert, :write_any_raw
  def write_any_raw(obj)
    obj = obj.to_bert if obj.respond_to?(:to_bert)

schmurfy commented Jan 9, 2011

I hope this ends up being merged as it is the first thing i tried to do with this library :p
Is there any reason/choice preventing it from being merged ?
I will go with the monkey patch for now, thanks for providing it.

bendiken commented Jan 9, 2011

@schmurfy, it appears to me that BERT is simply no longer actively maintained. We are therefore reconsidering other options such as Avro and MsgPack. I'll keep this pull request active for now given that the monkey-patch may be useful to others, but am not really expecting it to get merged anytime soon...


schmurfy commented Jan 9, 2011

@beniken, thanks for your answer, I did not checked the last commit date but I thinks it has more to do with the fact that the library is stable and does not require a lot of attention.

I improved a little your monkey patch, it may be of some use to someone else, the problem was that the result from to_bert was not passed through the convert method and thus did not work at all for me (I just used your approach but one level higher):

class BERT::Encoder
  class << self
    alias_method :convert_without_to_bert, :convert

    def convert(obj)
      obj = obj.to_bert if obj.respond_to?(:to_bert)

Bump? Could this be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment