Still unfinished JSON converter

This commit is contained in:
Caesar2011
2019-01-04 15:02:07 +01:00
committed by Sebastian Seedorf
parent bb85911324
commit e0ed23dec5
9 changed files with 149 additions and 64 deletions

View File

@@ -6,11 +6,12 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import de.sebse.fuplanner.tools.UtilsJson; import de.sebse.fuplanner.tools.UtilsJson;
public class Announcement implements UtilsJson.JsonConvertable { public class Announcement implements UtilsJson.JsonConvertible {
private final String id; private final String id;
private final String title; private final String title;
private final String body; private final String body;
@@ -27,6 +28,19 @@ public class Announcement implements UtilsJson.JsonConvertable {
this.urls = urls; this.urls = urls;
} }
public Announcement(JSONObject json) throws JSONException {
this.id = json.getString("id");
this.title = json.getString("title");
this.body = json.getString("body");
this.createdBy = json.getString("createdBy");
this.createdOn = json.getLong("createdOn");
ArrayList<String> urls = new ArrayList<>();
for (String url: UtilsJson.jsonArrayToIterableString(json.getJSONArray("urls"))) {
urls.add(url);
}
this.urls = urls;
}
public ArrayList<String> getUrls() { public ArrayList<String> getUrls() {
return urls; return urls;
} }

View File

@@ -5,13 +5,12 @@ import com.google.android.gms.common.internal.Objects;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import de.sebse.fuplanner.tools.UtilsJson; import de.sebse.fuplanner.tools.UtilsJson;
public class Assignment implements UtilsJson.JsonConvertable { public class Assignment implements UtilsJson.JsonConvertible {
private final String id; private final String id;
private final String title; private final String title;
private final long dueTime; private final long dueTime;

View File

@@ -5,7 +5,6 @@ import com.google.android.gms.common.internal.Objects;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.Serializable;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@@ -14,7 +13,7 @@ import androidx.annotation.NonNull;
import de.sebse.fuplanner.tools.ColorRGB; import de.sebse.fuplanner.tools.ColorRGB;
import de.sebse.fuplanner.tools.UtilsJson; import de.sebse.fuplanner.tools.UtilsJson;
public class Event implements UtilsJson.JsonConvertable { public class Event implements UtilsJson.JsonConvertible {
private final String siteId; private final String siteId;
private final String id; private final String id;
private final String type; private final String type;

View File

@@ -5,12 +5,10 @@ import com.google.android.gms.common.internal.Objects;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.Serializable;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import de.sebse.fuplanner.tools.UtilsJson; import de.sebse.fuplanner.tools.UtilsJson;
public class Grade implements UtilsJson.JsonConvertable { public class Grade implements UtilsJson.JsonConvertible {
private final String itemName; private final String itemName;
private final double grade; private final double grade;
private final double maxPoints; private final double maxPoints;

View File

@@ -11,7 +11,7 @@ import java.util.regex.Pattern;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import de.sebse.fuplanner.tools.UtilsJson; import de.sebse.fuplanner.tools.UtilsJson;
public class Lecturer implements UtilsJson.JsonConvertable { public class Lecturer implements UtilsJson.JsonConvertible {
private final String firstName; private final String firstName;
private final String surname; private final String surname;
private final String mail; private final String mail;
@@ -29,6 +29,13 @@ public class Lecturer implements UtilsJson.JsonConvertable {
this.isResponsible = matcher.group(4).equals("true"); this.isResponsible = matcher.group(4).equals("true");
} }
public Lecturer(JSONObject json) throws JSONException {
this.firstName = json.getString("firstName");
this.surname = json.getString("surname");
this.mail = json.getString("mail");
this.isResponsible = json.getBoolean("isResponsible");
}
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
} }

View File

@@ -11,11 +11,14 @@ import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@@ -26,6 +29,7 @@ import androidx.annotation.Nullable;
import de.sebse.fuplanner.tools.UtilsJson; import de.sebse.fuplanner.tools.UtilsJson;
import static de.sebse.fuplanner.tools.UtilsJson.collectionToJson; import static de.sebse.fuplanner.tools.UtilsJson.collectionToJson;
import static de.sebse.fuplanner.tools.UtilsJson.jsonArrayToIterableObject;
/** /**
* Created by sebastian on 29.10.17. * Created by sebastian on 29.10.17.
@@ -79,8 +83,7 @@ public class Modules implements Iterable<Modules.Module> {
public static Modules load(Context context) throws IOException { public static Modules load(Context context) throws IOException {
String ret = ""; String ret = "";
InputStream inputStream = context.openFileInput(FILE_NAME_TIMESTAMP); InputStream inputStream = context.openFileInput(FILE_NAME);
if (inputStream != null) { if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream); InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
@@ -97,11 +100,13 @@ public class Modules implements Iterable<Modules.Module> {
JSONObject json = new JSONObject(ret); JSONObject json = new JSONObject(ret);
Modules mod = new Modules(json.getString("username")); Modules mod = new Modules(json.getString("username"));
JSONArray arr = json.getJSONArray("modules"); JSONArray arr = json.getJSONArray("modules");
for (JSONObject jsonObject: jsonArrayToIterableObject(arr)) {
for (int i = 0; i < arr.length(); i++) { mod.list.add(new Module(jsonObject));
mod.list.add(Module.);
} }
return mod FileInputStream ofi = context.openFileInput(FILE_NAME_TIMESTAMP);
ObjectInputStream inputStream1 = new ObjectInputStream(ofi);
mod.mLoadTime = inputStream1.readLong();
return mod;
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -110,18 +115,20 @@ public class Modules implements Iterable<Modules.Module> {
public void save(Context context) throws IOException { public void save(Context context) throws IOException {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONArray jModules = new JSONArray();
try { try {
json.put("username", mUsername); json.put("username", mUsername);
json.put("modules", UtilsJson.collectionToJson(list)); json.put("modules", UtilsJson.collectionToJson(list));
OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE));
osw.write(json.toString());
osw.close();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
mLoadTime = System.currentTimeMillis(); mLoadTime = System.currentTimeMillis();
OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE)); FileOutputStream ofo = context.openFileOutput(FILE_NAME_TIMESTAMP, Context.MODE_PRIVATE);
osw.write(json.toString()); ObjectOutputStream outputStream = new ObjectOutputStream(ofo);
osw.close(); outputStream.writeLong(mLoadTime);
} }
public void delete(Context context) { public void delete(Context context) {
@@ -162,7 +169,7 @@ public class Modules implements Iterable<Modules.Module> {
return o1.hashCode() == o2.hashCode(); return o1.hashCode() == o2.hashCode();
} }
public class Module implements UtilsJson.JsonConvertable { public static class Module implements UtilsJson.JsonConvertible {
@Nullable public final Semester semester; @Nullable public final Semester semester;
@NotNull final HashSet<String> lvNumber; @NotNull final HashSet<String> lvNumber;
@NotNull public final String title; @NotNull public final String title;
@@ -226,25 +233,69 @@ public class Modules implements Iterable<Modules.Module> {
this.ID = ID; this.ID = ID;
} }
private Module(JSONObject json) throws JSONException { private Module(@NotNull JSONObject json) throws JSONException {
HashSet hs = new HashSet<>(); HashSet<String> lv = new HashSet<>();
Iterator<Object> it = UtilsJson.jsonArrayToIterator(json.getJSONArray("lvNumber")); for (String number: UtilsJson.jsonArrayToIterableString(json.getJSONArray("lvNumber"))) {
while (it.hasNext()) { lv.add(number);
hs.
} }
Module(new Semester(json.getJSONObject("semester")), LinkedHashSet<Lecturer> lecturer = new LinkedHashSet<>();
json.getString("lvNumber"), for (JSONObject l: UtilsJson.jsonArrayToIterableObject(json.getJSONArray("lecturer"))) {
json.getString("title"), lecturer.add(new Lecturer(l));
json.getJSONObject("lvNumber"), }
JSONObject obj = json.optJSONObject("semester");
json.getString("type"), this.semester = obj != null ? new Semester(obj) : null;
json.getString("description"), this.lvNumber = lv;
json.getString("ID")); this.title = json.getString("title");
json.put("announcements", collectionToJson(announcements)); this.lecturer = new ArrayList<>(lecturer);
json.put("assignments", collectionToJson(assignments)); this.type = json.getString("type");
json.put("events", collectionToJson(events)); this.description = json.getString("description");
json.put("gradebook", collectionToJson(gradebook)); this.ID = json.getString("ID");
json.put("resources", collectionToJson(resources));
JSONArray arr = json.optJSONArray("announcements");
if (obj != null) {
ArrayList<Announcement> announce = new ArrayList<>();
for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) {
announce.add(new Announcement(l));
}
this.announcements = announce;
}
arr = json.optJSONArray("assignments");
if (obj != null) {
AssignmentList assign = new AssignmentList();
for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) {
assign.add(new Assignment(l));
}
this.assignments = assign;
}
arr = json.optJSONArray("events");
if (obj != null) {
EventList events = new EventList();
for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) {
events.add(new Event(l));
}
this.events = events;
}
arr = json.optJSONArray("gradebook");
if (obj != null) {
ArrayList<Grade> grades = new ArrayList<>();
for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) {
grades.add(new Grade(l));
}
this.gradebook = grades;
}
arr = json.optJSONArray("resources");
if (obj != null) {
ArrayList<Resource> res = new ArrayList<>();
for (JSONObject l: UtilsJson.jsonArrayToIterableObject(arr)) {
if (l.getString("restype").equals("file"))
res.add(new Resource.File(l));
else
res.add(new Resource.Folder(l));
}
this.resources = res;
}
} }
@NonNull @NonNull

View File

@@ -5,7 +5,6 @@ import com.google.android.gms.common.internal.Objects;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import androidx.annotation.LayoutRes; import androidx.annotation.LayoutRes;
@@ -16,7 +15,7 @@ import de.sebse.fuplanner.tools.ui.treeview.LayoutItemType;
import de.sebse.fuplanner.tools.ui.treeview.TreeNode; import de.sebse.fuplanner.tools.ui.treeview.TreeNode;
public abstract class Resource implements UtilsJson.JsonConvertable { public abstract class Resource implements UtilsJson.JsonConvertible {
final String author; final String author;
final long modifiedDate; final long modifiedDate;

View File

@@ -3,13 +3,11 @@ package de.sebse.fuplanner.services.kvv.types;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.Serializable;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import de.sebse.fuplanner.tools.Regex; import de.sebse.fuplanner.tools.Regex;
import de.sebse.fuplanner.tools.UtilsJson; import de.sebse.fuplanner.tools.UtilsJson;
public class Semester implements UtilsJson.JsonConvertable { public class Semester implements UtilsJson.JsonConvertible {
public static final int SEM_WS = 1; public static final int SEM_WS = 1;
public static final int SEM_SS = 2; public static final int SEM_SS = 2;
private int type; private int type;
@@ -21,25 +19,17 @@ public class Semester implements UtilsJson.JsonConvertable {
} }
public Semester(String semester_string) throws NoSuchFieldException { public Semester(String semester_string) throws NoSuchFieldException {
/*Semester sem = null;
if (semester != null) {
sem = new Semester(semester)
semester = semester.replace("SS", "S");
semester = semester.replaceAll("[0-9]{2}([0-9]{2})", "$1");
}*/
/*String s1type = Regex.regex("^(S|WS) ", a);
int s1year = Integer.parseInt(Regex.regex("^(S|WS) ([0-9]{2})", a, 2));
String s2type = Regex.regex("^(S|WS) ", b);
int s2year = Integer.parseInt(Regex.regex("^(S|WS) ([0-9]{2})", b, 2));*/
String type = Regex.regex("^(SS|WS) ", semester_string); String type = Regex.regex("^(SS|WS) ", semester_string);
String year = Regex.regex("^(SS|WS) ([0-9]{2})", semester_string, 2); String year = Regex.regex("^(SS|WS) ([0-9]{2})", semester_string, 2);
this.type = type.equals("SS") ? SEM_SS : SEM_WS; this.type = type.equals("SS") ? SEM_SS : SEM_WS;
this.year = Integer.parseInt(year); this.year = Integer.parseInt(year);
} }
public Semester(JSONObject json) throws JSONException {
this.type = json.getInt("type");
this.year = json.getInt("year");
}
public int getType() { public int getType() {
return type; return type;
} }

View File

@@ -9,9 +9,8 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import de.sebse.fuplanner.services.kvv.types.Modules;
import de.sebse.fuplanner.services.kvv.types.SortedListModule;
public class UtilsJson { public class UtilsJson {
public static JSONArray collectionToJson(@Nullable HashSet<String> coll) { public static JSONArray collectionToJson(@Nullable HashSet<String> coll) {
@@ -24,7 +23,7 @@ public class UtilsJson {
return arr; return arr;
} }
public static <E extends UtilsJson.JsonConvertable> JSONArray collectionToJson(@Nullable Collection<E> coll) throws JSONException { public static <E extends JsonConvertible> JSONArray collectionToJson(@Nullable Collection<E> coll) throws JSONException {
if (coll == null) if (coll == null)
return null; return null;
JSONArray arr = new JSONArray(); JSONArray arr = new JSONArray();
@@ -44,7 +43,7 @@ public class UtilsJson {
return arr; return arr;
} }
public static <A extends UtilsJson.JsonConvertable> JSONArray collectionToJson(@Nullable SortedList<A, ?, ?> coll) throws JSONException { public static <A extends JsonConvertible> JSONArray collectionToJson(@Nullable SortedList<A, ?, ?> coll) throws JSONException {
if (coll == null) if (coll == null)
return null; return null;
JSONArray arr = new JSONArray(); JSONArray arr = new JSONArray();
@@ -54,8 +53,8 @@ public class UtilsJson {
return arr; return arr;
} }
public static Iterator<Object> jsonArrayToIterator(JSONArray array) { public static <T> Iterator<T> jsonArrayToIterator(JSONArray array, GetInterface<T> func) {
return new Iterator<Object>() { return new Iterator<T>() {
int size = array == null ? 0 : array.length(); int size = array == null ? 0 : array.length();
int pos = 0; int pos = 0;
@Override @Override
@@ -64,18 +63,47 @@ public class UtilsJson {
} }
@Override @Override
public Object next() { public T next() {
try { try {
return array.get(pos++); return func.get(array, pos++);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
};
}
@FunctionalInterface
private interface GetInterface<T> {
T get(JSONArray array, int index) throws JSONException;
}
public static JsonIterable<JSONObject> jsonArrayToIterableObject(JSONArray array) {
return new JsonIterable<>(array, JSONArray::getJSONObject);
}
public static JsonIterable<String> jsonArrayToIterableString(JSONArray array) {
return new JsonIterable<>(array, JSONArray::getString);
}
public static class JsonIterable<T> implements Iterable<T> {
private JSONArray array;
private GetInterface<T> getInterface;
JsonIterable(JSONArray array, GetInterface<T> getInterface) {
this.array = array;
this.getInterface = getInterface;
}
@NonNull
@Override
public Iterator<T> iterator() {
return UtilsJson.jsonArrayToIterator(array, getInterface);
} }
} }
public interface JsonConvertable { public interface JsonConvertible {
JSONObject toJSONObject() throws JSONException; JSONObject toJSONObject() throws JSONException;
} }
} }