1

I was going through a blog and one question came to my head. Is it possible to overwrite the way ObjectOutputStream is writing.

Let's say i am writing to a file out.dat i.e.

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("C:\\out.dat")));
out.writeObject(o);

When i opened the file out.dat in Notepad++, i saw the binary data. Which makes sense. What if, I would like to customize the way this data is being written. Lets say i want my out.dat file in JSON format (Thats just an example, It could be any other format). What method should i overwrite to do this?

6 Answers 6

1

You'll be able to do what you want by implementing Externalizable and overriding the writeExternal and readExternal methods. See http://download.oracle.com/javase/7/docs/platform/serialization/spec/output.html#3146 for details.

Note that it will allow customizing the output of the serialization of one object, but not the format of the whole stream. You will thus find your JSON string inside other binary data.

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

Comments

1

i think that you shouldn do it, because this format is used to keep comunication between tiers (work with distributed objects on a network). What you can do is just create a handler that store your object in a file using your pretty format.

1 Comment

Why shouldn't he do it? It isn't the most efficient transfer (but then neither is the default java serialization) but it's used on the web often enough without that being much of a problem. As long as he does it correctly, how the data looks like when being transmitted is pretty uninteresting.
1

You can make your object implement Externalizable and have full control over serialization. Use e.g. google-gson for JSON when implementing the readExternal / writeExternal methods.

Comments

1

In your scenario , where you are looking for a custom serialization mechanism , I would recommend that you implement Externalizable interface and provide implementations of methods

public void writeExternal(ObjectOutput out) throws IOException

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException

Its entirely up to you how you want to serialize in this case.

Comments

0

I'm pretty sure that java does not have internal support for serializing to JSON. In my opinion your best bet is to create a interface for a getting the JSON and have any objects you want serialized to JSON implement this interface:

public interface IJSONSerializable{
    public String getSerializedForm();
}

And then use a basic FileOutputStream to output since (as I understand it) the ObjectOutputStream is used to serialize a object to binary and does not have inherent support for JSON.

Other Thoughts

If you choose to go this way you could write a helper class for writing out things such as a property and a value.

Comments

0

Well Java itself has no built-in support for JSON serialization, but then I'm sure you can find frameworks that do that - or just write it yourself for simple classes.

So for any class you want to serialize in JSON format just overwrite

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;

Shouldn't be too hard if there's some JSON framework out there that gives you the data of one instance in string format and vice versa.

Comments

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.