-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConverter.java
More file actions
49 lines (40 loc) · 1.34 KB
/
Converter.java
File metadata and controls
49 lines (40 loc) · 1.34 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
// To use this code, add the following Maven dependency to your project:
//
// com.fasterxml.jackson.core : jackson-databind : 2.9.0
//
// Import this package:
//
// import io.quicktype.Converter;
//
// Then you can deserialize a JSON string with
//
// Reddit data = Converter.fromJsonString(jsonString);
package io.quicktype;
import java.util.Map;
import java.io.IOException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonProcessingException;
public class Converter {
// Serialize/deserialize helpers
public static Reddit fromJsonString(String json) throws IOException {
return getObjectReader().readValue(json);
}
public static String toJsonString(Reddit obj) throws JsonProcessingException {
return getObjectWriter().writeValueAsString(obj);
}
private static ObjectReader reader;
private static ObjectWriter writer;
private static void instantiateMapper() {
ObjectMapper mapper = new ObjectMapper();
reader = mapper.reader(Reddit.class);
writer = mapper.writerFor(Reddit.class);
}
private static ObjectReader getObjectReader() {
if (reader == null) instantiateMapper();
return reader;
}
private static ObjectWriter getObjectWriter() {
if (writer == null) instantiateMapper();
return writer;
}
}