This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJsonToDbUtil.java
More file actions
105 lines (84 loc) · 3.75 KB
/
JsonToDbUtil.java
File metadata and controls
105 lines (84 loc) · 3.75 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
102
103
104
105
package com.uwflow.flow_android.util;
import com.google.gson.*;
import com.uwflow.flow_android.db_object.*;
import org.json.JSONObject;
import java.lang.reflect.Type;
import java.sql.Timestamp;
public class JsonToDbUtil {
// Our custom GSON deserializer that knows how to handle a Timestamp
private static final Gson gson;
static {
gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Timestamp.class, new TimestampDeserializer())
.create();
}
public static User getUser(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, User.class);
}
public static User getUserMe(JSONObject jsonObject) {
User user = getUser(jsonObject);
user.setMe(true);
return user;
}
public static UserFriends getUserFriends(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, UserFriends.class);
}
public static UserCourse getUserCourse(JSONObject jsonObject) {
JsonObject gsonUserCourse = getJsonObject(jsonObject);
return gson.fromJson(gsonUserCourse, UserCourse.class);
}
public static UserCourseDetail getUserCourseDetail(JSONObject jsonObject) {
JsonObject gsonCourses = getJsonObject(jsonObject);
UserCourseDetail courseDetail = gson.fromJson(gsonCourses, UserCourseDetail.class);
return courseDetail;
}
public static ScheduleCourses getUserSchedule(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, ScheduleCourses.class);
}
public static Exams getUserExams(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, Exams.class);
}
public static Course getCourse(Object json) {
JsonObject gsonObject = getJsonObject(json);
return gson.fromJson(gsonObject, Course.class);
}
public static CourseDetail getCourseDetail(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, CourseDetail.class);
}
public static Professors getCourseProfessors(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, Professors.class);
}
public static Sections getCourseSections(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, Sections.class);
}
public static Exams getCourseExams(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, Exams.class);
}
public static CourseUserDetail getCourseUserDetail(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, CourseUserDetail.class);
}
public static SearchResults getSearchResults(JSONObject jsonObject) {
JsonObject gsonObject = getJsonObject(jsonObject);
return gson.fromJson(gsonObject, SearchResults.class);
}
private static JsonObject getJsonObject(Object json) {
return (JsonObject) new JsonParser().parse(json.toString());
}
private static class TimestampDeserializer implements JsonDeserializer<Timestamp> {
@Override
public Timestamp deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
return new Timestamp(jsonElement.getAsLong());
}
}
}