Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Java object into a C++ object?

My problem is like this. I have serialized binary Java objects stored in files. I need to reconstruct the objects in C++ and do further process.

I think I can deserialize the Java objects using Java, serialize again into acsii files, then load in C++ code. Also, I know it's possible to call Java function from C++ using JNI, but I don't know how to pass parameters, i.e., return the objects back to C++. But is there a better way?

Thanks!

like image 222
opmn Avatar asked Feb 25 '23 14:02

opmn


1 Answers

If you feel brave enough, you could look at the Java serialization specification and write a C++ parser for Java serialized data.

The JNI route works, but you end up with Java objects, not C++ objects. So your C++ code will basically have to make lots of calls into the VM to manipulate that data (although you could do it just to fetch the object's field and copy them into a C++ object).

If writing a Java unmarshaler in C++ sounds like to much trouble, you could do what yourself suggested and write a Java program that writes data into a format that you can easily parse from C++. But now you're writing both a marshaler (in Java) and, if you don't have one, an unmarshaler (in C++), so maybe just parsing the Java serialized format would be easier.

like image 103
vanza Avatar answered Mar 08 '23 07:03

vanza