下面列出了org.bukkit.configuration.serialization.ConfigurationSerialization#deserializeObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Parses through the input map to deal with serialized objects a la {@link
* ConfigurationSerializable}.
*
* <p>Called recursively first on Maps and Lists before passing the parsed input over to {@link
* ConfigurationSerialization#deserializeObject(java.util.Map)}. Basically this means it will
* deserialize the most nested objects FIRST and the top level object LAST.
*
* @param input the input
* @return the object that deserialize
*/
public static Object deserialize(@NotNull final Map<?, ?> input) {
final Map<String, Object> output = new LinkedHashMap<>(input.size());
for (final Map.Entry<?, ?> e : input.entrySet()) {
if (e.getValue() instanceof Map) {
output.put(e.getKey().toString(), deserialize((Map<?, ?>) e.getValue()));
} else if (e.getValue() instanceof List) {
output.put(e.getKey().toString(), deserialize((List<?>) e.getValue()));
} else {
output.put(e.getKey().toString(), e.getValue());
}
}
if (output.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
try {
return ConfigurationSerialization.deserializeObject(output);
} catch (IllegalArgumentException ex) {
throw new YAMLException("Could not deserialize object", ex);
}
}
return output;
}
@Override
public Object construct(Node node) {
if (node.isTwoStepsConstruction()) {
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
Map<?, ?> raw = (Map<?, ?>) super.construct(node);
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
Map<String, Object> typed = new LinkedHashMap<String, Object>(raw.size());
for (Map.Entry<?, ?> entry : raw.entrySet()) {
typed.put(entry.getKey().toString(), entry.getValue());
}
try {
return ConfigurationSerialization.deserializeObject(typed);
} catch (IllegalArgumentException ex) {
throw new YAMLException("Could not deserialize object", ex);
}
}
return raw;
}
@Override
public Object construct(Node node) {
if (node.isTwoStepsConstruction()) {
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
Map<?, ?> raw = (Map<?, ?>) super.construct(node);
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
Map<String, Object> typed = new LinkedHashMap<String, Object>(raw.size());
for (Map.Entry<?, ?> entry : raw.entrySet()) {
typed.put(entry.getKey().toString(), entry.getValue());
}
try {
return ConfigurationSerialization.deserializeObject(typed);
} catch (IllegalArgumentException ex) {
throw new YAMLException("Could not deserialize object", ex);
}
}
return raw;
}
@Override
public ConfigurationSerializable read(JsonReader in) {
Map<String, Object> map = this.gson.fromJson(in, RAW_OUTPUT_TYPE);
if (map == null) {
return null;
}
deserializeChildren(map);
return ConfigurationSerialization.deserializeObject(map);
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static Object deserialize(Map<String, Object> map) {
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
entry.setValue(deserialize((Map) entry.getValue()));
} else if (entry.getValue() instanceof Iterable) {
entry.setValue(convertIterable((Iterable) entry.getValue()));
} else if (entry.getValue() instanceof Number) {
entry.setValue(convertNumber((Number) entry.getValue()));
}
}
return map.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY) ? ConfigurationSerialization.deserializeObject(map) : map;
}
@Override
public ConfigurationSerializable deserialize(TypeToken<?> type, ConfigurationNode from) throws ObjectMappingException {
Map<String, Object> map = from.getValue(TYPE);
deserializeChildren(map);
return ConfigurationSerialization.deserializeObject(map);
}