0

I have a java object which I want to send from Java Client(written in Java) using serialization or any other techniques and want to deserialize the same object in Server (written in C) ?

I tried to read the message received in C, but it is some hexadecimal values.

How can I print the same object parameters value in my server written in C.

Thanks in Advance

Ajay Yadav
  • 31
  • 1
  • 1
  • 7
  • Would using json be an option? – Paul Ogilvie Feb 06 '17 at 09:52
  • @Paul.. Hi know Json and already implemented, but there is requirement in which I don't have to use Json. And looking for some binary representation of data. The object needs to be converted into binary format and then have to send across the network and retrieve the same in Server (written in c) . – Ajay Yadav Feb 06 '17 at 09:59
  • 1
    And what would "binary format" be? You may end up with the "endian" problem (that bytes of an int are in different order on different platforms). – Paul Ogilvie Feb 06 '17 at 10:03
  • There are some libraries like cbor, messagepack etc which will take care of the same ? But I am not able to find any implementation or usage of the same. – Ajay Yadav Feb 06 '17 at 10:08
  • You already asked this question – Steve C Feb 06 '17 at 11:33

3 Answers3

0

I would probably serialise the object to JSON; this has the advantage that the serialised object is human-readable. See this answer for a guide on how to serialise to JSON, and this for help on deserialising the result in C.

Community
  • 1
  • 1
Halmackenreuter
  • 681
  • 5
  • 9
  • @Halmackenretuer.. Hi I know Json and already implemented, but there is requirement in which I don't have to use Json. And looking for some binary representation of data. The object needs to be converted into binary format and then have to send across the network and retrieve the same in Server (written in c – Ajay Yadav Feb 06 '17 at 10:00
  • 1
    Ok; you should probably add these constraints to your question. – Halmackenreuter Feb 06 '17 at 10:04
  • Can we do the packing and unpacking of object, similarly like in Python we have ? Do you have any idea about the same ? – Ajay Yadav Feb 06 '17 at 10:06
0

I have a java object which I want to send from Java Client(written in Java) using serialization or any other techniques and want to deserialize the same object in Server (written in C) ?

You can't.

C does not even know about objects and when thinking of C++ as another OO language: this has a completly different concept of objects.

So you cant "recreate" a Java object outside a JVM.

But what you most likely want is to tranfer the data your Java object holds to the C-program.

In order to do this you have to specify a protocoll that both, the Java program and the C-program understand. One possibility has been proposed by @Halmackenreuter. Others are XML or CSV. The consensus of all three is: whe convert the data into a plain text file and transfer this to the other end who knows how to read (parse) it. This process is called marshalling/unmarshalling.


there is requirement in which I don't have to use Json. And looking for some binary representation of data. The object needs to be converted into binary format and then have to send across the network and retrieve the same in Server (written in c – Ajay Yadav

Then you have to specify (or know) this binary format and create the stream of bytes to sent over the network.


somebody suggested to use ByteBuffer it to the same. But still struggling on how to use the same. – Ajay Yadav

This still means you cannot send "the java object". You have to put the objects data into the ByteBuffer in the order the protocoll expects them to be.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • I came across some libraries like cbor, messagepack which encode and decode object between different languages. But not able to find its implementation, and somebody suggested to use ByteBuffer it to the same. But still struggling on how to use the same. – Ajay Yadav Feb 06 '17 at 10:03
  • *You can't.* Sure you can. There's an entire standard describing how Java serializes objects: https://docs.oracle.com/javase/7/docs/platform/serialization/spec/serialTOC.html It might be tedious, hard to maintain, and not worth the trouble to deserialize the Java object in C, but it's certainly feasible. A text-based transfer is a lot easier. – Andrew Henle Feb 06 '17 at 11:20
  • Yet the C standard mentions "objects". But that's not exactly what OOPLs mean. – too honest for this site Feb 06 '17 at 13:24
0

It depends how are you serialize your java objects. If you're using standard Java serialization mechanism this is could be painful to deserialize it into same object in C. At least you will have two parallel hierarchy of objects in C and Java to support.

I would recommend to take a look into external serialization libraries with code generation which supports both languages, C and Java:

For example:

Vlad Bochenin
  • 3,007
  • 1
  • 20
  • 33
  • Can we do the packing and unpacking of object, similarly like in Python we have ? – Ajay Yadav Feb 06 '17 at 10:05
  • @AjayYadav I don't know how it works in Python. but on this page https://thrift.apache.org/ example with Thrift definition, Python client and Java server. In your cease you will have Thrift definition, Java as client and C as server. Main goal is to have one resource with definition of binary protocol between client and server – Vlad Bochenin Feb 06 '17 at 10:32
  • @AjayYadav comparing of different serialization libraries: http://stackoverflow.com/questions/4633611/what-are-the-key-differences-between-apache-thrift-google-protocol-buffers-mes – Vlad Bochenin Feb 06 '17 at 10:35