001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.io;
020
021/**
022 * A <code>Serializer</code> converts objects to raw binary data and vice versa, enabling persistent storage
023 * of objects to files, HTTP cookies, or other mechanism.
024 * <p/>
025 * A <code>Serializer</code> should only do conversion, never change the data, such as encoding/decoding or
026 * encryption.  These orthogonal concerns are handled elsewhere by Shiro, for example, via
027 * {@link org.apache.shiro.codec.CodecSupport CodecSupport} and {@link org.apache.shiro.crypto.CipherService CipherService}s.
028 *
029 * @param <T> The type of the object being serialized and deserialized.
030 * @since 0.9
031 */
032public interface Serializer<T> {
033
034    /**
035     * Converts the specified Object into a byte[] array.  This byte[] array must be able to be reconstructed
036     * back into the original Object form via the {@link #deserialize(byte[]) deserialize} method.
037     *
038     * @param o the Object to convert into a byte[] array.
039     * @return a byte[] array representing the Object's state that can be restored later.
040     * @throws SerializationException if an error occurrs converting the Object into a byte[] array.
041     */
042    byte[] serialize(T o) throws SerializationException;
043
044    /**
045     * Converts the specified raw byte[] array back into an original Object form.  This byte[] array is expected to
046     * be the output of a previous {@link #serialize(Object) serialize} method call.
047     *
048     * @param serialized the raw data resulting from a previous {@link #serialize(Object) serialize} call.
049     * @return the Object that was previously serialized into the raw byte[] array.
050     * @throws SerializationException if an error occurrs converting the raw byte[] array back into an Object.
051     */
052    T deserialize(byte[] serialized) throws SerializationException;
053}