Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A nested class serialization.

 
Zeev Klein
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First, I'm quite new in Java, so my questions might look a bit stupid.

I've tried to search the forum, but couldn't find the case that I'm interested in (or simply didn't understand the explanation ).

The problem:
I'm writing a Java TCP thread (RedHead linux machine) which is supposed to
send a data (originally a large number of the nested Java classes) to a C++ process running on the Unix (AIX) based server.
The data is already converted to the C++ classes representation and I have to convert these nested classes into a byte stream.
Is there a way to convert a nested class into the byte[], without a need to handle each one of the sub-classes ?

Sincerely.

Zeev
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Zeev IsNotRequired" (aka "Zeev NewInJava"),

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here.

Thank you for your prompt attention!

-Marc
 
Zeev Klein
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already did, prior to your request )
 
Zeev Klein
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, is it possible to move my topic to the Threads/Serialization section ? I don't know yet how to do it myself.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Zeev]: Already did, prior to your request

No, "NewInJava" is not even remotely similar to a real last name. And playing around like this hardly encourages anyone to help you here. Get a real last name, or your account will be closed.
 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Zeev NewInJava:
By the way, is it possible to move my topic to the Threads/Serialization section ? I don't know yet how to do it myself.


The section that you may be referring to is the "Threads and Synchronization" forum. Synchronization and Serialization are two different concepts.

Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Mr. Klein. Now on to the question:

As Henry notes, the Threads forum is not appropriate for this topic. The I/O forum might be, but I think that the possible solutions to your problem may benefit from this more general forum, so I'm going to leave the thread here, for now at least.

One approach is to make all the nested classes implement Serializable, and then use an ObjectOutputStream to write the objects to a byte stream. The problem with that, however, is that it assumes there's going to be an ObjectInputStream to read the bytes from the other end of the stream. If instead you've got something in C++doing the reading, then that won't work so well. You might study Java's serialization specification and code up something in C++ to interpret it, but that seems like a lot of work to me; there's probably an easier approach here.

You say you've already converted to a C++ representation. Are you saying these nested classes are Java classes which closely resemble some C++ structures or classes which you're trying to convert to? Can you give an example of what one of these classes looks like, and how it correlates to something on the C++ side? Are the contents of these classes always primitives? Strings? Arrays? Collections? Other more complex types?

I'm thinking that it may be possible to use reflection to make a simple version of serialization, all under your own control, that could be made to fit the C++ format which you require. This is probably not too difficult if the data are all primitives, Strings, primitive wrappers (Integer etc) and arrays or collections of these. If you've got to handle references to other types - well, it gets trickier.

Well "not too difficult" is subjective, and you say you're a Java beginner, so maybe not. But it sounds like you've got experience programming in general, just not much in Java, so I'm thinking this may work out.

It's also possible there are existing tools for this out there, but I'm not sure what they might be, or how closely they might match your requirements. There are tools to convert objects to XML representation - I like XStream. You could use something like that on the Java side, and write a simple interpreter on the C++ side. My gut feeling is that my previous idea of writing your own simple serializer in Java using reflection sounds simpler, but that's because I'm a Java guy, much more than a C++ guy. You may have the opposite idea. Or maybe someone else can suggest another existing tool that would be a better fit for you.

The other thing to consider is, just how many of these nested classes are there? (And how often will they change, or will new types be added?) While writing custom code to serialize each one doesn't sound desireable at first, it may end up easier than a general solution which might prove to be too complex for your current Java skill level. That will have to be your call, but it's something to consider.
[ September 03, 2006: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic