Bug 4104: Save timepoints as long instead of a string in JSON.

This commit is contained in:
Henri Kohlberg committed 2018-09-12 11:06:20 +02:00
1 parent be101a6a66
commit 4c03df87f0
3 files changed
+49 -38

No files matched your search

@@ -79,29 +79,21 @@ public class TagDTO implements Serializable {
*
* @param timepoint
* {@link TimePoint} to be serialized
* @return serialized string
* @return serialized timepoint as long, <code>0</code> if <code>timepoint</code> is <code>null</code>
*/
public String serializeTimePoint(TimePoint timepoint) {
if (timepoint != null) {
return Long.toString(timepoint.asMillis());
} else {
return "";
}
public long serializeTimePoint(TimePoint timepoint) {
return timepoint == null ? 0 : timepoint.asMillis();
}
/**
* Deserializes string to {@link TimePoint}.
* Deserializes long to {@link MillisecondsTimePoint}.
*
* @param timepoint
* timepoint to be deserialized
* @return {@link TimePoint}
*/
public TimePoint deserilizeTimePoint(String timepoint) {
if (!timepoint.isEmpty()) {
return new MillisecondsTimePoint(Long.parseLong(timepoint));
} else {
return null;
}
public TimePoint deserilizeTimePoint(long timepoint) {
return new MillisecondsTimePoint(timepoint);
}
/**
@@ -5,6 +5,7 @@ import java.util.List;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
@@ -33,9 +34,9 @@ public class TagDTODeSerializer extends TagDTO.TagDeSerializer {
result.put(FIELD_IMAGE_URL, new JSONString(tag.getImageURL()));
result.put(FIELD_USERNAME, new JSONString(tag.getUsername()));
result.put(FIELD_VISIBLE_FOR_PUBLIC, JSONBoolean.getInstance(tag.isVisibleForPublic()));
result.put(FIELD_RACE_TIMEPOINT, new JSONString(serializeTimePoint(tag.getRaceTimepoint())));
result.put(FIELD_CREATED_AT, new JSONString(serializeTimePoint(tag.getCreatedAt())));
result.put(FIELD_REVOKED_AT, new JSONString(serializeTimePoint(tag.getRevokedAt())));
result.put(FIELD_RACE_TIMEPOINT, new JSONNumber(serializeTimePoint(tag.getRaceTimepoint())));
result.put(FIELD_CREATED_AT, new JSONNumber(serializeTimePoint(tag.getCreatedAt())));
result.put(FIELD_REVOKED_AT, new JSONNumber(serializeTimePoint(tag.getRevokedAt())));
return result;
}
@@ -47,17 +48,29 @@ public class TagDTODeSerializer extends TagDTO.TagDeSerializer {
* @return {@link TagDTO tag}
*/
private TagDTO deserialize(JSONObject jsonObject) {
JSONString tag = (JSONString) jsonObject.get(FIELD_TAG);
JSONString comment = (JSONString) jsonObject.get(FIELD_COMMENT);
JSONString imageURL = (JSONString) jsonObject.get(FIELD_IMAGE_URL);
JSONString username = (JSONString) jsonObject.get(FIELD_USERNAME);
boolean visibleForPublic = Boolean.valueOf(jsonObject.get(FIELD_VISIBLE_FOR_PUBLIC).toString());
TimePoint raceTimePoint = deserilizeTimePoint(
((JSONString) (jsonObject.get(FIELD_RACE_TIMEPOINT))).stringValue());
TimePoint createdAt = deserilizeTimePoint(((JSONString) (jsonObject.get(FIELD_CREATED_AT))).stringValue());
TimePoint revokedAt = deserilizeTimePoint(((JSONString) (jsonObject.get(FIELD_REVOKED_AT))).stringValue());
return new TagDTO(tag.stringValue(), comment.stringValue(), imageURL.stringValue(), username.stringValue(),
visibleForPublic, raceTimePoint, createdAt, revokedAt);
// if deserializing throws an error, return null
try {
JSONString tag = (JSONString) jsonObject.get(FIELD_TAG);
JSONString comment = (JSONString) jsonObject.get(FIELD_COMMENT);
JSONString imageURL = (JSONString) jsonObject.get(FIELD_IMAGE_URL);
JSONString username = (JSONString) jsonObject.get(FIELD_USERNAME);
boolean visibleForPublic = Boolean.valueOf(jsonObject.get(FIELD_VISIBLE_FOR_PUBLIC).toString());
// GWT-JSON-Library only supports its own types such as JSONNumber, which is saved as double caused by
// JavaScript not supporting 64-bit long numbers. TimePoints are saved as long values in Java, therefor
// conversion from JSONObject -> JSONValue -> JSONNumber -> Double -> Long is needed.
// TODO: Find better way of type conversion
TimePoint raceTimePoint = deserilizeTimePoint(
Double.valueOf(((JSONNumber) (jsonObject.get(FIELD_RACE_TIMEPOINT))).doubleValue()).longValue());
TimePoint createdAt = deserilizeTimePoint(
Double.valueOf(((JSONNumber) (jsonObject.get(FIELD_CREATED_AT))).doubleValue()).longValue());
TimePoint revokedAt = deserilizeTimePoint(
Double.valueOf(((JSONNumber) (jsonObject.get(FIELD_REVOKED_AT))).doubleValue()).longValue());
return new TagDTO(tag.stringValue(), comment.stringValue(), imageURL.stringValue(), username.stringValue(),
visibleForPublic, raceTimePoint, createdAt, revokedAt);
} catch (Exception e) {
return null;
}
}
@Override
@@ -79,18 +79,24 @@ public class TagsResource extends AbstractSailingServerResource {
*
* @param jsonObject
* {@link JSONObject jsonObject} to be deserialized
* @return {@link TagDTO tag}
* @return <code>null</code> if <code>jsonObject</code> is no valid json for a tag, otherwise {@link TagDTO tag}
*/
private TagDTO deserialize(JSONObject jsonObject) {
String tag = (String) jsonObject.get(FIELD_TAG);
String comment = (String) jsonObject.get(FIELD_COMMENT);
String imageURL = (String) jsonObject.get(FIELD_IMAGE_URL);
String username = (String) jsonObject.get(FIELD_USERNAME);
boolean visibleForPublic = (Boolean) jsonObject.get(FIELD_VISIBLE_FOR_PUBLIC);
TimePoint raceTimePoint = deserilizeTimePoint((String) (jsonObject.get(FIELD_RACE_TIMEPOINT)));
TimePoint createdAt = deserilizeTimePoint((String) (jsonObject.get(FIELD_CREATED_AT)));
TimePoint revokedAt = deserilizeTimePoint((String) (jsonObject.get(FIELD_REVOKED_AT)));
return new TagDTO(tag, comment, imageURL, username, visibleForPublic, raceTimePoint, createdAt, revokedAt);
// if deserializing throws an error, return null
try {
String tag = (String) jsonObject.get(FIELD_TAG);
String comment = (String) jsonObject.get(FIELD_COMMENT);
String imageURL = (String) jsonObject.get(FIELD_IMAGE_URL);
String username = (String) jsonObject.get(FIELD_USERNAME);
boolean visibleForPublic = (Boolean) jsonObject.get(FIELD_VISIBLE_FOR_PUBLIC);
TimePoint raceTimePoint = deserilizeTimePoint((Long) (jsonObject.get(FIELD_RACE_TIMEPOINT)));
TimePoint createdAt = deserilizeTimePoint((Long) (jsonObject.get(FIELD_CREATED_AT)));
TimePoint revokedAt = deserilizeTimePoint((Long) (jsonObject.get(FIELD_REVOKED_AT)));
return new TagDTO(tag, comment, imageURL, username, visibleForPublic, raceTimePoint, createdAt,
revokedAt);
} catch (Exception e) {
return null;
}
}
/**