-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonMethodDemo.java
75 lines (60 loc) · 2.72 KB
/
JsonMethodDemo.java
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
package co.casterlabs.rakurai.json;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import co.casterlabs.rakurai.json.annotating.JsonClass;
import co.casterlabs.rakurai.json.annotating.JsonDeserializationMethod;
import co.casterlabs.rakurai.json.annotating.JsonExclude;
import co.casterlabs.rakurai.json.annotating.JsonSerializationMethod;
import co.casterlabs.rakurai.json.element.JsonElement;
import co.casterlabs.rakurai.json.element.JsonString;
import co.casterlabs.rakurai.json.validation.JsonValidate;
import lombok.NonNull;
import lombok.ToString;
public class JsonMethodDemo {
public static void main(String[] args) throws Exception {
// Enable assertions programatically.
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
// Here we have a list of users with String ids, in Java we want them as ints.
String json = "[{\"_id\": \"1\", \"name\": \"Casterlabs\"},{\"_id\": \"2\" \"name\": null}]";
List<User> users = Rson.DEFAULT.fromJson(json, new TypeToken<List<User>>() {
});
System.out.println(users);
System.out.println(Rson.DEFAULT.toJsonString(users)); // To show that we can modify the output with methods.
}
@ToString
@JsonClass(exposeAll = true)
public static final class User {
// So Rson won't try to serialize the type.
public @JsonExclude int _id;
public String name;
// The higher the weight the sooner it'll be executed.
// In our case, we want the ID to get deserialized before
// the name (so we can generate a default based on the ID)
@JsonDeserializationMethod(value = "_id", weight = 1)
private void _jsonId(@NonNull JsonElement e) {
// Exceptions will be caught and rethrown as JsonParseException.
this._id = Integer.parseInt(e.getAsString());
}
@JsonSerializationMethod("_id")
private @Nullable JsonElement _jsonId() {
// Keep the type as JsonString in the json.
return new JsonString(String.valueOf(this._id));
}
@JsonDeserializationMethod("name")
private void _jsonName(@NonNull JsonElement e) {
// If it's JsonNull this'll return false,
// and it's guaranteed to not be null.
if (e.isJsonString()) {
this.name = e.getAsString();
} else {
this.name = String.format("New User (no. %s)", this._id);
}
}
// JsonValidate will be executed after the methods.
// If needed, the methods can also do their own validation and throw an error.
@JsonValidate
private void validate() {
assert this.name != null;
}
}
}