Skip to content

Commit

Permalink
Pass the value type to #storeDefault so the default value can be seri…
Browse files Browse the repository at this point in the history
…alized (#113)
  • Loading branch information
lucko committed Jul 11, 2018
1 parent 4dba3f9 commit 80b0ec1
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ private <T> T storeDefault(T defValue) {
return defValue;
}

private <T> T storeDefault(TypeToken<T> type, T defValue) throws ObjectMappingException {
if (defValue != null && getOptions().shouldCopyDefaults()) {
setValue(type, defValue);
}
return defValue;
}

@Override
public Object getValue(Object def) {
Object ret = value.getValue();
Expand Down Expand Up @@ -197,15 +204,15 @@ public <T> List<T> getList(@NonNull TypeToken<T> type, @NonNull Supplier<List<T>
public <T> T getValue(@NonNull TypeToken<T> type, T def) throws ObjectMappingException {
Object value = getValue();
if (value == null) {
return storeDefault(def);
return storeDefault(type, def);
}

TypeSerializer serial = getOptions().getSerializers().get(type);
if (serial == null) {
if (type.getRawType().isInstance(value)) {
return (T) type.getRawType().cast(value);
} else {
return storeDefault(def);
return storeDefault(type, def);
}
}
return (T) serial.deserialize(type, this);
Expand All @@ -216,15 +223,15 @@ public <T> T getValue(@NonNull TypeToken<T> type, T def) throws ObjectMappingExc
public <T> T getValue(@NonNull TypeToken<T> type, @NonNull Supplier<T> defSupplier) throws ObjectMappingException {
Object value = getValue();
if (value == null) {
return storeDefault(defSupplier.get());
return storeDefault(type, defSupplier.get());
}

TypeSerializer serial = getOptions().getSerializers().get(type);
if (serial == null) {
if (type.getRawType().isInstance(value)) {
return (T) type.getRawType().cast(value);
} else {
return storeDefault(defSupplier.get());
return storeDefault(type, defSupplier.get());
}
}
return (T) serial.deserialize(type, this);
Expand Down

0 comments on commit 80b0ec1

Please sign in to comment.