@Repeatable(value=SerdeDefaultsContainer.class) @Retention(value=RUNTIME) @Target(value=FIELD) public @interface SerdeDefault
By default, the default value is used when the field to deserialize has
no corresponding entry in the Config.
class MyObject {
// When deserializing, if the config entry "servers" is missing, the
// field will be assigned to the default value, as returned by defaultServers().
// If the config value is null, the field will be assigned to null.
@SerdeDefault(provider = "defaultServers")
List<String> servers;
List<String> defaultServers() {
return Arrays.asList("example.org");
}
}
class MyObject {
// When deserializing, if the config entry "servers" is missing, the
// field will be assigned to the default value, as returned by defaultServersSupplier.get().
// If the config value is null, the field will be assigned to null.
@SerdeDefault(provider = "defaultServersSupplier")
List<String> servers;
// Note the "transient" annotation, which excludes this field from (de)serialization.
private transient Supplier<List<String>> defaultServersSupplier = () -> Arrays.asList("example.org");
}
import java.util.Arrays;
import java.util.Collections;
import static com.electronwill.nightconfig.core.serde.annotations.SerdeDefault.WhenValue.*;
import static com.electronwill.nightconfig.core.serde.annotations.SerdeDefault.SerdePhase.*;
class MyObject {
// When deserializing, if the config entry "servers" is missing, null or empty,
// the field will be assigned to the value returned by
// DefaultProviders.defaultServersWhenDeserializing().
//
// When serializing, if the object's field is null, the config entry will be set to the
// value returned by DefaultsProviders.defaultServersWhenSerializing().
@SerdeDefault(cls = DefaultProviders.class, provider = "defaultServersWhenDeserializing", phase = DESERIALIZING, whenValue = {IS_MISSING, IS_NULL, IS_EMPTY})
@SerdeDefault(cls = DefaultProviders.class, provider = "defaultServersWhenSerializing", phase = SERIALIZING, whenValue = {IS_NULL})
List<String> servers;
}
class DefaultProviders {
// Here, the default provider is not in the class of the object to deserialize,
// therefore it needs to be static.
static List<String> defaultServersWhenDeserializing() {
return Arrays.asList("example.org");
}
static List<String> defaultServersWhenSerializing() {
return Collections.emptyList();
}
}
| Modifier and Type | Required Element and Description |
|---|---|
java.lang.String |
provider
The name of the method or field that provides the default value.
|
| Modifier and Type | Optional Element and Description |
|---|---|
java.lang.Class<?> |
cls
The class that defines the method that provides the default value.
|
SerdePhase |
phase
Controls whether the default value applies when Serializing and/or
Deserializing.
|
SerdeDefault.WhenValue[] |
whenValue
Controls when to use the default value, based on the actual value.
|
public abstract java.lang.String provider
cls() is set to its non-default value, the method must be static.
java.util.function.Supplier<T>
where T is the type of the field to (de)serialize.
In most cases, the provider field should be declared with the
transient
keyword, to prevent it from being (de)serialized.Supplierpublic abstract java.lang.Class<?> cls
By default, it is set to Object.class, which is treated as a special
value.
It means that the actual class to use is the one of the object that is
currently being (de)serialized.
public abstract SerdePhase phase
public abstract SerdeDefault.WhenValue[] whenValue
When serializing, the "actual value" is the value of the field. When deserializing, the "actual value" is the value of the config entry.