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);
}
}
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();
}
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;
}
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;
}
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);
}
}
Aggregations