use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project Skree by Skelril.
the class ItemSerializer method deserializeItemStack.
public static ItemStack deserializeItemStack(JsonElement element) throws IOException {
try (StringReader source = new StringReader(element.toString())) {
try (BufferedReader reader = new BufferedReader(source)) {
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setSource(() -> reader).build();
ConfigurationNode node = loader.load();
return ItemStack.builder().fromContainer(CONFIGURATION_NODE.translate(node)).build();
}
}
}
use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project Skree by Skelril.
the class ItemSerializer method serializeItemStack.
public static JsonElement serializeItemStack(ItemStack item) throws IOException {
try (StringWriter sink = new StringWriter()) {
try (BufferedWriter writer = new BufferedWriter(sink)) {
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setSink(() -> writer).build();
ConfigurationNode node = CONFIGURATION_NODE.translate(item.toContainer());
loader.save(node);
return new JsonParser().parse(sink.toString());
}
}
}
use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project AdamantineShield by Karanum.
the class DataUtils method dataToString.
public static String dataToString(DataView data) throws IOException {
StringWriter writer = new StringWriter();
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setSink(() -> new BufferedWriter(writer)).build();
ConfigurationNode result = compress(DataTranslators.CONFIGURATION_NODE.translate(data));
if (result.getChildrenMap().size() == 1 && result.getChildrenMap().containsKey(compressionMap.get("ContentVersion")))
return null;
loader.save(compress(DataTranslators.CONFIGURATION_NODE.translate(data)));
return writer.toString().replaceAll("\\n\\s*", "");
}
use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project LanternServer by LanternPowered.
the class TextTypeSerializer method deserialize.
@Override
public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
final StringWriter writer = new StringWriter();
final 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 TextSerializers.JSON.deserialize(writer.toString());
}
use of ninja.leaping.configurate.gson.GsonConfigurationLoader in project AtherysCore by Atherys-Horizons.
the class AbstractConfigurateAdapter method deserialize.
/*@Override
public void write( JsonWriter out, T value ) throws IOException {
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setLenient( true ).setIndent( 0 ).build();
try {
ConfigurationNode node = loader.createEmptyNode().setValue( TypeToken.of( clazz ), value );
StringWriter writer = new StringWriter();
loader.saveInternal( node, writer );
String json = writer.toString();
AtherysCore.getInstance().getLogger().info( "Write: " + json ); // DEBUG
out.jsonValue( json );
} catch ( ObjectMappingException e ) {
e.printStackTrace();
}
}
@Override
public T read( JsonReader in ) throws IOException {
String json = parser.parse( in ).toString();
AtherysCore.getInstance().getLogger().info( "Read: " + json ); // DEBUG
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setSource( () -> new BufferedReader( new StringReader( json ) ) ).build();
ConfigurationNode node = loader.load();
try {
return node.getValue( TypeToken.of( clazz ) );
} catch ( ObjectMappingException e ) {
e.printStackTrace();
}
return null;
}*/
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String jsonString = json.toString();
try {
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(jsonString))).build();
ConfigurationNode node = loader.load();
try {
return node.getValue(TypeToken.of(clazz));
} catch (ObjectMappingException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Aggregations