1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.shiro.lang.io;
20
21 /**
22 * A <code>Serializer</code> converts objects to raw binary data and vice versa, enabling persistent storage
23 * of objects to files, HTTP cookies, or other mechanism.
24 * <p/>
25 * A <code>Serializer</code> should only do conversion, never change the data, such as encoding/decoding or
26 * encryption. These orthogonal concerns are handled elsewhere by Shiro, for example, via
27 * {@link org.apache.shiro.lang.codec.CodecSupport CodecSupport} and {@link org.apache.shiro.crypto.CipherService CipherService}s.
28 *
29 * @param <T> The type of the object being serialized and deserialized.
30 * @since 0.9
31 */
32 public interface Serializer<T> {
33
34 /**
35 * Converts the specified Object into a byte[] array. This byte[] array must be able to be reconstructed
36 * back into the original Object form via the {@link #deserialize(byte[]) deserialize} method.
37 *
38 * @param o the Object to convert into a byte[] array.
39 * @return a byte[] array representing the Object's state that can be restored later.
40 * @throws SerializationException if an error occurs converting the Object into a byte[] array.
41 */
42 byte[] serialize(T o) throws SerializationException;
43
44 /**
45 * Converts the specified raw byte[] array back into an original Object form. This byte[] array is expected to
46 * be the output of a previous {@link #serialize(Object) serialize} method call.
47 *
48 * @param serialized the raw data resulting from a previous {@link #serialize(Object) serialize} call.
49 * @return the Object that was previously serialized into the raw byte[] array.
50 * @throws SerializationException if an error occurs converting the raw byte[] array back into an Object.
51 */
52 T deserialize(byte[] serialized) throws SerializationException;
53 }