Java Library to (de-)serialize data in various formats (e.g. JSON, YAML)
- Zero dependencies
- Serialize any class without any modifications
- Auto replacer of placeholders
- Ability to add comments and new lines to the fields
Elytrium Java Serializer is uploaded to the Maven Central repository, so you can just add it as a dependency to your maven or gradle project.
- Maven (pom.xml):
<dependencies> <dependency> <groupId>net.elytrium</groupId> <artifactId>serializer</artifactId> <version>1.1.1</version> </dependency> </dependencies>
- Gradle (build.gradle):
dependencies { implementation("net.elytrium:serializer:1.1.1") }
- Create the class
publicclassSettings { publicStringregularField = "regular value"; }
- Create YamlWriter
Settingssettings = newSettings(); YamlWriterwriter = newYamlWriter(Files.newBufferedWriter(Path.of("config.yml"))); writer.writeNode(settings, null);
- Create YamlReader
Settingssettings = newSettings(); YamlReaderreader = newYamlReader(Files.newBufferedReader(Path.of("config.yml"))); reader.readSerializableObject(settings, Settings.class);
- Create the class that extends YamlSerializable. You can optionally modify the config and call
YamlSerializable#setConfig(SerializerConfig)method. You can safely removesuper(Settings.CONFIG).publicclassSettingsextendsYamlSerializable { privatestaticfinalSerializerConfigCONFIG = newSerializerConfig.Builder().build(); Settings() { super(Settings.CONFIG); } publicStringregularField = "regular value"; }
- Instantiate it.
Settingssettings = newSettings(); settings.reload(Path.of("config.yml"));
- Done!
@NewLine(amount = 3)
@Comment(
value = {
@CommentValue(" SAME_LINE comment Line 1")
},
at = Comment.At.SAME_LINE
)
@Comment(
value = {
@CommentValue(" SAME_LINE APPEND second comment Line 1"),
@CommentValue(type = CommentValue.Type.NEW_LINE),
@CommentValue(" SAME_LINE APPEND second comment Line 2")
},
at = Comment.At.APPEND
)
publicStringregularField = "regular value";Final fields - unmodifiable fields that will be saved in the config.
Transient fields - unmodifiable fields that won't be saved in the config.
publicfinalStringfinalField = "final";
@FinalpublicStringfinalFieldToo = "final";
publictransientStringtransientField = "transient";
@TransientpublicStringtransientFieldToo = "transient";@RegisterPlaceholders({"PLACEHOLDER", "another-placeholder"})
publicStringanotherStringWithPlaceholders = "{PLACEHOLDER} {ANOTHER_PLACEHOLDER}";Assertions.assertEquals("value 1 value 2", Placeholders.replace(settings.anotherStringWithPlaceholders, "value 1", "value 2"));Custom placeholder will be instantiated once for one SerializableConfig.
Placeholders.replace will work even with custom placeholder.
@RegisterPlaceholders(value = {"PLACEHOLDER", "another-placeholder"}, replacer = StringPlaceholderReplacer.class)
publicStringanotherStringWithPlaceholders = "{PLACEHOLDER} {ANOTHER_PLACEHOLDER}";publicclassStringPlaceholderReplacerimplementsPlaceholderReplacer<String> {
@OverridepublicStringreplace(Stringvalue, String[] placeholders, Object... values) {
for (inti = Math.min(values.length, placeholders.length) - 1; i >= 0; --i) {
value = value.replace(placeholders[i], String.valueOf(values[i]));
}
returnvalue;
}
}Custom serializers will be instantiated once for one SerializableConfig.
@Serializer(ExternalClassSerializer.class)
publicExternalDeserializedClasstestClass = newExternalDeserializedClass();publicstaticclassExternalDeserializedClass {
publiclongx;
publiclongy;
publiclongz;
publicExternalNestedClassnestedClass;
// Public constructor with no args should be created for deserializer to workpublicExternalDeserializedClass() {
this.x = 0;
this.y = 0;
this.z = 0;
this.nestedClass = newExternalNestedClass("sample-name");
}
publicExternalDeserializedClass(longx, longy, longz, Stringname) {
this.x = x;
this.y = y;
this.z = z;
this.nestedClass = newExternalNestedClass(name);
}
publicstaticclassExternalNestedClass {
privateStringname;
// Public constructor with no args should be created for deserializer to workpublicExternalNestedClass() {
}
publicExternalNestedClass(Stringname) {
this.name = name;
}
}
}publicclassExternalClassSerializerextendsClassSerializer<ExternalDeserializedClass, Map<String, Object>> {
@SuppressWarnings("unchecked")
protectedExternalClassSerializer() {
super(ExternalDeserializedClass.class, (Class<Map<String, Object>>) (Class<?>) Map.class);
}
@OverridepublicMap<String, Object> serialize(ExternalDeserializedClassfrom) {
returnSerializerTest.map("field-x", from.x, "field-y", from.y, "field-z", from.z, "nested-class-name", from.nestedClass.name);
}
@OverridepublicExternalDeserializedClassdeserialize(Map<String, Object> from) {
returnnewExternalDeserializedClass(
(long) from.get("field-x"),
(long) from.get("field-y"),
(long) from.get("field-z"),
(String) from.get("nested-class-name")
);
}
}In case if you can't add @Serializer annotation or if you have multiple entries with similar classes, you can register the serializer in the config.
privatestaticfinalSerializerConfigCONFIG = newSerializerConfig.Builder().registerSerializer(newPathSerializer()).registerSerializer(newClassSerializer<>(String.class, String.class) {
@OverridepublicStringserialize(Stringfrom) {
returnfrom == null ? "" : from;
}
@OverridepublicStringdeserialize(Stringfrom) {
returnfrom.trim().isEmpty() ? null : from;
}
}).build();If you want to get help or donate to us, you can join our Discord server and talk to us here.
Invite link: https://elytrium.net/discord
