开发者

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 Jav开发者_StackOverflow社区a 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!


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.


Probably you can write a program in Java to read (deserialize) those objects, then serialize them using Protobuf (http://code.google.com/p/protobuf/) to some file, and that can be deserialized into C++ objects.


You can open the serialized files direcly and pickup the data from C++. You can see the specification but I recomend to start reading an article like The Java serialization algorithm revealed.

But the easier and for me the better way is to load the objects in Java again and Serialize it to other format, for example JSON with a java library like google-gson or others and then open back into C++ the data.


When a object is serialized to a stream it loses information about the source language, in effect becoming a blob of data that is language independent. So your problem isn't converting from Java to C++ but converting a binary blob of data to it's object representation in C++. Sure that blob was serialized in Java but that's irrelavent to the deserialization process. Now what you need to do is find out the format of the data as it is serialized and write code in C++ to deserialize it. If you're lucky the serialization may have been done using XML serialization in which case it's trivial to use an XML library on the C++ end to deserialize it.


although I don't know of any way to do what you're saying. If I were in your position, I would write a description of each object to a text/binary file. Then have C++ read the description and fill in the blanks.


As stated by sashang, the data is just a dump of the object, there is no such thing that it is owned by Java. You could absolutely deserialise the data in C++ if you understand the format. While I don't know the format, I guess it'd be better off deseralise it in Java and pass the object back to C++. Your code would be much more robust and you don't have to reinvent the wheels.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜