mongo-ruby-driver/lib/bson/bson_java.rb

29 lines
907 B
Ruby
Raw Normal View History

2010-09-30 13:43:17 +00:00
include Java
module BSON
class BSON_JAVA
# TODO: Pool or cache instances of RubyBSONEncoder so that
# we don't create a new one on each call to #serialize.
2010-09-30 13:43:17 +00:00
def self.serialize(obj, check_keys=false, move_id=false)
raise InvalidDocument, "BSON_JAVA.serialize takes a Hash" unless obj.is_a?(Hash)
enc = Java::OrgJbson::RubyBSONEncoder.new(JRuby.runtime, check_keys, move_id)
2010-09-30 13:43:17 +00:00
ByteBuffer.new(enc.encode(obj))
end
def self.deserialize(buf)
dec = Java::OrgJbson::RubyBSONDecoder.new
callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
2010-09-30 13:43:17 +00:00
dec.decode(buf.to_s.to_java_bytes, callback)
callback.get
end
def self.max_bson_size
Java::OrgJbson::RubyBSONEncoder.max_bson_size(self)
end
def self.update_max_bson_size(connection)
Java::OrgJbson::RubyBSONEncoder.update_max_bson_size(self, connection)
end
2010-09-30 13:43:17 +00:00
end
end