mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-21 05:05:31 +00:00
# deserialization for event, coursearea, venue
This commit is contained in:
@@ -11,3 +11,4 @@ sailing_debug0*
|
||||
/mobile\com.sap.sailing.racecommittee.glue\bin/com.sap.sailing.racecommittee.glue.jar
|
||||
/mobile\com.sap.sailing.racecommittee.glue\bin/jarlist.cache
|
||||
/mobile/com.sap.sailing.racecommittee.glue/bin/com.sap.sailing.racecommittee.glue.jar
|
||||
/java/.settings/org.eclipse.jdt.core.prefs
|
||||
|
||||
@@ -36,7 +36,7 @@ public class EventImpl implements Event {
|
||||
this.regattas = new HashSet<Regatta>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public Serializable getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.sap.sailing.server.gateway.deserialization;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.domain.base.CourseArea;
|
||||
import com.sap.sailing.domain.base.impl.CourseAreaImpl;
|
||||
import com.sap.sailing.server.gateway.serialization.CourseAreaJsonSerializer;
|
||||
|
||||
public class CourseAreaJsonDeserializer implements JsonDeserializer<CourseArea> {
|
||||
|
||||
@Override
|
||||
public CourseArea deserialize(JSONObject object)
|
||||
throws JsonDeserializationException {
|
||||
String name = object.get(CourseAreaJsonSerializer.FIELD_NAME).toString();
|
||||
|
||||
CourseArea courseArea = new CourseAreaImpl(name);
|
||||
|
||||
/*JSONArray racesArray = Helpers.getNestedArraySafe(object, CourseAreaJsonSerializer.FIELD_RACES);
|
||||
for (Object element : racesArray) {
|
||||
/// TODO: add races to courseArea...
|
||||
System.out.println(element.toString());
|
||||
}*/
|
||||
|
||||
return courseArea;
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.sap.sailing.server.gateway.deserialization;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.domain.base.Event;
|
||||
import com.sap.sailing.domain.base.Venue;
|
||||
import com.sap.sailing.domain.base.impl.EventImpl;
|
||||
import com.sap.sailing.server.gateway.serialization.EventJsonSerializer;
|
||||
|
||||
public class EventJsonDeserializer implements JsonDeserializer<Event> {
|
||||
private JsonDeserializer<Venue> venueDeserializer;
|
||||
|
||||
public EventJsonDeserializer(JsonDeserializer<Venue> venueDeserializer) {
|
||||
this.venueDeserializer = venueDeserializer;
|
||||
}
|
||||
|
||||
public Event deserialize(JSONObject object) throws JsonDeserializationException {
|
||||
String id = object.get(EventJsonSerializer.FIELD_ID).toString();
|
||||
String name = object.get(EventJsonSerializer.FIELD_NAME).toString();
|
||||
String publicationUrl = object.get(EventJsonSerializer.FIELD_PUBLICATION_URL).toString();
|
||||
|
||||
UUID uuid = null;
|
||||
try {
|
||||
uuid = UUID.fromString(id);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
throw new JsonDeserializationException(
|
||||
String.format("Field %s with %s couldn't be parsed as UUID.", EventJsonSerializer.FIELD_ID, id),
|
||||
iae);
|
||||
}
|
||||
|
||||
JSONObject venueObject = Helpers.getNestedObjectSafe(
|
||||
object,
|
||||
EventJsonSerializer.FIELD_VENUE);
|
||||
Venue venue = venueDeserializer.deserialize(venueObject);
|
||||
|
||||
return new EventImpl(
|
||||
name,
|
||||
venue,
|
||||
publicationUrl,
|
||||
true,
|
||||
uuid);
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.sap.sailing.server.gateway.deserialization;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Helpers {
|
||||
|
||||
public static JSONObject toJSONObjectSafe(Object object)
|
||||
throws JsonDeserializationException {
|
||||
if (object instanceof JSONObject) {
|
||||
return (JSONObject) object;
|
||||
}
|
||||
throw new JsonDeserializationException(
|
||||
String.format("Expected a JSONObject, got %s.", object.getClass().getName()));
|
||||
}
|
||||
|
||||
public static JSONObject getNestedObjectSafe(JSONObject parent, String fieldName)
|
||||
throws JsonDeserializationException {
|
||||
Object childObject = parent.get(fieldName);
|
||||
if (!(childObject instanceof JSONObject)) {
|
||||
throw new JsonDeserializationException(
|
||||
String.format("Field %s with %s wasn't a nested JSON object.",
|
||||
fieldName, childObject.toString()));
|
||||
}
|
||||
return (JSONObject) childObject;
|
||||
}
|
||||
|
||||
public static JSONArray getNestedArraySafe(JSONObject parent, String fieldName)
|
||||
throws JsonDeserializationException {
|
||||
Object childObject = parent.get(fieldName);
|
||||
if (!(childObject instanceof JSONArray)) {
|
||||
throw new JsonDeserializationException(
|
||||
String.format("Field %s with %s wasn't a nested JSON array.",
|
||||
fieldName, childObject.toString()));
|
||||
}
|
||||
return (JSONArray) childObject;
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.sap.sailing.server.gateway.deserialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class JsonDeserializationException extends IOException {
|
||||
private static final long serialVersionUID = -6725762788023063937L;
|
||||
|
||||
public JsonDeserializationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public JsonDeserializationException(String message, Throwable innerException) {
|
||||
super(message, innerException);
|
||||
}
|
||||
|
||||
public JsonDeserializationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JsonDeserializationException(Throwable innerException) {
|
||||
super(innerException);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.sap.sailing.server.gateway.deserialization;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public interface JsonDeserializer<T> {
|
||||
|
||||
T deserialize(JSONObject object) throws JsonDeserializationException;
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.sap.sailing.server.gateway.deserialization;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.domain.base.CourseArea;
|
||||
import com.sap.sailing.domain.base.Venue;
|
||||
import com.sap.sailing.domain.base.impl.VenueImpl;
|
||||
import com.sap.sailing.server.gateway.serialization.VenueJsonSerializer;
|
||||
|
||||
public class VenueJsonDeserializer implements JsonDeserializer<Venue> {
|
||||
private JsonDeserializer<CourseArea> courseAreaDeserializer;
|
||||
|
||||
public VenueJsonDeserializer(JsonDeserializer<CourseArea> courseAreaDeserializer) {
|
||||
this.courseAreaDeserializer = courseAreaDeserializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Venue deserialize(JSONObject object)
|
||||
throws JsonDeserializationException {
|
||||
|
||||
String name = object.get(VenueJsonSerializer.FIELD_NAME).toString();
|
||||
Venue venue = new VenueImpl(name);
|
||||
|
||||
JSONArray courseAreaArray = Helpers.getNestedArraySafe(
|
||||
object,
|
||||
VenueJsonSerializer.FIELD_COURSE_AREAS);
|
||||
for (Object element : courseAreaArray) {
|
||||
JSONObject courseAreaObject = Helpers.toJSONObjectSafe(element);
|
||||
venue.addCourseArea(courseAreaDeserializer.deserialize(courseAreaObject));
|
||||
}
|
||||
|
||||
return venue;
|
||||
}
|
||||
|
||||
}
|
||||
+2
-4
@@ -1,10 +1,8 @@
|
||||
package com.sap.sailing.server.gateway.serialization;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.domain.base.CourseArea;
|
||||
import com.sap.sailing.domain.base.RaceDefinition;
|
||||
|
||||
public class CourseAreaJsonSerializer implements JsonSerializer<CourseArea> {
|
||||
public static final String FIELD_NAME = "name";
|
||||
@@ -15,12 +13,12 @@ public class CourseAreaJsonSerializer implements JsonSerializer<CourseArea> {
|
||||
JSONObject result = new JSONObject();
|
||||
|
||||
result.put(FIELD_NAME, object.getName());
|
||||
JSONArray races = new JSONArray();
|
||||
/*JSONArray races = new JSONArray();
|
||||
for (RaceDefinition race : object.getRaces())
|
||||
{
|
||||
races.add(race.getId().toString());
|
||||
}
|
||||
result.put(FIELD_RACES, races);
|
||||
result.put(FIELD_RACES, races);*/
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user