Search in sources :

Example 1 with ConfigurationSerializable

use of org.bukkit.configuration.serialization.ConfigurationSerializable in project TriggerReactor by wysohn.

the class TestCommonFunctions method testSerializeLocation.

@Test
public void testSerializeLocation() {
    World vWorld = Mockito.mock(World.class);
    Location loc1 = new Location(vWorld, 1, 2, 3);
    double testX = 3.4;
    Assert.assertTrue(fn.serializeLocation(vWorld, 1, 2, 3) instanceof ConfigurationSerializable);
    Assert.assertSame(((ConfigurationSerializable) fn.serializeLocation(vWorld, 1, 2, 3)).serialize().get("world"), vWorld.getName());
}
Also used : ConfigurationSerializable(org.bukkit.configuration.serialization.ConfigurationSerializable) World(org.bukkit.World) SerializableLocation(io.github.wysohn.triggerreactor.bukkit.tools.SerializableLocation) Location(org.bukkit.Location) Test(org.junit.Test)

Example 2 with ConfigurationSerializable

use of org.bukkit.configuration.serialization.ConfigurationSerializable in project TriggerReactor by wysohn.

the class BukkitConfigurationSerializer method deserialize.

@Override
public ConfigurationSerializable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject ser = (JsonObject) json;
    // ignore Map without SERIALIZED_TYPE_KEY (they are simple map in such case)
    if (ser.get(ConfigurationSerialization.SERIALIZED_TYPE_KEY) == null)
        return null;
    Map<String, Object> map = new LinkedHashMap<>();
    map.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ser.get(ConfigurationSerialization.SERIALIZED_TYPE_KEY).getAsString());
    try {
        Map<String, Object> subs = new HashMap<>();
        ser.entrySet().forEach(entry -> {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            if (value instanceof JsonObject) {
                ConfigurationSerializable sub = deserialize(value, typeOfT, context);
                if (sub == null) {
                    // just a Map if JsonObject and is not serialized value
                    subs.put(key, context.deserialize(value, Map.class));
                } else {
                    subs.put(key, sub);
                }
            } else {
                subs.put(key, context.deserialize(value, Object.class));
            }
        });
        map.putAll(subs);
        return ConfigurationSerialization.deserializeObject(map);
    } catch (Exception ex) {
        throw new RuntimeException("Cannot deserialize " + json, ex);
    }
}
Also used : ConfigurationSerializable(org.bukkit.configuration.serialization.ConfigurationSerializable) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ConfigurationSerializable (org.bukkit.configuration.serialization.ConfigurationSerializable)2 SerializableLocation (io.github.wysohn.triggerreactor.bukkit.tools.SerializableLocation)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1 Test (org.junit.Test)1