Skip to content

Commit d6269bf

Browse files
Add README example
1 parent 9bf30cb commit d6269bf

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,60 @@ JASKL can automatically validate config entries (e.g. ensure that a number is al
7373
IntegerConfigEntry positiveIntegerConfigEntry = IntegerConfigEntry.of(config, "example.integer", "Example Integer", 1, Validator.INTEGER_POSITIVE);
7474
```
7575

76+
### Custom Objects (Annotations)
77+
```java
78+
public class MyObject {
79+
80+
@Entry
81+
@Validate.StringNotEmpty
82+
public String myString = "Default String"; // Annotated fields must be public and default values should not be null
83+
84+
@Entry("some.other.path")
85+
public int myInt = 5;
86+
87+
public MyObject() {} // An empty constructor is required
88+
}
89+
```
90+
You can than register a ConfigEntry:
91+
```java
92+
ConfigEntry<MyObject> entry = CustomConfigEntry.of(config, "example.myObject", "Some description", new MyObject());
93+
```
94+
95+
### Custom Objects (ObjectMapper)
96+
If you don't want to use annotations, an ObjectMapper can be used instead.
97+
```java
98+
ObjectMapper<MyObject> mapper = new ObjectMapper<MyObject>() {
99+
@Override
100+
public @NotNull MyObject createInstance(@Unmodifiable @NotNull Map<@NotNull String, @NotNull Object> values) throws InvalidTypeException, ValidationException {
101+
return new MyObject((String) values.get("myString"), (int) values.get("myInt"));
102+
}
103+
104+
@Override
105+
public @Unmodifiable @NotNull Map<@NotNull String, @NotNull Object> readValues(@NotNull MyObject instance) throws InvalidTypeException {
106+
Map<String, Object> values = new HashMap<>();
107+
values.put("myString", instance.getMyString());
108+
values.put("myInt", instance.getMyInt());
109+
return Collections.unmodifiableMap(values);
110+
}
111+
112+
@Override
113+
public @NotNull Class<MyObject> getObjectClass() {
114+
return MyObject.class;
115+
}
116+
117+
@Override
118+
public @NotNull Map<@NotNull String, @NotNull Type<?>> getProperties() {
119+
Map<String, Type<?>> properties = new HashMap<>();
120+
properties.put("myString", Type.validated(Type.STRING, Validator.STRING_NOT_EMPTY));
121+
properties.put("myInt", Type.INTEGER);
122+
return Collections.unmodifiableMap(properties);
123+
}
124+
};
125+
126+
// register the entry
127+
ConfigEntry<MyObject> entry = CustomConfigEntry.of(config, "example.mapper.myObject", "Some description", new MyObject(), mapper);
128+
```
129+
76130
### Annotation-based Configs
77131
You can also use annotation-based configs:
78132
```java

0 commit comments

Comments
 (0)