Created
March 2, 2010 09:45
-
-
Save nayutaya/319395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MessagePack | |
def self.pack(obj, schema = nil) | |
ostream = java.io.ByteArrayOutputStream.new | |
packer = org.msgpack.Packer.new(ostream) | |
value = self.ruby_to_java(obj) | |
if schema | |
packer.packWithSchema(value, org.msgpack.Schema.parse(schema)) | |
else | |
packer.pack(value) | |
end | |
bytes = ostream.toByteArray | |
return bytes.to_a.pack("c*") | |
end | |
def self.unpack(binary, schema = nil) | |
bytes = binary.unpack("c*").to_java(:byte) | |
istream = java.io.ByteArrayInputStream.new(bytes) | |
unpacker = org.msgpack.Unpacker.new(istream) | |
unpacker.useSchema(org.msgpack.Schema.parse(schema)) if schema | |
iterator = unpacker.iterator | |
value = iterator.next | |
return self.java_to_ruby(value) | |
end | |
def self.ruby_to_java(obj) | |
case obj | |
when Fixnum then return java.lang.Long.new(obj) | |
when Array | |
return java.util.ArrayList.new(obj.map { |item| | |
self.ruby_to_java(item) | |
}) | |
when Hash | |
return obj.inject({}) { |memo, (key, value)| | |
memo[self.ruby_to_java(key)] = self.ruby_to_java(value) | |
memo | |
} | |
else return obj | |
end | |
end | |
def self.java_to_ruby(value) | |
if value.java_kind_of?(java.util.HashMap) | |
return Hash[value.map { |key, value| | |
[self.java_to_ruby(key), self.java_to_ruby(value)] | |
}] | |
else | |
if value.respond_to?(:getClass) | |
case value.getClass.getName | |
when "java.util.Arrays$ArrayList" then return value.to_a | |
else return value | |
end | |
else | |
return value | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment