1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/*
2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  this work for additional information regarding copyright ownership.
5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  (the "License"); you may not use this file except in compliance with
7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  the License.  You may obtain a copy of the License at
8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  See the License for the specific language governing permissions and
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  limitations under the License.
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.io;
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/**
2147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * Marks classes that can be serialized by {@link ObjectOutputStream} and
2247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * deserialized by {@link ObjectInputStream}.
2347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
2447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <p><strong>Warning:</strong> this interface limits how its implementing
2547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * classes can change in the future. By implementing {@code Serializable} you
2647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * expose your flexible in-memory implementation details as a rigid binary
2747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * representation. Simple code changes--like renaming private fields--are
2847f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * not safe when the changed class is serializable.
2947f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
3047f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <h3>The Serialized Form</h3>
3147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * By default, the serialization mechanism encodes an object's class name, the
3247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * names of its non-transient fields (including non-public fields), and the
3347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * values of all of those fields. The output is an opaque sequence of bytes.
3447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * Those bytes can be decoded into a new, equivalent instance as long as the
3547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * decoder has compatible versions of the originating classes.
3647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
3747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <p>Changing the class name, field names or field types breaks serialization
3847f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * compatibility and complicates interoperability between old and new versions
3947f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * of the serializable class. Adding or removing fields also complicates
4047f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * serialization between versions of a class because it requires your code to
4147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * cope with missing fields.
4247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
4347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <p>Every serializable class is assigned a version identifier called a {@code
4447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * serialVersionUID}. By default, this identifier is computed by hashing the
4547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * class declaration and its members. This identifier is included in the
4647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * serialized form so that version conflicts can be detected during
4747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * deserialization. If the local {@code serialVersionUID} differs from the
4847f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * {@code serialVersionUID} in the serialized data, deserialization will fail
4947f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * with an {@link InvalidClassException}.
5047f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
5147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <p>You can avoid this failure by declaring an explicit {@code
5247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * serialVersionUID}. Declaring an explicit {@code serialVersionUID} tells the
5347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * serialization mechanism that the class is forward and backward compatible
5447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * with all versions that share that {@code serialVersionUID}. Declaring a
5547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * {@code serialVersionUID} looks like this: <pre>   {@code
5647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
5747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *     private static final long serialVersionUID = 0L;
5847f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * }</pre>
5947f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * If you declare a {@code serialVersionUID}, you should increment it each
6047f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * time your class changes incompatibly with the previous version. Typically
6147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * this is when you add, change or remove a non-transient field.
6247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
6347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <p>You can take control of your serialized form by implementing these two
6447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * methods with these exact signatures in your serializable classes:
6547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <pre>   {@code
6647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
6747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *   private void writeObject(java.io.ObjectOutputStream out)
6847f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *       throws IOException {
6947f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *     // write 'this' to 'out'...
7047f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *   }
7147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
7247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *   private void readObject(java.io.ObjectInputStream in)
7347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *       throws IOException, ClassNotFoundException {
7447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *     // populate the fields of 'this' from the data in 'in'...
7547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *   }
7647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * }</pre>
7747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * It is impossible to maintain serialization compatibility across a class name
7847f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * change. For this reason, implementing {@code Serializable} in anonymous
7947f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * inner classes is highly discouraged: simply reordering the members in the
8047f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * file could change the generated class name and break serialization
8147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * compatibility.
8247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
8347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <p>You can exclude member fields from serialization by giving them the {@code
8447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * transient} modifier. Upon deserialization, the transient field's value will
8547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * be null, 0, or false according to its type.
8647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
8747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <h3>Implement Serializable Judiciously</h3>
8847f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * Refer to <i>Effective Java</i>'s chapter on serialization for thorough
8947f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * coverage of the serialization API. The book explains how to use this
9047f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * interface without harming your application's maintainability.
9147f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson *
9247f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <h3>Recommended Alternatives</h3>
9347f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * <strong>JSON</strong> is concise, human-readable and efficient. Android
9447f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * includes both a {@link android.util.JsonReader streaming API} and a {@link
9547f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * org.json.JSONObject tree API} to read and write JSON. Use a binding library
9647f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * like <a href="http://code.google.com/p/google-gson/">GSON</a> to read and
9747f29998eae296ad812d697c8da12e84e394fee2Jesse Wilson * write Java objects directly.
98adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
99adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic interface Serializable {
100adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
101