Skip to content

Commit

Permalink
nested-named deserialization. fix #10
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursiveG committed May 5, 2017
1 parent 392cd85 commit 889f648
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/main/java/cat/nyaa/nyaacore/configuration/ISerializable.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,31 @@ static void pushSerializable(ConfigurationSection config, ISerializable serializ
return map;
}

/**
* Similar to {@link Map#get(Object)}
* But the key is dot-separated and the map may be nested.
* @return null if not found
*/
static Object searchMap(Map map, String key) {
if (map == null || key == null || "".equals(key)) return null;
if (!key.contains(".")) return map.get(key);
if (key.charAt(0) == '.' || key.charAt(key.length()-1) == '.') throw new IllegalArgumentException();
String prefix = "";
String[] seg = key.split("\\.");
for (int i=0;i<seg.length-1;i++) {
prefix += seg[i];
if (map.containsKey(prefix)) {
Object obj = map.get(prefix);
if (obj instanceof Map) {
return searchMap((Map)obj, key.substring(prefix.length()+1));
} else {
return null;
}
}
prefix += ".";
}
return map.get(key);
}
// deserialize ISerializable from primitive map
// Also deals with StandaloneConfig
static void popSerializable(Map<String, ?> primitiveMap, ISerializable serializable) {
Expand Down Expand Up @@ -233,29 +258,18 @@ static void popSerializable(Map<String, ?> primitiveMap, ISerializable serializa
Serializable anno = f.getAnnotation(Serializable.class);
if (anno == null || anno.manualSerialization()) continue;
f.setAccessible(true);
String cfgName = anno.name().equals("") ? f.getName() : anno.name();
//String cfgName = anno.name().equals("") ? f.getName() : anno.name();
try {
Object origValue = f.get(serializable);
//Object origValue = f.get(serializable);
Object newPrimitiveValue = null;
boolean hasValue = false;
for (String key : anno.alias()) {
if (primitiveMap.containsKey(key)) {
newPrimitiveValue = primitiveMap.get(key);
hasValue = true;
break;
}
}
if (!hasValue && primitiveMap.containsKey(f.getName())) {
newPrimitiveValue = primitiveMap.get(f.getName());
hasValue = true;
}
if (!hasValue && anno.name().length() > 0 && primitiveMap.containsKey(anno.name())) {
newPrimitiveValue = primitiveMap.get(anno.name());
hasValue = true;
}
if (!hasValue) {
continue;
newPrimitiveValue = searchMap(primitiveMap, key);
if (newPrimitiveValue != null) break;
}
if (newPrimitiveValue == null) newPrimitiveValue = searchMap(primitiveMap, f.getName());
if (newPrimitiveValue == null && anno.name().length() > 0)
newPrimitiveValue = searchMap(primitiveMap, anno.name());
if (newPrimitiveValue == null) continue;

Object newValue = null;
if (Map.class.isAssignableFrom(f.getType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class ISerializableTest {
Expand Down Expand Up @@ -59,4 +60,19 @@ public void test2() throws Exception {
assertEquals((long) (i * 10 + j), (long) ((DataObject) cls.objs.get(1)).nestedList.get(Integer.toString(i, 2)).get(j));
}
}

static class Test3Class implements ISerializable {
@Serializable(name = "object.nested.double")
DataObject.NestedObject obj = new DataObject.NestedObject();
}

@Test
public void test3() throws Exception {
YamlConfiguration cfg = new YamlConfiguration();
Test3Class cls = new Test3Class();
cls.serialize(cfg);
cls.obj = null;
cls.deserialize(cfg);
assertNotNull(cls.obj);
}
}

0 comments on commit 889f648

Please sign in to comment.