-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use ValueSerializer #25
Comments
You use ValueSerialiser so you can use a custom type in your configuration. Let's say you had the following: public class CustomType {
private final String value;
public CustomType(String value) {
this.value = value;
}
public String stringValue() {
return value;
}
}
public class MyCustomSerializer implements ValueSerialiser<CustomType> {
@Override
public Class<CustomType> getTargetClass() {
return CustomType.class;
}
@Override
public CustomType deserialise(FlexibleType flexibleType) throws BadValueException {
// Convert from a String -> CustomType
String value = flexibleType.getString();
return new CustomType(value);
}
@Override
public Object serialise(CustomType value, Decomposer decomposer) {
// Convert from a CustomType -> String
return value.stringValue();
}
}
ConfigurationOptions options = new ConfigurationOptions.Builder()
.addSerialiser(new MyCustomSerializer())
.build(); Use the ConfigurationOptions when you create your config factory (GsonConfigurationFactory, SnakeYamlConfigurationFactory, etc.) |
How i can use this for object with more variables? |
I'm not exactly sure what you mean by that. If you need to use this type in your configuration, you can very easily: public interface Config {
@DefaultString("default value")
CustomType something();
// You can even use it in a List, Set, or Collection
@DefaultStrings({"value1", "value2"})
List<CustomType> listOfThings();
} |
I need put this to config:
|
I see. For that, you can treat a public class LevelSerializer implements ValueSerialiser<Level> {
@Override
public Class<Level> getTargetClass() {
return Level.class;
}
@Override
public Level deserialise(FlexibleType flexibleType) throws BadValueException {
List<Integer> values = new ArrayList<>();
for (FlexibleType element : flexibleType.getList()) {
values.add(element.getInteger());
}
return new Level(values);
}
@Override
public Object serialise(Level value, Decomposer decomposer) {
return value.values();
}
}
public class Level {
private final List<Integer> values;
public Level(List<Integer> values) {
this.values = List.copyOf(values);
if (this.values.size() != 5) {
throw new IllegalArgumentException("Size of Level values must be 5");
}
}
public List<Integer> values() {
return values;
}
} There are other ways you could do this, like by treating a Level as a configuration section, but I choose this way because it's relatively simple. |
But you can show my how i can do ths like by treating a level as configuration section? Your solution is not convenient to configuration. Level:
|
Yes. You can even do this without using a value serializer: public interface Config {
static Map<Integer, Level> defaultLevels() {
Map<Integer, Level> levelsMap = new HashMap<>();
levelsMap.put(1, Level.of(5, 10, 1, 10, 0));
levelsMap.put(2, Level.of(7, 12, 2, 11, 10));
levelsMap.put(3, Level.of(9, 14, 3, 12, 20));
levelsMap.put(4, Level.of(12, 16, 4, 13, 30));
levelsMap.put(5, Level.of(15, 18, 5, 14, 40));
return levelsMap;
}
@ConfDefault.DefaultObject("defaultLevels")
Map<Integer, @SubSection Level> levels();
}
public interface Level {
int minSpawnDelay();
int maxSpawnDelay();
int spawnCount();
int spawnRange();
int price();
static Level of(int minSpawnDelay, int maxSpawnDelay, int spawnCount, int spawnRange, int price) {
return new Level() {
@Override
public int minSpawnDelay() { return minSpawnDelay; }
@Override
public int maxSpawnDelay() { return maxSpawnDelay; }
@Override
public int spawnCount() { return spawnCount; }
@Override
public int spawnRange() { return spawnRange; }
@Override
public int price() { return price; }
};
}
} |
Note that you need @ConfDefault.DefaultObject("defaultLevels")
Map<Integer, @SubSection Level> levels(); |
Thank you wery much <3 |
You're welcome. A thanks to you too - I've added this information to the wiki now: |
Map key need be String :(
|
Thanks. This is a bug with the snakeyaml extension. The core should support non-string keys. Also, I tested that this bug is specific to If you need comments and can't wait until the next release, let me know and I will publish a SNAPSHOT release with the bug fix. |
Released in 1.2.1 |
Hi, how i can use ValueSerializer?
The text was updated successfully, but these errors were encountered: