public static ConfigurationSerializable deserialize(Map<String, Object> map) { if (map == null) return null; for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); Object val = entry.getValue(); if (!(val instanceof Map)) continue; Map<String, Object> subMap = (Map<String, Object>) val; if (subMap.containsKey(SERIALIZED_TYPE_KEY)) { map.put(key, deserialize(subMap)); } } return ConfigurationSerialization.deserializeObject(map); }
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { Class<?> rawType = type.getRawType(); TypeAdapter<Map> mapAdapter = gson.getAdapter(Map.class); TypeAdapter<Collection> collectionAdapter = gson.getAdapter(Collection.class); if (Location.class.isAssignableFrom(rawType)) { return (TypeAdapter<T>) new LocationTypeAdapter(); } else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) { return (TypeAdapter<T>) new ConfigurationSerializableAdapter(mapAdapter); } else if (GUISignature.class.isAssignableFrom(rawType)) { return (TypeAdapter<T>) new GUISignatureTypeAdapter(mapAdapter, collectionAdapter); } else if (MaterialAndData.class.isAssignableFrom(rawType)) { return (TypeAdapter<T>) new MaterialAndDataAdapter(); } return null; }
public PrimitiveComponent() { super( Integer.class, Integer.TYPE, String.class, Float.class, Float.TYPE, Boolean.class, Boolean.TYPE, Double.class, Double.TYPE, Long.class, Long.TYPE, Short.class, Short.TYPE, Byte.class, Byte.TYPE, Character.class, Character.TYPE, ConfigurationSerializable.class); }
/** * Loads a previously serialized object from a given file using YAML. * * @param file File to load from * @param clazz Class the object should be of * @param exists Whether or not the file exists and the expensive * {@link File#exists()} operation can be skipped * @return The deserialized object, or null if the file does not exist * @throws IllegalArgumentException If the file or class is null * @throws IOException If the file cannot be read * @throws InvalidConfigurationException If the given file is not a valid * YAML configuration * @see #save(ConfigurationSerializable, File) */ @SuppressWarnings("unchecked") public static <T extends ConfigurationSerializable> T load(File file, Class<T> clazz, boolean exists) throws IOException, InvalidConfigurationException { Validate.notNull(file, "file cannot be null!"); Validate.notNull(clazz, "clazz cannot be null!"); if (! exists && ! file.exists()) return null; YamlConfiguration config = new YamlConfiguration(); config.load(file); Map<String, Object> map = config.getValues(true); return (T) ConfigurationSerialization.deserializeObject(map, clazz); }
/** * Saves a serializable object to a given file. * * @param instance Object to seriaize * @param file File to save to * @throws IllegalArgumentException If the instance or file is null * @throws IOException If the file cannot be written to * @see #load(File, Class, boolean) */ public static void save(ConfigurationSerializable instance, File file) throws IOException { Validate.notNull(instance, "instance cannot be null!"); Validate.notNull(file, "file cannot be null!"); file.delete(); file.createNewFile(); YamlConfiguration config = new YamlConfiguration(); for (Entry<String, Object> entry : instance.serialize().entrySet()) { config.set(entry.getKey(), entry.getValue()); } config.save(file); }
static void marshalSerializeable(Element baseElement, ConfigurationSerializable serializeable) { Map<String, Object> serialized = serializeable.serialize(); if (serializeable instanceof ItemMeta) { baseElement.addAttribute("itemmeta", String.valueOf(true)); } for (Entry<String, Object> entry : serialized.entrySet()) { Element entryElement = baseElement.addElement(entry.getKey()); Object value = entry.getValue(); if (value instanceof ItemMeta) { marshalSerializeable(entryElement, (ItemMeta) value); } else { serializeObject(value, entryElement); } } }
@Deprecated private static Map<String, Object> handleSerialization(Map<String, Object> map) { Map<String, Object> serialized = recreateMap(map); for (Entry<String, Object> entry : serialized.entrySet()) { if (entry.getValue() instanceof ConfigurationSerializable) { entry.setValue(serialize((ConfigurationSerializable) entry.getValue())); } else if (entry.getValue() instanceof Iterable<?>) { List<Object> newList = new ArrayList<>(); for (Object object : ((Iterable) entry.getValue())) { if (object instanceof ConfigurationSerializable) { object = serialize((ConfigurationSerializable) object); } newList.add(object); } entry.setValue(newList); } else if (entry.getValue() instanceof Map<?, ?>) { // unchecked cast here. If you're serializing to a non-standard Map you deserve ClassCastExceptions entry.setValue(handleSerialization((Map<String, Object>) entry.getValue())); } } return serialized; }
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { Class<? super T> clazz = type.getRawType(); if (!ConfigurationSerializable.class.isAssignableFrom(clazz)) { return null; } //noinspection unchecked return (TypeAdapter<T>) new Adapter(gson); }
@Override public void write(JsonWriter out, ConfigurationSerializable value) { Map<String, Object> serialized = value.serialize(); Map<String, Object> map = new LinkedHashMap<>(serialized.size() + 1); map.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(value.getClass())); map.putAll(serialized); gson.toJson(map, RAW_OUTPUT_TYPE, out); }
public static Map<String, Object> serialize(ConfigurationSerializable serializable) { Map<String, Object> serialized = new HashMap<>(); serialized.putAll(serializable.serialize()); for (Map.Entry<String, Object> entry : serialized.entrySet()) { String key = entry.getKey(); Object val = entry.getValue(); if (val instanceof ConfigurationSerializable) { serialized.put(key, serialize((ConfigurationSerializable) val)); } } serialized.put(SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass())); return serialized; }
@Override public void write(JsonWriter out, ConfigurationSerializable value) throws IOException { if (value != null) mapAdapter.write(out, serialize(value)); else out.nullValue(); }
@SuppressWarnings("unchecked") @Nullable public final static <T extends ConfigurationSerializable> T deserializeCS(final String s, final Class<T> c) { final YamlConfiguration y = new YamlConfiguration(); try { y.loadFromString(s); } catch (final InvalidConfigurationException e) { return null; } final Object o = y.get("value"); if (!c.isInstance(o)) return null; return (T) o; }
@SuppressWarnings("unchecked") @Deprecated @Nullable public final static <T extends ConfigurationSerializable> T deserializeCSOld(final String s, final Class<T> c) { final YamlConfiguration y = new YamlConfiguration(); try { y.loadFromString(s.replace("\uFEFF", "\n")); } catch (final InvalidConfigurationException e) { return null; } final Object o = y.get("value"); if (!c.isInstance(o)) return null; return (T) o; }
@SuppressWarnings({"unchecked"}) @Override @Nullable public String getID(final @NonNull Class<?> c) { if (ConfigurationSerializable.class.isAssignableFrom(c) && Classes.getSuperClassInfo(c) == Classes.getExactClassInfo(Object.class)) return configurationSerializablePrefix + ConfigurationSerialization.getAlias((Class<? extends ConfigurationSerializable>) c); return null; }
@Override @Nullable public Class<? extends ConfigurationSerializable> getClass(final @NonNull String id) { if (id.startsWith(configurationSerializablePrefix)) return ConfigurationSerialization.getClassByAlias(id.substring(configurationSerializablePrefix.length())); return null; }
void setClass(final String name, final Class<?> clazz) { if (!classes.containsKey(name)) { classes.put(name, clazz); if (ConfigurationSerializable.class.isAssignableFrom(clazz)) { Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class); ConfigurationSerialization.registerClass(serializable); } } }
private void removeClass(String name) { Class<?> clazz = classes.remove(name); try { if ((clazz != null) && (ConfigurationSerializable.class.isAssignableFrom(clazz))) { Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class); ConfigurationSerialization.unregisterClass(serializable); } } catch (NullPointerException ex) { // Boggle! // (Native methods throwing NPEs is not fun when you can't stop it before-hand) } }
/** * @see ConfigurationSerializable */ public static ConfigurationSerializable deserialize(Map<String, Object> map) { Type type = Type.valueOf((String) map.get(TYPE)); if (type == null) { throw new IllegalArgumentException(map.get(TYPE) + " is not a valid Type"); } return builder() .flicker((Boolean) map.get(FLICKER)) .trail((Boolean) map.get(TRAIL)) .withColor((Iterable<?>) map.get(COLORS)) .withFade((Iterable<?>) map.get(FADE_COLORS)) .with(type) .build(); }
@Override public Node representData(Object data) { ConfigurationSerializable serializable = (ConfigurationSerializable) data; Map<String, Object> values = new LinkedHashMap<String, Object>(); values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass())); values.putAll(serializable.serialize()); return super.representData(values); }
@Override protected Object replaceObject(Object obj) throws IOException { if (!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable)) { obj = Wrapper.newWrapper((ConfigurationSerializable) obj); } return super.replaceObject(obj); }
/** * A helper method to convert a tag directly to a MetadataValue. * * @param plugin The owning plugin * @param tag The tag to convert * @return A new MetadataValue object, or null if the tag could not be converted */ public static PersistentMetadataValue convertToMetadata(Plugin plugin, NBTBase tag) { if (plugin == null || tag == null) return null; Object converted = convert(tag); // Note that we don't currently export arrays as metadata if (converted instanceof String) { return new PersistentMetadataValue(plugin, (String)converted); } else if (converted instanceof Integer) { return new PersistentMetadataValue(plugin, (Integer)converted); } else if (converted instanceof Short) { return new PersistentMetadataValue(plugin, (Short)converted); } else if (converted instanceof Byte) { return new PersistentMetadataValue(plugin, (Byte)converted); } else if (converted instanceof Long) { return new PersistentMetadataValue(plugin, (Long)converted); } else if (converted instanceof Float) { return new PersistentMetadataValue(plugin, (Float)converted); } else if (converted instanceof Double) { return new PersistentMetadataValue(plugin, (Double)converted); } else if (converted instanceof Map) { return new PersistentMetadataValue(plugin, (Map<String, ?>)converted); } else if (converted instanceof List) { return new PersistentMetadataValue(plugin, (List<?>)converted); } else if (converted instanceof ConfigurationSerializable) { return new PersistentMetadataValue(plugin, (ConfigurationSerializable)converted); } return null; }