Skip to content

Commit

Permalink
Fixed List and Map serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigal committed Nov 6, 2020
1 parent 7622502 commit 462d72a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ maven {
url = 'https://repo.mikigal.pl/releases'
}
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.0'
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.0.1'
```

#### Maven
Expand All @@ -30,7 +30,7 @@ compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.0'
<dependency>
<groupId>pl.mikigal</groupId>
<artifactId>ConfigAPI</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'pl.mikigal'
version '1.0'
version '1.0.1'

publishing {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ public class UniversalListSerializer extends Serializer<List> {
protected void saveObject(String path, List object, BukkitConfiguration configuration) {
Class<?> generic = TypeUtils.getListGeneric(object);
Serializer<?> serializer = Serializers.of(generic);
if (serializer == null) {
if (serializer == null && !TypeUtils.isSimpleType(generic)) {
throw new MissingSerializerException(generic);
}

configuration.set(path + ".type", generic.getName());
for (int i = 0; i < object.size(); i++) {
if (serializer == null) {
configuration.set(path + "." + i, object.get(i));
continue;
}

serializer.serialize(path + "." + i, object.get(i), configuration);
}
}
Expand All @@ -41,12 +46,12 @@ public List<?> deserialize(String path, BukkitConfiguration configuration) {

Serializer<?> serializer = Serializers.of(serializerClass);

if (serializer == null) {
try {
try {
if (serializer == null && !TypeUtils.isSimpleType(Class.forName(serializerClass))) {
throw new MissingSerializerException(Class.forName(serializerClass));
} catch (ClassNotFoundException e) {
throw new MissingSerializerException("Could not find class " + serializerClass);
}
} catch (ClassNotFoundException e) {
throw new MissingSerializerException("Could not find class " + serializerClass);
}

List list = new ArrayList<>();
Expand All @@ -55,6 +60,11 @@ public List<?> deserialize(String path, BukkitConfiguration configuration) {
continue;
}

if (serializer == null) {
list.add(configuration.get(path + "." + index));
continue;
}

list.add(serializer.deserialize(path + "." + index, configuration));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ public class UniversalMapSerializer extends Serializer<Map> {
@Override
protected void saveObject(String path, Map object, BukkitConfiguration configuration) {
Class<?> generic = TypeUtils.getMapGeneric(object)[1];
if (TypeUtils.isSimpleType(generic)) {
for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
configuration.set(path + "." + entry.getKey(), entry.getValue());
}

return;
}

Serializer<?> serializer = Serializers.of(generic);
if (serializer == null) {
if (serializer == null && !TypeUtils.isSimpleType(generic)) {
throw new MissingSerializerException(generic);
}

configuration.set(path + ".type", generic.getName());
for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
if (serializer == null) {
configuration.set(path + "." + entry.getKey(), entry.getValue());
continue;
}

serializer.serialize(path + "." + entry.getKey(), entry.getValue(), configuration);
}
}
Expand Down Expand Up @@ -58,12 +55,12 @@ protected void saveObject(String path, Map object, BukkitConfiguration configura

Serializer<?> serializer = Serializers.of(serializerClass);

if (serializer == null) {
try {
try {
if (serializer == null && !TypeUtils.isSimpleType(Class.forName(serializerClass))) {
throw new MissingSerializerException(Class.forName(serializerClass));
} catch (ClassNotFoundException e) {
throw new MissingSerializerException("Could not find class " + serializerClass);
}
} catch (ClassNotFoundException e) {
throw new MissingSerializerException("Could not find class " + serializerClass);
}

Map map = new HashMap<>();
Expand All @@ -72,6 +69,11 @@ protected void saveObject(String path, Map object, BukkitConfiguration configura
continue;
}

if (serializer == null) {
map.put(key, configuration.get(path + "." + key));
continue;
}

map.put(key, serializer.deserialize(path + "." + key, configuration));
}

Expand Down

0 comments on commit 462d72a

Please sign in to comment.