-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGsonAdapter.java
More file actions
95 lines (84 loc) · 3.19 KB
/
KeyGsonAdapter.java
File metadata and controls
95 lines (84 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package net.theevilreaper.aves.file.gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.kyori.adventure.key.Key;
import org.jetbrains.annotations.Contract;
import java.io.IOException;
import java.util.function.BiFunction;
/**
* This class is used to serialize and deserialize a {@link Key} object.
* The {@link Key} object is represented as a JSON object with the keys "namespace" and "value".
* The namespace is the namespace of the key and the value is the value of the key.
* The JSON object is represented as follows:
* <pre>
* {
* "namespace": "namespace",
* "value": "value"
* }
* </pre>
* A key can be created by calling the {@link Key#key(String, String)} method.
* Use {@link #create()} to create a default adapter.
* Use {@link #create(BiFunction)} to create an adapter with a custom key creation function.
* Use {@link #createMinestom()} to create an adapter with a custom key({@link Key}) creation function for Minestom.
*
* @since 1.0.0
* @version 1.0.0
* @see Key
* @see Key#key(String, String)
* @author TheMeinerLP
*/
public class KeyGsonAdapter extends TypeAdapter<Key> {
private final BiFunction<String, String, Key> createKeyObject;
private KeyGsonAdapter(BiFunction<String, String, Key> createKeyObject) {
this.createKeyObject = createKeyObject;
}
private KeyGsonAdapter() {
this(Key::key);
}
/**
* Creates a new instance of the {@link KeyGsonAdapter} with the default key creation function.
* @return the new instance of the {@link KeyGsonAdapter}
*/
@Contract(value = " -> new", pure = true)
public static KeyGsonAdapter create() {
return new KeyGsonAdapter();
}
/**
* Creates a new instance of the {@link KeyGsonAdapter} with a custom key creation function.
* @param createKeyObject the custom key creation function
* @return the new instance of the {@link KeyGsonAdapter}
*/
@Contract(value = "_ -> new", pure = true)
public static KeyGsonAdapter create(BiFunction<String, String, Key> createKeyObject) {
return new KeyGsonAdapter(createKeyObject);
}
/**
* Creates a new instance of the {@link KeyGsonAdapter} with a custom key) creation function for Minestom.
* @return the new instance of the {@link KeyGsonAdapter}
*/
public static KeyGsonAdapter createMinestom() {
return new KeyGsonAdapter(Key::key);
}
@Override
public void write(JsonWriter out, Key value) throws IOException {
out.beginObject();
out.name("namespace").value(value.namespace());
out.name("value").value(value.value());
out.endObject();
}
@Override
public Key read(JsonReader in) throws IOException {
in.beginObject();
if (!in.nextName().equals("namespace")) {
throw new IOException("Expected namespace");
}
var namespace = in.nextString();
if (!in.nextName().equals("value")) {
throw new IOException("Expected value");
}
var value = in.nextString();
in.endObject();
return this.createKeyObject.apply(namespace, value);
}
}