23 | * To convert a config instance to a string, you may simply call the {@code deserialize()} method (we assume that your config is written in JSON):
24 | *
28 | * You can also make your own serializer/deserializer. For example:
29 | *
{@code
30 | * prism.serialize(MyConfig.class, content, (string) -> {
31 | * // Implement your own serializing mechanics here
32 | * });
33 | * }
34 | * To serialize comments, Prism Config uses {@link blue.endless.jankson.Comment} from Jankson,
35 | * so if you want to make your own comment parser, you should use this as well.
36 | *
37 | * @since 0.1.0
38 | */
39 | @SuppressWarnings("unused")
40 | public interface PrismConfig {
41 | /**
42 | * Lazily get the instance of {@link PrismConfig}.
43 | *
44 | * @return an instance of {@link PrismConfig}.
45 | * If the instance cache does not exist, it will try to create one.
46 | */
47 | static PrismConfig getInstance() {
48 | if (PrismConfigImpl.INSTANCE_CACHE != null) {
49 | return PrismConfigImpl.INSTANCE_CACHE;
50 | }
51 | return new PrismConfigImpl();
52 | }
53 |
54 | /**
55 | * Cast the given config content to an instance of the config whose type is specified by the "clazz" parameter and cache the serializer for the given type.
56 | *
57 | * @param clazz the class of the config instance type
58 | * @param content the content of the config as a string
59 | * @param serializer the serializer used to parse the config string
60 | * @param the type of the config instance
61 | * @return an instance of the config
62 | */
63 | T serialize(Class clazz, String content, Function serializer);
64 |
65 | /**
66 | * Cast the given config content to an instance of the config whose type is specified by the "clazz" parameter and cache the serializer for the given type.
67 | *
68 | * @param clazz the class of the config instance type
69 | * @param file the content of the config as a file
70 | * @param serializer the serializer used to parse the config string
71 | * @param the type of the config instance
72 | * @return an instance of the config
73 | */
74 | default T serialize(Class clazz, File file, Function serializer) {
75 | try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
76 | String content = Utils.readFromFile(reader);
77 | return serialize(clazz, content, serializer);
78 | } catch (IOException e) {
79 | try {
80 | return clazz.getDeclaredConstructor().newInstance();
81 | } catch (InvocationTargetException |
82 | InstantiationException |
83 | IllegalAccessException |
84 | NoSuchMethodException ex) {
85 | throw new RuntimeException("Failed to parse config", ex);
86 | }
87 | }
88 | }
89 |
90 | /**
91 | * Cast the given config content to an instance of the config whose type is specified by the "clazz" parameter, using the cached serializer.
92 | *
If the serializer for this class is not cached, a {@link RuntimeException} is thrown.
93 | *
94 | * @param clazz the class of the config instance type
95 | * @param content the content of the config as a string
96 | * @param the type of the config instance
97 | * @return an instance of the config
98 | */
99 | T serializeCached(Class clazz, String content);
100 |
101 | /**
102 | * Cast the given config content to an instance of the config whose type is specified by the "clazz" parameter and cache the serializer for the given type.
103 | *
104 | * @param clazz the class of the config instance type
105 | * @param file the content of the config as a file
106 | * @param the type of the config instance
107 | * @return an instance of the config
108 | */
109 | default T serializeCached(Class clazz, File file) {
110 | try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
111 | String content = Utils.readFromFile(reader);
112 | return serializeCached(clazz, content);
113 | } catch (IOException e) {
114 | try {
115 | return clazz.getDeclaredConstructor().newInstance();
116 | } catch (InvocationTargetException |
117 | InstantiationException |
118 | IllegalAccessException |
119 | NoSuchMethodException ex) {
120 | throw new RuntimeException("Failed to parse config", ex);
121 | }
122 | }
123 | }
124 |
125 | /**
126 | * Convert the given config instance to a string representing the config content and cache the deserializer for the given type.
127 | *
128 | * @param clazz the class of the config instance type
129 | * @param content the content of the config as an instance
130 | * @param deserializer the deserializer used to parse the config instance
131 | * @param the type of the config instance
132 | * @return the content of the config as a string
133 | */
134 | String deserialize(Class clazz, T content, Function deserializer);
135 |
136 | /**
137 | * Convert the given config instance to a string representing the config content, using the cached deserializer.
138 | *
If the deserializer for this class is not cached, a {@link RuntimeException} is thrown.
139 | *
140 | * @param clazz the class of the config instance type
141 | * @param content the content of the config as an instance
142 | * @param the type of the config instance
143 | * @return the content of the config as a string
144 | */
145 | String deserializeCached(Class clazz, T content);
146 |
147 | /**
148 | * Write the given config instance to the target file as a string representing the config content and cache the deserializer for the given type.
149 | *
150 | * @param clazz the class of the config instance type
151 | * @param content the content of the config as an instance
152 | * @param deserializer the deserializer used to parse the config instance
153 | * @param file the target file the config is written to
154 | * @param the type of the config instance
155 | */
156 | default void deserializeAndWrite(Class clazz, T content, Function deserializer, File file) {
157 | String string = deserialize(clazz, content, deserializer);
158 | Utils.writeToConfigFile(file, string);
159 | }
160 |
161 | /**
162 | * Write the given config instance to the target file as a string representing the config content, using the cached deserializer.
163 | *
If the deserializer for this class is not cached, a {@link RuntimeException} is thrown.
164 | *
165 | * @param clazz the class of the config instance type
166 | * @param content the content of the config as an instance
167 | * @param file the target file the config is written to
168 | * @param the type of the config instance
169 | */
170 | default void deserializeAndWriteCached(Class clazz, T content, File file) {
171 | String string = deserializeCached(clazz, content);
172 | Utils.writeToConfigFile(file, string);
173 | }
174 |
175 | @ApiStatus.Internal
176 | class Utils {
177 | private static @NotNull String readFromFile(@NotNull BufferedReader reader) throws IOException {
178 | StringBuilder stringBuilder = new StringBuilder();
179 | String line;
180 | String ls = System.getProperty("line.separator");
181 | while ((line = reader.readLine()) != null) {
182 | stringBuilder.append(line);
183 | stringBuilder.append(ls);
184 | }
185 | stringBuilder.deleteCharAt(stringBuilder.length() - 1);
186 | reader.close();
187 |
188 | return stringBuilder.toString();
189 | }
190 |
191 | private static void writeToConfigFile(@NotNull File file, String string) {
192 | if (!file.exists()) {
193 | try {
194 | file.createNewFile();
195 | } catch (IOException e) {
196 | throw new RuntimeException("Failed to create file", e);
197 | }
198 | }
199 | try (FileWriter writer = new FileWriter(file)) {
200 | writer.write(""); // Empty the file
201 | writer.write(string);
202 | writer.flush();
203 | } catch (IOException e) {
204 | throw new RuntimeException("Failed to write config");
205 | }
206 | }
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/src/main/java/io/github/prismwork/prismconfig/api/config/DefaultDeserializers.java:
--------------------------------------------------------------------------------
1 | package io.github.prismwork.prismconfig.api.config;
2 |
3 | import io.github.prismwork.prismconfig.impl.config.DefaultDeserializersImpl;
4 |
5 | import java.util.function.Function;
6 |
7 | /**
8 | * The default deserializers Prism Config provides.
9 | *
For now there are three: JSON, JSON5 and TOML (0.4.0).
10 | *
11 | * @since 0.1.0
12 | */
13 | @SuppressWarnings("unused")
14 | public interface DefaultDeserializers {
15 | /**
16 | * Lazily get the instance of {@link DefaultDeserializers}.
17 | *
18 | * @return an instance of {@link DefaultDeserializers}.
19 | * If the instance cache does not exist, it will try to create one.
20 | */
21 | static DefaultDeserializers getInstance() {
22 | if (DefaultDeserializersImpl.INSTANCE_CACHE != null) {
23 | return DefaultDeserializersImpl.INSTANCE_CACHE;
24 | }
25 | return new DefaultDeserializersImpl();
26 | }
27 |
28 | /**
29 | * Returns a JSON deserializer for the given config class.
30 | *
31 | * @param clazz the config class the deserializer is going to handle
32 | * @param the type of the config class
33 | * @return the JSON deserializer for the given config class
34 | */
35 | Function json(Class clazz);
36 |
37 | /**
38 | * Returns a JSON5 deserializer for the given config class.
39 | *
40 | * @param clazz the config class the deserializer is going to handle
41 | * @param the type of the config class
42 | * @return the JSON5 deserializer for the given config class
43 | */
44 | Function json5(Class clazz);
45 |
46 | /**
47 | * Returns a TOML (0.4.0) deserializer for the given config class.
48 | *
49 | * @param clazz the config class the deserializer is going to handle
50 | * @param the type of the config class
51 | * @return the TOML deserializer for the given config class
52 | */
53 | Function toml(Class clazz);
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/io/github/prismwork/prismconfig/api/config/DefaultSerializers.java:
--------------------------------------------------------------------------------
1 | package io.github.prismwork.prismconfig.api.config;
2 |
3 | import io.github.prismwork.prismconfig.impl.config.DefaultSerializersImpl;
4 |
5 | import java.util.function.Function;
6 |
7 | /**
8 | * The default serializers Prism Config provides.
9 | *
For now there are three: JSON, JSON5 and TOML (0.4.0).
10 | *
11 | * @since 0.1.0
12 | */
13 | @SuppressWarnings("unused")
14 | public interface DefaultSerializers {
15 | /**
16 | * Lazily get the instance of {@link DefaultSerializers}.
17 | *
18 | * @return an instance of {@link DefaultSerializers}.
19 | * If the instance cache does not exist, it will try to create one.
20 | */
21 | static DefaultSerializers getInstance() {
22 | if (DefaultSerializersImpl.INSTANCE_CACHE != null) {
23 | return DefaultSerializersImpl.INSTANCE_CACHE;
24 | }
25 | return new DefaultSerializersImpl();
26 | }
27 |
28 | /**
29 | * Returns a JSON serializer for the given config class.
30 | *
31 | * @param clazz the config class the serializer is going to handle
32 | * @param the type of the config class
33 | * @return the JSON serializer for the given config class
34 | */
35 | Function json(Class clazz);
36 |
37 | /**
38 | * Returns a JSON5 serializer for the given config class.
39 | *
40 | * @param clazz the config class the serializer is going to handle
41 | * @param the type of the config class
42 | * @return the JSON5 serializer for the given config class
43 | */
44 | Function json5(Class clazz);
45 |
46 | /**
47 | * Returns a TOML (0.4.0) serializer for the given config class.
48 | *
49 | * @param clazz the config class the serializer is going to handle
50 | * @param the type of the config class
51 | * @return the TOML serializer for the given config class
52 | */
53 | Function toml(Class clazz);
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/io/github/prismwork/prismconfig/impl/PrismConfigImpl.java:
--------------------------------------------------------------------------------
1 | package io.github.prismwork.prismconfig.impl;
2 |
3 | import io.github.prismwork.prismconfig.api.PrismConfig;
4 | import org.jetbrains.annotations.ApiStatus;
5 | import org.jetbrains.annotations.Nullable;
6 |
7 | import java.util.HashMap;
8 | import java.util.Map;
9 | import java.util.function.Function;
10 |
11 | @ApiStatus.Internal
12 | @SuppressWarnings("unchecked")
13 | public final class PrismConfigImpl implements PrismConfig {
14 | public static @Nullable PrismConfig INSTANCE_CACHE;
15 |
16 | private final Map, Function> cachedSerializers;
17 | private final Map, Function