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;
020
021import java.net.URI;
022import java.net.URL;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026import java.util.UUID;
027import java.util.regex.Pattern;
028
029/**
030 * A number of {@link TypeSerializer}s provided by configurate.
031 */
032public class TypeSerializers {
033    static final TypeSerializerCollection DEFAULT_SERIALIZERS = new TypeSerializerCollection(null);
034
035    /**
036     * Gets the default {@link TypeSerializer}s.
037     *
038     * @return The default serializers
039     * @deprecated see {@link TypeSerializerCollection#defaults()}
040     */
041    @Deprecated
042    public static TypeSerializerCollection getDefaultSerializers() {
043        return TypeSerializerCollection.defaults();
044    }
045
046    /**
047     * Creates a new collection with the Configurate defaults as a parent
048     *
049     * @return the new collection
050     * @deprecated see {@link TypeSerializerCollection#create()}
051     */
052    @Deprecated
053    public static TypeSerializerCollection newCollection() {
054        return TypeSerializerCollection.create();
055    }
056
057    static {
058        DEFAULT_SERIALIZERS.register(TypeToken.of(URI.class), new URISerializer());
059        DEFAULT_SERIALIZERS.register(TypeToken.of(URL.class), new URLSerializer());
060        DEFAULT_SERIALIZERS.register(TypeToken.of(UUID.class), new UUIDSerializer());
061        DEFAULT_SERIALIZERS.register(input -> input.getRawType().isAnnotationPresent(ConfigSerializable.class), new AnnotatedObjectSerializer());
062        DEFAULT_SERIALIZERS.register(NumberSerializer.getPredicate(), new NumberSerializer());
063        DEFAULT_SERIALIZERS.register(TypeToken.of(Character.class), new CharSerializer());
064        DEFAULT_SERIALIZERS.register(TypeToken.of(String.class), new StringSerializer());
065        DEFAULT_SERIALIZERS.register(TypeToken.of(Boolean.class), new BooleanSerializer());
066        DEFAULT_SERIALIZERS.register(new TypeToken<Map<?, ?>>() {}, new MapSerializer());
067        DEFAULT_SERIALIZERS.register(new TypeToken<List<?>>() {}, new ListSerializer());
068        DEFAULT_SERIALIZERS.register(new TypeToken<Enum<?>>() {}, new EnumValueSerializer());
069        DEFAULT_SERIALIZERS.register(TypeToken.of(Pattern.class), new PatternSerializer());
070        DEFAULT_SERIALIZERS.register(ArraySerializer.Objects.predicate(), new ArraySerializer.Objects());
071        DEFAULT_SERIALIZERS.register(TypeToken.of(boolean[].class), new ArraySerializer.Booleans());
072        DEFAULT_SERIALIZERS.register(TypeToken.of(byte[].class), new ArraySerializer.Bytes());
073        DEFAULT_SERIALIZERS.register(TypeToken.of(char[].class), new ArraySerializer.Chars());
074        DEFAULT_SERIALIZERS.register(TypeToken.of(short[].class), new ArraySerializer.Shorts());
075        DEFAULT_SERIALIZERS.register(TypeToken.of(int[].class), new ArraySerializer.Ints());
076        DEFAULT_SERIALIZERS.register(TypeToken.of(long[].class), new ArraySerializer.Longs());
077        DEFAULT_SERIALIZERS.register(TypeToken.of(float[].class), new ArraySerializer.Floats());
078        DEFAULT_SERIALIZERS.register(TypeToken.of(double[].class), new ArraySerializer.Doubles());
079        DEFAULT_SERIALIZERS.register(new TypeToken<Set<?>>() {}, new SetSerializer());
080        DEFAULT_SERIALIZERS.register(ConfigurationNodeSerializer.TYPE, new ConfigurationNodeSerializer());
081    }
082}