forked from auth0/auth0-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonTest.java
More file actions
46 lines (35 loc) · 1.35 KB
/
JsonTest.java
File metadata and controls
46 lines (35 loc) · 1.35 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
package com.auth0.json;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonTest<T> {
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
private ObjectMapper mapper;
public JsonTest() {
this.mapper = new ObjectMapper();
}
public String toJSON(T value) throws JsonProcessingException {
return mapper.writeValueAsString(value);
}
public T fromJSON(String json, Class<T> tClazz) throws IOException {
return mapper.readValue(json, tClazz);
}
public T fromJSON(String json, TypeReference<T> tReference) throws IOException {
return mapper.readValue(json, tReference);
}
public String readTextFile(String path) throws IOException {
return new String(Files.readAllBytes(Paths.get(path)));
}
protected Date parseJSONDate(String dateString) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.parse(dateString);
}
}