Msgpack: messaging between C ++ and java

I have two applications (one written in C ++ and the other in Java). I use msgpack to package a C ++ class in binary format. Then I post it in Java. I wonder if I unzip this message in java (using msgpack too), do I get the correct Java class object?

Consider the following example:

 // C++ class Foo { public: // some methods here... void Pack(uint8_t *data, size_t size); private: std::string m1_; std::string m2_; int m3_; public: MSGPACK_DEFINE(m1_, m2_, m3_); } void Foo::Pack(uint8_t *data, size_t size) { msgpack::sbuffer sbuf; msgpack::pack(sbuf, *this); data = sbuf.data(); size = sbuf.size(); } 

And the Java side:

 // Java public class Foo { public void Unpack(byte[] raw, Foo obj) { MessagePack msgpack = new MessagePack(); try { obj = msgpack.read(raw, Foo.class); // Does obj m1_, m2_ and m3_ contains proper values from C++ class Foo? } catch(IOException ex) { // ... } } // ... private String m1_; private String m2_; private int m3_; } 

I do not want to pack Foo members one by one because they have a lot of them.

Thanks in advance.

+4
source share
1 answer

As said on msgpack website

MessagePack is an efficient binary serialization format. This allows you to exchange data between several languages, such as JSON.

So, the answer is “yes, your java object must be properly deserialized”

0
source

All Articles