I have a Java code, which calls into native C++ code through JNI. Today the result produced by the C++ code is returned as an XML string.
I would like to replace it with a Protocol Buffers object.
Now, I have two options after producing the PB object:
- Serialize it to a string and return it
- Try and return the object itself.
Has anyone tried the second option? Will it work? Any potential problems with it?
Thanks.
Your question is somewhat unclear about what is meant by “Protocol Buffers object“. I assume that you mean the message object, generated from .proto file by
protoc. Then i dare to say it won’t work. While the PB serialization form is indeed cross-platform and cross-language, the implementation isn’t. While you can call methods on Java objects in C(++) thanks to vast array of C functions provided by JNI, there is no reverse interface to do the same from Java (on C++ objects).But anyway, the PB objects are nothing more than an instrumented structs. The instrumentation is useful only for PB serialization. If you won’t use the serialization, PB is essentialy useless for you. If your XML has at least somewhat stable structure (PB won’t accomodate unkown structure anyway), why don’t you reflect it in plain Java object, pass it on C++ side over JNI and fill up there, using all the JNI functions mentioned?