-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonUtils.java
More file actions
101 lines (94 loc) · 3.11 KB
/
JsonUtils.java
File metadata and controls
101 lines (94 loc) · 3.11 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
95
96
97
98
99
100
101
package cs3500.pa05.model;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import cs3500.pa05.model.json.EventJson;
import cs3500.pa05.model.json.TaskJson;
import cs3500.pa05.model.json.WeekJson;
/**
* Utils class for converting between JSON and records
*/
public class JsonUtils {
/**
* Converts a given record object to a JsonNode.
*
* @param record the record to convert
* @return the JsonNode representation of the given record
* @throws IllegalArgumentException if the record could not be converted correctly
*/
public static JsonNode serializeRecord(Record record) throws IllegalArgumentException {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(record, JsonNode.class);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Given record cannot be serialized");
}
}
/**
* Deserializes the given Json string
*
* @param str Json string
* @return WeekJson of the given data
*/
public static WeekJson deserializeJsonString(String str) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(str, JsonNode.class);
return mapper.convertValue(node, WeekJson.class);
} catch (IllegalArgumentException | JsonProcessingException e) {
throw new IllegalArgumentException("Given string cannot be deserialized");
}
}
/**
* Converts the given Event to an EventJson
*
* @param event event
* @return event json
*/
public static EventJson convertEvent(Event event) {
EventJson eventJson = new EventJson(
event.nameProperty(), event.descriptionProperty(),
event.dayOfWeekProperty().getDay(), event.startTimeProperty(),
event.durationProperty());
return eventJson;
}
/**
* Converts the given EventJson to an Event
*
* @param eventJson event json
* @return event
*/
public static Event convertEventJson(EventJson eventJson) {
Event event = new Event(
eventJson.name(), eventJson.description(),
eventJson.startTime(),
eventJson.duration(), DayType.valueOf(eventJson.day().toUpperCase()));
System.out.println(event.eventToString());
return event;
}
/**
* Converts the given Task to an TaskJson
*
* @param task task
* @return task json
*/
public static TaskJson convertTask(Task task) {
TaskJson taskJson = new TaskJson(
task.nameProperty(), task.descriptionProperty(),
task.dayOfWeekProperty().getDay(), Boolean.toString(task.completeProperty()));
return taskJson;
}
/**
* Converts the given TaskJson to a Task
*
* @param taskJson task json
* @return task
*/
public static Task convertTaskJson(TaskJson taskJson) {
Task task = new Task(
taskJson.name(), taskJson.description(),
DayType.valueOf(taskJson.day().toUpperCase()),
Boolean.parseBoolean(taskJson.completed()));
return task;
}
}