user775757
user775757

Reputation: 231

Binary serialization protocol

I have a requirement where i need to transfer information through the wire(binary over tcp) between 2 applications. One is in Java and the other in C++. I need a protocol implementation to transfer objects between these 2 applications. The Object classes are present in both the applications (are mapped accordingly). I just need some encoding scheme on one side which retains the Object representation on one side and can be decoded on the other side as a complete Object.

For eg,

C++ class

class Person
{
   int age;
   string name;
};

Java class

class Person
{
   int age;
   String name;
}

C++ encoding

Person p;
p.age = 20;
p.name = "somename";
char[] arr = SomeProtocolEncoder.encode(p);
socket.send(arr);

Java decoding

byte[] arr = socket.read();
SomeProtocolIntermediateObject object = SomeProtocolDecoder.decode(arr);
Person p = (Person)ReflectionUtil.get(object);    

The protocol should provide some intermediate object which maintains the object representational state so that using reflection i can get back the object later.

Upvotes: 3

Views: 4146

Answers (7)

ZiglioUK
ZiglioUK

Reputation: 2610

This project is the ultimate comparison of Java serialization protocols:

https://github.com/eishay/jvm-serializers/wiki

Some libraries also provide C++ serialization. I've personally ported Python Construct to Java. If there's some interest I'll be happy to start a conversion project to C++ and/or JavaScript!

  1. http://construct.wikispaces.com/
  2. https://github.com/ZiglioNZ/construct

Upvotes: 0

jsantander
jsantander

Reputation: 5102

What about plain old ASN.1?

It would have the advantage of being really backed by a standard (and widely used). The problem is finding a compiler/runtime for each language.

Upvotes: 1

David
David

Reputation: 11

You can check the amef protocol, an example of C++ encoding in amef would be like,

    //Create a new AMEF object
    AMEFObject *object = new AMEFObject();

    //Add a child string object
    object->addPacket("This is the Automated Message Exchange Format Object property!!","adasd");   

    //Add a child integer object
    object->addPacket(21213);

    //Add a child boolean object
    object->addPacket(true);

    AMEFObject *object2 = new AMEFObject();
    string j = "This is the property of a nested Automated Message Exchange Format Object";
    object2->addPacket(j);
    object2->addPacket(134123);
    object2->addPacket(false);

    //Add a child character object
    object2->addPacket('d');

    //Add a child AMEF Object
    object->addPacket(object2);

    //Encode the AMEF obejct
    string str = new AMEFEncoder()->encode(object,false);

Decoding in java would be like,

    byte arr = amef encoded byte array value;
    AMEFDecoder decoder = new AMEFDecoder()
    AMEFObject object1 = AMEFDecoder.decode(arr,true);

The Protocol implementation has codecs for both C++ and Java, the interesting part is it can retain object class representation in the form of name value pairs, I required a similar protocol in my last project, when i incidentally stumbled upon this protocol, i had actually modified the base library according to my requirements. Hope this helps you.

Upvotes: 1

Binil Thomas
Binil Thomas

Reputation: 13779

You might want to check out these projects and choose one:

  1. Protocol Buffers
  2. Thrift
  3. Apache Avro

Here is a Thrift-vs-PB comparison I read recently. You should also refer to this Wiki for performance comparisons between these libraries.

Upvotes: 1

Chris Eberle
Chris Eberle

Reputation: 48785

Thrift is what you're looking for. You just create a definition of the structs and methods you need to call and it does all of the heavy lifting. It's got binary protocols (optionally with zlib compression or ssl). It'll probably do your taxes but you didn't hear that from me.

Upvotes: 1

Femi
Femi

Reputation: 64700

Sounds like you want Protobufs: http://code.google.com/apis/protocolbuffers/docs/tutorials.html

Upvotes: 5

Keith
Keith

Reputation: 43024

Check out Google's protocol buffers.

Upvotes: 1

Related Questions