forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonUtils.java
More file actions
94 lines (83 loc) · 2.52 KB
/
JsonUtils.java
File metadata and controls
94 lines (83 loc) · 2.52 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
package land.oras.utils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import land.oras.OrasException;
import org.jspecify.annotations.NullMarked;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class for JSON operations.
* Use Gson internally for JSON operations
*/
@NullMarked
public final class JsonUtils {
private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);
/**
* Gson instance
*/
private static final Gson gson;
/**
* Utils class
*/
private JsonUtils() {
// Hide constructor
}
/**
* Type adapter for ZonedDateTime
*/
private static final class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
@Override
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
out.value(value.toString());
}
@Override
public ZonedDateTime read(JsonReader in) throws IOException {
return ZonedDateTime.parse(in.nextString());
}
}
static {
gson = new GsonBuilder()
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter())
.create();
}
/**
* Convert an object to a JSON string
* @param object The object to convert
* @return The JSON string
*/
public static String toJson(Object object) {
return gson.toJson(object);
}
/**
* Convert a JSON string to an object
* @param json The JSON string
* @param clazz The class of the object
* @param <T> The type of the object
* @return The object
*/
public static <T> T fromJson(String json, Class<T> clazz) {
return gson.fromJson(json, clazz);
}
/**
* Convert a JSON string to an object
* @param path The path to the JSON file
* @param clazz The class of the object
* @param <T> The type of the object
* @return The object
*/
public static <T> T fromJson(Path path, Class<T> clazz) {
try {
return gson.fromJson(Files.readString(path, StandardCharsets.UTF_8), clazz);
} catch (IOException e) {
throw new OrasException("Unable to read JSON file due to IO error", e);
}
}
}