Class: BSON::BSON_JAVA

Inherits:
Object
  • Object
show all
Defined in:
lib/bson/bson_java.rb

Constant Summary collapse

ENC =
Java::OrgJbson::RubyBSONEncoder.new(JRuby.runtime)
DEC =
JMongo::BSONDecoder.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deserialize(buf) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/bson/bson_java.rb', line 30

def self.deserialize(buf)
  if buf.is_a? String
    buf = ByteBuffer.new(buf.unpack("C*")) if buf
  end
  callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
  DEC.decode(buf.to_a.to_java(Java::byte), callback)
  callback.get
end

.serialize(obj, check = false, move = false) ⇒ Object



45
46
47
# File 'lib/bson/bson_java.rb', line 45

def self.serialize(obj, check=false, move=false)
  ENC.encode(obj)
end

Instance Method Details

#deserialize(buf) ⇒ Object



39
40
41
42
43
# File 'lib/bson/bson_java.rb', line 39

def deserialize(buf)
  callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
  DEC.decode(buf.to_a.to_java(Java::byte), callback)
  callback.get
end

#from_java_object(obj) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bson/bson_java.rb', line 83

def from_java_object(obj)
  case obj
   when JMongo::BasicBSONList
    obj
   when JMongo::BasicBSONObject
    hsh = {}
    obj.toMap.keySet.each do |key|
      value    = obj.get(key)
      hsh[key] = self.from_java_object(value)
    end
    hsh
   when JMongo::ObjectId
     BSON::ObjectID.from_string(obj.toStringMongod())
   else
     obj
   end
end

#to_dbobject(obj) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bson/bson_java.rb', line 49

def to_dbobject obj
  case obj
  when Array
    array_to_dblist obj
  when Hash
    hash_to_dbobject obj
  #when BSON::Binary
  #  JMongo::Binary.new(obj.subtype, obj.to_a)
  when BSON::ObjectID
    JMongo::ObjectId.new(obj.to_s)
  when Regexp
    str     = obj.source
    options = obj.options
    options_flag = 0
    options_flag |= JavaPattern::CASE_INSENSITIVE if ((options & Regexp::IGNORECASE) != 0)
    options_flag |= JavaPattern::MULTILINE  if ((options & Regexp::MULTILINE) != 0)
    #options_flag |= JavaPattern::EXTENDED if ((options & Regexp::EXTENDED) != 0)
    Java::JavaUtilRegex::Pattern.compile(str, options_flag)
  when Symbol
    JMongo::Symbol.new(obj)
  when BSON::Binary
    obj.put_int(obj.size, 0)
    b = JMongo::Binary.new(obj.subtype, obj.to_a.to_java(Java::byte))
    obj.to_a.to_java(Java::byte)
  when BSON::DBRef


  else
    # primitive value, no conversion necessary
    #puts "Un-handled class type [#{obj.class}]"
    obj
  end
end