1

I have a java library, I would like to save an instance of a java object to a text file. I have tried to use all java libraries for serialization and deserialization to xml: http://karussell.wordpress.com/2009/09/03/xml-serializers-for-java/ but deserializing does not work. I have posted a question here: http://stackoverflow.com/questions/6139702/deserializing-xml-file-from-xstream but It seems that I could not get a solution for that.

I also have tried to serialize to json but deserialize from json does not work.

So, I would like to know apart of serializing to xml and json, is there any other way to do serialization and deserialization from a java object (cannot modify to add tags: @XmlRootElement(name="customer")) to text file?

Thanks in advance!

3
  • 3
    I personally think you'd be better-off sticking with xstream. It CAN be made to work, you just need to stick with it. See my comments on your original thread. Cheers. Keith... and you DO need to define your problems. "Does not work" is like going to your doctor and saying "I don't feel good" and expecting the exact treatment for your specific ailment. Commented May 28, 2011 at 10:02
  • @corlettk: On that note, I vaguely recall a recent Stack Exchange podcast where Joel, Jeff and Jon Skeet were discussing algorithms to detect questions that claim something doesn't work, while not providing any useful data. :) Commented May 28, 2011 at 10:24
  • 2
    You will find that deserializing does work for all the serialziation libraries once you work out what you are doing wrong. Just changing library isn't going to fix the problem. Commented May 28, 2011 at 10:42

2 Answers 2

6

The easiest way is probably to use the native Java serialization. It will generate a binary representation of the object, but you can encode the generated byte array with Base64 to transform it to text:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(myObject);
oos.flush();
byte[] binary = baos.toByteArray();
String text = Base64.encodeBase64String(binary); // Base64 is in the apache commons codec library
// save text to a file

Note that the object, and every object it references (recursively) must implement java.io.Serializable for this to work.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thanks but as I mentioned the object is in a java library, I can not add object with java.io.serializable.
1

You can use Gson to convert java object to Json and vice versa

Here is example from the Gson user guide.

Or may be apache digester can help.

3 Comments

I hadn't heard of Gson. Noice and SIMPLE! Thanks for the fish ;-)
Hi Falcon, I just have tried digester. it is not easy as my object contains a lot of references and child objects. It is not easy to create an xml output. Gson is very simple but does not support circular reference. Thanks!
why did you find it difficult? there is a good documentation, if you are facing specific issues about digester then may be you can post another question for digester.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.