public final class ObjectSerializer
extends java.lang.Object
UnmodifiableConfig, Config, etc.).
class Position {
private final int x, y, z;
public Position(int x, int y, int z) {
this.x=x; this.y=y; this.z=z;
}
}
And an instance like this:
Position pos = new Position(12, -20, 42);
You can serialize your instance of Position to a Config with:
Config conf = new ObjectSerializer.standard().serializeFields(pos, Config::inMemory);
// result: SimpleConfig{x=12, y=-20, z=42}
It is also possible to serialize an object to an existing configuration,
which is especially handy if you are working with FileConfigs.
FileConfig myFileConfig = ...; // your FileConfig here
new ObjectSerializer().standard().serializeFields(pos, myFileConfig);
// the FileConfig is modified with the serialization result
Use builder() or blankBuilder() to precisely configure
the serialization process.
| Modifier and Type | Method and Description |
|---|---|
static ObjectSerializerBuilder |
blankBuilder()
Creates a new
ObjectSerializerBuilder with some standard serializers
already registered. |
static ObjectSerializerBuilder |
builder()
Creates a new
ObjectSerializerBuilder with some standard serializers
already registered. |
java.lang.Object |
serialize(java.lang.Object value,
java.util.function.Supplier<? extends Config> configSupplier)
Serializes a single value.
|
void |
serializeFields(java.lang.Object source,
Config destination)
Serializes an object as a
Config by transforming its fields into
configuration entries. |
<C extends Config> |
serializeFields(java.lang.Object source,
java.util.function.Supplier<C> configSupplier)
Serializes an object as a
Config or type C by transforming
its fields into configuration
entries. |
static ObjectSerializer |
standard()
Creates a new
ObjectSerializer with the standard deserializers. |
public static ObjectSerializerBuilder builder()
ObjectSerializerBuilder with some standard serializers
already registered.public static ObjectSerializerBuilder blankBuilder()
ObjectSerializerBuilder with some standard serializers
already registered.public static ObjectSerializer standard()
ObjectSerializer with the standard deserializers.
This is equivalent to ObjectSerializer.builder().build().
public java.lang.Object serialize(java.lang.Object value,
java.util.function.Supplier<? extends Config> configSupplier)
configSupplier.
To ensure that you serialize the fields of a Java object into a
Config, use
serializeFields(Object, Supplier).
public <C extends Config> C serializeFields(java.lang.Object source, java.util.function.Supplier<C> configSupplier)
Config or type C by transforming
its fields into configuration
entries. A new configuration is created with the configSupplier.
The serialization depends on the registered value
serializers.
public void serializeFields(java.lang.Object source,
Config destination)
Config by transforming its fields into
configuration entries.
The entries are inserted in destination.
The serialization depends on the registered value
serializers.
source - object to serializedestination - configuration to store the result inObjectSerializerBuilder.withSerializerForClass(Class, ValueSerializer),
ObjectSerializerBuilder.withSerializerForExactClass(Class,
ValueSerializer),
ObjectSerializerBuilder.withSerializerProvider(ValueSerializerProvider)