-
Notifications
You must be signed in to change notification settings - Fork 2
/
JsonParsing.java
119 lines (101 loc) · 4.12 KB
/
JsonParsing.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
///usr/bin/env jbang "$0" "$@" ; exit $?
// To be able to execute the code in this example, dependencies are needed.
// Therefore, you can't run it with `java JsonParsing.java`.
// But it can be executed with https://www.jbang.dev with `jbang JsonParsing.java`.
// This example uses a Record, which is available since Java 14.
// More info about using specific Java versions with jbang is documented on
// https://www.jbang.dev/documentation/guide/latest/javaversions.html
//JAVA 14+
//DEPS com.fasterxml.jackson.core:jackson-annotations:2.14.1
//DEPS com.fasterxml.jackson.core:jackson-core:2.14.1
//DEPS com.fasterxml.jackson.core:jackson-databind:2.14.1
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
/**
* Example code to illustrate how a Java file that needs dependencies can be executed with JBang.
* Make sure to follow the README of this project to learn more about JBang and how to install it.
*
* This example can be executed with:
* jbang JsonParsing.java
*/
public class JsonParsing {
public static void main (String[] args) {
var json = """
[
{
"level": 0,
"timestamp": 1675867184342,
"message": "Program started"
},
{
"level": 5,
"timestamp": 1675867185921,
"message": "File X not found"
},
{
"level": 9,
"timestamp": 1675867186357,
"message": "Error at line Y"
}
]
""";
try {
ObjectMapper objectMapper = new ObjectMapper();
LogMessage[] logMessages = objectMapper.readValue(json, LogMessage[].class);
System.out.println("Data loaded from JSON:\n");
for (LogMessage logMessage : logMessages) {
System.out.println("Log message at " + logMessage.getZonedDateTime()
+ "\n\tSeverity: " + logMessage.level().getLabel()
+ "\n\tMessage: " + logMessage.message());
}
} catch (IOException ex) {
System.err.println("Json processing exception: " + ex.getMessage());
}
}
record LogMessage(Level level, Long timestamp, String message) {
// If you want to export ZonedDateTime to JSON,
// you need to comment '@JsonIgnore', and uncomment '@JsonFormat'.
@JsonIgnore
//@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
public ZonedDateTime getZonedDateTime() {
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp()), ZoneId.of("UTC"));
}
}
enum Level {
INFO(0, "Informative message", 0x00aa00),
WARNING(5, "Warning message", 0xFFA500),
ERROR(9, "Error message", 0xA30000);
@JsonValue
private final int severity;
private final String label;
private final int color;
private Level(int severity, String label, int color) {
this.severity = severity;
this.label = label;
this.color = color;
}
public int getSeverity() {
return severity;
}
public String getLabel() {
return label;
}
public int getColor() {
return color;
}
@JsonCreator
public static Level fromValue(Integer severity) throws IllegalArgumentException {
return Arrays.stream(Level.values()).sequential()
.filter(v -> v.getSeverity() == severity)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("The given severity does not exist:" + severity));
}
}
}