001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ninja.leaping.configurate.objectmapping.serialize;
018
019import com.google.common.reflect.TypeToken;
020import ninja.leaping.configurate.ConfigurationNode;
021import ninja.leaping.configurate.objectmapping.ObjectMappingException;
022import org.checkerframework.checker.nullness.qual.NonNull;
023import org.checkerframework.checker.nullness.qual.Nullable;
024
025/**
026 * Represents an object which can serialize and deserialize objects of a given type.
027 *
028 * @param <T> The type
029 */
030public interface TypeSerializer<T> {
031
032    /**
033     * Deserialize an object (of the correct type) from the given configuration node.
034     *
035     * @param type The type of return value required
036     * @param value The node containing serialized data
037     * @return An object
038     * @throws ObjectMappingException If the presented data is invalid
039     */
040    @Nullable
041    T deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException;
042
043    /**
044     * Serialize an object to the given configuration node.
045     *
046     * @param type The type of the input object
047     * @param obj The object to be serialized
048     * @param value The node to write to
049     * @throws ObjectMappingException If the object cannot be serialized
050     */
051    void serialize(@NonNull TypeToken<?> type, @Nullable T obj, @NonNull ConfigurationNode value) throws ObjectMappingException;
052
053}