4

I have two applications (one written in C++ and other in Java). I use msgpack to pack the C++ class to the binary format. Then I send this to the Java side. I wonder if I unpack 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 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's m1_, m2_ and m3_ contains proper values from C++ class Foo?
        }
        catch(IOException ex)
        {
            // ...
        }
    }

    // ...

    private String m1_;
    private String m2_;
    private int m3_;
}

I don't want to pack the Foo's members one by one because I have a lot of them.

Thanks in advance.

maverik
  • 5,508
  • 3
  • 35
  • 55

1 Answers1

0

As it is said on msgpack website

MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON.

So the answer is "yes, your java object should be properly deserialized"

  • 1
    @PetterFriberg I guess you're right, I got wrong with this one, should have been paying closer attention – Spc_555 Aug 19 '16 at 06:15
  • @VasilyAlexeev no problem, continue to review if you have doubts you can always pass by the [SOCVR room](http://chat.stackoverflow.com/rooms/41570/so-close-vote-reviewers) – Petter Friberg Aug 19 '16 at 08:04