A straightforward object persistence library.
To use in your project, add this in your build.gradle
:
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.mindustry-ddns-net:FileStore:2.1.1'
}
The library provides the Store
and FileStore
classes to load and save your objects, with the format handled by a Serializer
.
Here are the default formats provided by the library (make sure the implementations are available in the classpath) :
-
Json: Use this for your settings, the stuff that can change at runtime.
-
Properties: I recommend using this for configurations, stuff that don't change during runtime, generally used at the application startup.
-
Other formats :
- Jackson: You can use one of the bindings like xml by passing the mapper with
Serializers.jackson(ObjectMapper)
.
- Jackson: You can use one of the bindings like xml by passing the mapper with
For example, let's say you have this PersonSettings
POJO class :
public class PersonSettings {
private String name;
private int age;
public PersonSettings(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
You'll just have to do :
FileStore<PersonSettings> store = FileStore.of("./person.json", Serializers.gson(), new TypeToken<>(){});
Then you can freely call the load()
or save()
methods whenever you mutate the PersonSettings
object stored in the store with set()
or get()
.
- Interfaces with generic parameters aren't supported by the
config
(Owner) serializer.