Reputation: 1522
I am programming a client and server in Java where the client sends a request to create a Certificate. This request contains a lot of different datatypes including byte [], the way that I am doing it now is by using ObjectStreams like :
objectStream.writeObject( new String("value of field1"));
objectStream.flush();
objectStream.writeObject( new String("value of field"));
objectStream.flush();
objectStream.writeObject( publicKey);
objectStream.flush();
...
Now I know that this is pretty bad design but I'm not quite sure how to improve it.
Would XML be a good Idea??
thanks,
Upvotes: 1
Views: 110
Reputation: 346260
Now I know that this is pretty bad design
Why do you think so? Java serialization is easy to use and works. Don't complicate things needlessly.
Do you know that you'll have to accomodate non-Java participants, or evolve the protocol without being able to update existing clients? Those would be reasons to worry about the protocol. Otherwise, do the simplest thing that works. Put all the fields in a class, make that Serializable
, and it's even simpler than what you're doing now. Make sure you keep all logic related to that in one place, and if and when you need a more portable protocol, it won't be too hard to serialize to XML or JSON instead of an ObjectOutputStream
.
Upvotes: 1
Reputation: 6718
Using Java serialization ties both server and client to Java and also makes upgrading either problematic (yes there are plenty of ways around it, but it's a frequent source of problems). XML is pretty verbose, while is ok for small amounts of data, but doesn't scale well.
I'd suggest using something like JSON, or FUDGE which let you send name value pairs in a pretty concise encoding.
Upvotes: 1
Reputation: 21241
Have you considered using RMI if the whole application is built in Java and doesn't have to include other platforms?
You still have to deal with serialization, however you could implement your application more natively and don't have to bother with things like marshalling/unmarshalling or low-level serialization.
Upvotes: 1
Reputation: 30448
You have several options:
objectStream.writeObject(myObj);
Upvotes: 2