Search in sources :

Example 1 with GsonConfigurationLoader

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();
        }
    }
}
Also used : ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader)

Example 2 with GsonConfigurationLoader

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());
        }
    }
}
Also used : ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) JsonParser(com.google.gson.JsonParser)

Example 3 with GsonConfigurationLoader

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*", "");
}
Also used : StringWriter(java.io.StringWriter) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) BufferedWriter(java.io.BufferedWriter)

Example 4 with GsonConfigurationLoader

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());
}
Also used : StringWriter(java.io.StringWriter) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 5 with GsonConfigurationLoader

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;
}
Also used : ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) 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