001/* 002 * PermissionsEx 003 * Copyright (C) zml and PermissionsEx 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 ca.stellardrift.permissionsex.impl.config; 018 019import ca.stellardrift.permissionsex.datastore.DataStoreFactory; 020import ca.stellardrift.permissionsex.datastore.ProtoDataStore; 021import org.checkerframework.checker.nullness.qual.Nullable; 022import org.spongepowered.configurate.ConfigurationNode; 023import org.spongepowered.configurate.serialize.SerializationException; 024import org.spongepowered.configurate.serialize.TypeSerializer; 025import ca.stellardrift.permissionsex.exception.PermissionsLoadingException; 026 027import java.lang.reflect.Type; 028 029public class ProtoDataStoreSerializer implements TypeSerializer<ProtoDataStore<?>> { 030 @Override 031 public ProtoDataStore<?> deserialize(final Type type, final ConfigurationNode value) throws SerializationException { 032 final String dataStoreType = value.node("type").getString(value.key().toString()); 033 final @Nullable DataStoreFactory<?> factory = DataStoreFactory.forType(dataStoreType); 034 if (factory == null) { 035 throw new SerializationException("Unknown DataStore type " + dataStoreType); 036 } 037 try { 038 return factory.create(value.key().toString(), value); 039 } catch (PermissionsLoadingException e) { 040 throw new SerializationException(e); 041 } 042 } 043 044 @Override 045 public void serialize(final Type type, final @Nullable ProtoDataStore<?> store, final ConfigurationNode value) throws SerializationException { 046 if (store == null) { 047 value.set(null); 048 return; 049 } 050 051 store.serialize(value); 052 value.node("type").set(store.factory().name()); 053 } 054}