Search in sources :

Example 11 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException 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 12 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException 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)

Example 13 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project AtherysCore by Atherys-Horizons.

the class PluginConfig method save.

/**
 * Save the contents of the object mapper to the config file. This will override config values already-present in the file.
 */
public void save() {
    try {
        SimpleConfigurationNode out = SimpleConfigurationNode.root();
        this.configMapper.serialize(out);
        this.loader.save(out);
    } catch (ObjectMappingException | IOException e) {
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 14 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project SpongeAPI by SpongePowered.

the class TextFormatConfigSerializer method deserialize.

@Override
public TextFormat deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
    TextColor color = TextColors.NONE;
    GameRegistry registry = Sponge.getRegistry();
    String colorId = value.getNode(NODE_COLOR).getString();
    if (colorId != null) {
        color = registry.getType(TextColor.class, colorId).orElseThrow(() -> new ObjectMappingException("Color not found: " + colorId));
    }
    TextStyle style = TextStyles.NONE;
    ConfigurationNode styleNode = value.getNode(NODE_STYLE);
    for (TextStyle.Base component : registry.getAllOf(TextStyle.Base.class)) {
        if (styleNode.getNode(component.getId().toLowerCase()).getBoolean()) {
            style = style.and(component);
        }
    }
    return TextFormat.NONE.color(color).style(style);
}
Also used : TextStyle(org.spongepowered.api.text.format.TextStyle) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) GameRegistry(org.spongepowered.api.GameRegistry) TextColor(org.spongepowered.api.text.format.TextColor) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 15 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException 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)

Aggregations

ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)30 IOException (java.io.IOException)21 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)10 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)8 Map (java.util.Map)7 GsonConfigurationLoader (ninja.leaping.configurate.gson.GsonConfigurationLoader)6 Optional (java.util.Optional)5 Detection (com.ichorpowered.guardianapi.detection.Detection)4 Path (java.nio.file.Path)4 Set (java.util.Set)4 Text (org.spongepowered.api.text.Text)4 Maps (com.google.common.collect.Maps)3 ConfigurationAssignment (com.ichorpowered.guardian.content.assignment.ConfigurationAssignment)3 GuardianMapValue (com.ichorpowered.guardian.util.item.mutable.GuardianMapValue)3 GuardianValue (com.ichorpowered.guardian.util.item.mutable.GuardianValue)3 ContentContainer (com.ichorpowered.guardianapi.content.ContentContainer)3 ContentKey (com.ichorpowered.guardianapi.content.key.ContentKey)3 DetectionContentLoader (com.ichorpowered.guardianapi.detection.DetectionContentLoader)3 Key (com.ichorpowered.guardianapi.util.item.key.Key)3 MapValue (com.ichorpowered.guardianapi.util.item.value.mutable.MapValue)3