Search in sources :

Example 6 with GsonConfigurationLoader

use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project SpongeAPI by SpongePowered.

the class TextConfigSerializer method serialize.

@Override
public void serialize(TypeToken<?> type, Text obj, ConfigurationNode value) throws ObjectMappingException {
    String json = (String) obj.toContainer().get(Queries.JSON).get();
    GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(json))).build();
    try {
        value.setValue(gsonLoader.load());
    } catch (IOException e) {
        throw new ObjectMappingException(e);
    }
}
Also used : GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 7 with GsonConfigurationLoader

use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project SpongeAPI by SpongePowered.

the class TextConfigSerializer method deserialize.

@Override
public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
    StringWriter writer = new StringWriter();
    GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder().setIndent(0).setSink(() -> new BufferedWriter(writer)).setHeaderMode(HeaderMode.NONE).build();
    try {
        gsonLoader.save(value);
    } catch (IOException e) {
        throw new ObjectMappingException(e);
    }
    return Sponge.getDataManager().deserialize(Text.class, DataContainer.createNew().set(Queries.JSON, writer.toString())).get();
}
Also used : StringWriter(java.io.StringWriter) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) Text(org.spongepowered.api.text.Text) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 8 with GsonConfigurationLoader

use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project AtherysCore by Atherys-Horizons.

the class AbstractConfigurateAdapter method serialize.

@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setLenient(true).setIndent(0).build();
    try {
        ConfigurationNode node = loader.createEmptyNode().setValue(TypeToken.of(clazz), src);
        StringWriter writer = new StringWriter();
        loader.saveInternal(node, writer);
        String json = writer.toString();
        return parser.parse(json);
    } catch (ObjectMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : StringWriter(java.io.StringWriter) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 9 with GsonConfigurationLoader

use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project ClearMob by axle2005.

the class ConfigHandler method loadConfiguration.

public static GlobalConfig loadConfiguration() throws ObjectMappingException, IOException {
    ClearMob instance = ClearMob.getInstance();
    Path configDir = instance.getConfigDir();
    // Creates ClearMob folder in the 'plugins' directory defined in sponge global.conf
    if (!Files.exists(configDir)) {
        Files.createDirectories(configDir);
    }
    Path configFile = Paths.get(configDir + "/ClearMob.json");
    GsonConfigurationLoader configLoader = GsonConfigurationLoader.builder().setPath(configFile).build();
    ConfigurationNode configNode = configLoader.load();
    GlobalConfig config = configNode.getValue(TypeToken.of(GlobalConfig.class), new GlobalConfig());
    // Creates Clearmob.json if it does not exist
    if (!Files.exists(configFile)) {
        Files.createFile(configFile);
        instance.getLogger().info("Created default configuration!");
    }
    // Adds default entries for Options
    if (config.options.isEmpty()) {
        OptionsConfig options = new OptionsConfig();
        options.initializeDefaults();
        config.options.add(options);
    }
    // Adds default entries for Mob Limiter
    if (config.mobLimiter.isEmpty()) {
        MobLimiterConfig mobLimiter = new MobLimiterConfig();
        mobLimiter.initializeDefaults();
        config.mobLimiter.add(mobLimiter);
    }
    // Adds default entries for Compression mode
    if (config.compressEntities.isEmpty()) {
        CompressEntities compressEntities = new CompressEntities();
        compressEntities.initializeDefault();
        config.compressEntities.add(compressEntities);
    }
    // Adds default entries for Passive mode
    if (config.passive.isEmpty()) {
        PassiveConfig passive = new PassiveConfig();
        passive.initializeDefault();
        config.passive.add(passive);
    }
    // Adds default entries for Warning Message
    if (config.warning.isEmpty()) {
        WarningConfig warning = new WarningConfig();
        warning.initializeDefaults();
        config.warning.add(warning);
    }
    configNode.setValue(TypeToken.of(GlobalConfig.class), config);
    configLoader.save(configNode);
    instance.getLogger().info("Configuration loaded.");
    return config;
}
Also used : Path(java.nio.file.Path) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) ClearMob(io.github.axle2005.clearmob.ClearMob) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader)

Example 10 with GsonConfigurationLoader

use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project LanternServer by LanternPowered.

the class TextTypeSerializer method serialize.

@Override
public void serialize(TypeToken<?> type, Text obj, ConfigurationNode value) throws ObjectMappingException {
    final String json = TextSerializers.JSON.serialize(obj);
    final GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(json))).build();
    try {
        value.setValue(gsonLoader.load());
    } catch (IOException e) {
        throw new ObjectMappingException(e);
    }
}
Also used : GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

GsonConfigurationLoader (ninja.leaping.configurate.gson.GsonConfigurationLoader)10 IOException (java.io.IOException)6 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)6 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)6 StringWriter (java.io.StringWriter)4 BufferedReader (java.io.BufferedReader)3 BufferedWriter (java.io.BufferedWriter)3 StringReader (java.io.StringReader)3 JsonParser (com.google.gson.JsonParser)1 ClearMob (io.github.axle2005.clearmob.ClearMob)1 Path (java.nio.file.Path)1 Text (org.spongepowered.api.text.Text)1