x.509
x.509

Reputation: 2235

Customizing Java Serialization

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?

Upvotes: 1

Views: 963

Answers (6)

Bhaskar
Bhaskar

Reputation: 7523

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.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

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.

Upvotes: 1

jeha
jeha

Reputation: 10710

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.

Upvotes: 1

Voo
Voo

Reputation: 30216

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.

Upvotes: 0

irobson
irobson

Reputation: 875

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.

Upvotes: 1

Ian Dallas
Ian Dallas

Reputation: 12741

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.

Upvotes: 0

Related Questions