Commit f2806d13 authored by Robert Hitt's avatar Robert Hitt
Browse files

Fixed the Json deserialization

- Also changed the .gitignore and readme's slightly
parent 5b82c670
......@@ -34,9 +34,7 @@ captures/
# Intellij
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/libraries
.idea/
# Keystore files
*.jks
......
......@@ -28,7 +28,7 @@ To get started, you'll need the following installed:
* [Android Studio](https://developer.android.com/studio/index.html)
```bash
git clone http://git.gmu.edu/srct/whats-open-android.git
git clone https://git.gmu.edu/srct/whats-open-android.git
```
That's about it. From there you can use Android Studio's built in VCS tools,
......
......@@ -9,17 +9,23 @@ import io.realm.annotations.PrimaryKey;
public class Facility extends RealmObject {
@PrimaryKey
private String name;
@SerializedName("name")
private String mName;
@SerializedName("location")
private String mLocation;
@SerializedName("main_schedule")
private MainSchedule mMainSchedule;
private boolean isFavorited;
private RealmList<OpenTimes> mOpenTimes;
public String getName() {
return name;
return mName;
}
public void setName(String name) {
this.name = name;
this.mName = name;
}
public boolean isFavorited() {
......@@ -30,11 +36,19 @@ public class Facility extends RealmObject {
isFavorited = favorited;
}
public RealmList<OpenTimes> getOpenTimes() {
return mOpenTimes;
public String getLocation() {
return mLocation;
}
public void setLocation(String location) {
mLocation = location;
}
public MainSchedule getMainSchedule() {
return mMainSchedule;
}
public void setOpenTimes(RealmList<OpenTimes> openTimes) {
this.mOpenTimes = openTimes;
public void setMainSchedule(MainSchedule mainSchedule) {
mMainSchedule = mainSchedule;
}
}
package srct.whatsopen.model;
import com.google.gson.annotations.SerializedName;
import io.realm.RealmList;
import io.realm.RealmObject;
public class MainSchedule extends RealmObject {
@SerializedName("open_times")
private RealmList<OpenTimes> mOpenTimesList;
public RealmList<OpenTimes> getOpenTimesList() {
return mOpenTimesList;
}
public void setOpenTimesList(RealmList<OpenTimes> openTimesList) {
mOpenTimesList = openTimesList;
}
}
package srct.whatsopen.service;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.realm.RealmList;
import io.realm.RealmObject;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import srct.whatsopen.util.OpenTimesDeserializer;
import srct.whatsopen.util.OpenTimesListDeserializer;
import srct.whatsopen.model.OpenTimes;
/**
* Singleton for Retrofit instance
*/
public class WhatsOpenClient {
private static volatile Retrofit sRetrofit = null;
private static WhatsOpenService mService;
private static final String BASE_URL = "https://whatsopen.gmu.edu/api/";
public WhatsOpenClient() {
}
// Returns a singleton of WhatsOpenService
public static WhatsOpenService getInstance() {
if(mService == null) {
synchronized (WhatsOpenClient.class) {
if(mService == null) {
mService = getRetrofit().create(WhatsOpenService.class);
}
}
}
return mService;
}
// Configures Retrofit's JSON parsing logic
private synchronized static Retrofit getRetrofit() {
if(sRetrofit == null) {
synchronized(WhatsOpenClient.class) {
if(sRetrofit == null) {
Gson gson = new GsonBuilder()
// Ensures Retrofit plays nicely with Realm
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaredClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.registerTypeAdapter(OpenTimes.class, new OpenTimesDeserializer())
.registerTypeAdapter(new TypeToken<RealmList<OpenTimes>>(){}.getType(),
new OpenTimesListDeserializer())
.create();
sRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
}
}
return sRetrofit;
}
}
package srct.whatsopen.services;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.realm.RealmObject;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Singleton for Retrofit instance
*/
public class WhatsOpenClient {
private static volatile Retrofit sRetrofit = null;
private static WhatsOpenService mService;
private static final String BASE_URL = "https://whatsopen.gmu.edu/api/";
public WhatsOpenClient() {
}
// Returns a singleton of WhatsOpenService
public static WhatsOpenService getInstance() {
if(mService == null) {
synchronized (WhatsOpenClient.class) {
if(mService == null) {
mService = getRetrofit().create(WhatsOpenService.class);
}
}
}
return mService;
}
// Configures Retrofit's JSON parsing logic
private synchronized static Retrofit getRetrofit() {
if(sRetrofit == null) {
synchronized(WhatsOpenClient.class) {
if(sRetrofit == null) {
Gson gson = new GsonBuilder()
// Ensures Retrofit plays nicely with Realm
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaredClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();
sRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
}
}
return sRetrofit;
}
}
package srct.whatsopen.service;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import srct.whatsopen.model.Facility;
// Interface for Retrofit's Http request
public interface WhatsOpenService {
@GET("schedules")
Call<List<Facility>> facilityList();
}
package srct.whatsopen.services;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import srct.whatsopen.model.Facility;
// Interface for Retrofit's Http request
public interface WhatsOpenService {
@GET("facilities")
Call<List<Facility>> facilityList();
}
......@@ -16,8 +16,8 @@ import retrofit2.Callback;
import retrofit2.Response;
import srct.whatsopen.R;
import srct.whatsopen.service.WhatsOpenClient;
import srct.whatsopen.service.WhatsOpenService;
import srct.whatsopen.services.WhatsOpenClient;
import srct.whatsopen.services.WhatsOpenService;
import srct.whatsopen.model.Facility;
public class MainActivity extends AppCompatActivity {
......
package srct.whatsopen.util;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import srct.whatsopen.model.OpenTimes;
// Deserializer for a nested Json object, OpenTimes
public class OpenTimesDeserializer implements JsonDeserializer<OpenTimes> {
@Override
public OpenTimes deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException
{
JsonElement openTimes = je.getAsJsonObject().get("open_time");
return new Gson().fromJson(openTimes, OpenTimes.class);
}
}
package srct.whatsopen.util;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import io.realm.RealmList;
import srct.whatsopen.model.OpenTimes;
// Json Deserializer for a list of OpenTimes
public class OpenTimesListDeserializer implements JsonDeserializer<RealmList<OpenTimes>> {
@Override
public RealmList<OpenTimes> deserialize(JsonElement json, Type type, JsonDeserializationContext
jdc) throws JsonParseException {
RealmList<OpenTimes> openTimes = new RealmList<>();
JsonArray ja = json.getAsJsonArray();
for(JsonElement je : ja) {
openTimes.add((OpenTimes) jdc.deserialize(je, OpenTimes.class));
}
return openTimes;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment