Permalink
Browse files

Using Gson serialization.

  • Loading branch information...
1 parent 562fb53 commit 7edb80281c07402cf95d95ac8e4a1c41e732c911 Andrew Kirilenko committed Mar 23, 2012
View
@@ -29,10 +29,5 @@
<artifactId>commons-io</artifactId>
<version>[1.3.0,1.4.0)</version>
</dependency>
- <dependency>
- <groupId>joda-time</groupId>
- <artifactId>joda-time</artifactId>
- <version>[2.0,2.2)</version>
- </dependency>
</dependencies>
</project>
@@ -29,9 +29,11 @@ public APIClient getAPI() {
JsonObject codes = api.codesList(options);
List<CodeEntity> codesList = new ArrayList<CodeEntity>();
+
+ Gson gson = new Gson();
for (JsonElement code : codes.get("codes").getAsJsonArray()) {
- codesList.add(CodeEntity.fromJsonObject(code.getAsJsonObject()));
+ codesList.add(gson.fromJson(code, CodeEntity.class));
}
return codesList;
@@ -46,7 +48,7 @@ public APIClient getAPI() {
}
public CodeEntity getCode(String codeId) throws APIException {
- return CodeEntity.fromJsonObject(api.codesGet(codeId));
+ return (new Gson()).fromJson(api.codesGet(codeId), CodeEntity.class);
}
public void createCode(BaseCode code) throws APIException {
@@ -62,8 +64,10 @@ public void deleteCode(String codeId) throws APIException {
List<CodeRevisionEntity> codeRevisionsList = new ArrayList<CodeRevisionEntity>();
+ Gson gson = new Gson();
+
for (JsonElement codeRevision : codeRevisions.get("revisions").getAsJsonArray()) {
- codeRevisionsList.add(CodeRevisionEntity.fromJsonObject(codeRevision.getAsJsonObject()));
+ codeRevisionsList.add(gson.fromJson(codeRevision, CodeRevisionEntity.class));
}
return codeRevisionsList;
@@ -94,8 +98,10 @@ public void deleteCode(String codeId) throws APIException {
List<TaskEntity> tasksList = new ArrayList<TaskEntity>();
+ Gson gson = new Gson();
+
for (JsonElement task : tasks.get("tasks").getAsJsonArray()) {
- tasksList.add(TaskEntity.fromJsonObject(task.getAsJsonObject()));
+ tasksList.add(gson.fromJson(task, TaskEntity.class));
}
return tasksList;
@@ -110,7 +116,7 @@ public void deleteCode(String codeId) throws APIException {
}
public TaskEntity getTask(String taskId) throws APIException {
- return TaskEntity.fromJsonObject(api.tasksGet(taskId));
+ return (new Gson()).fromJson(api.tasksGet(taskId), TaskEntity.class);
}
public TaskEntity createTask(String codeName, Map<String, Object> params, Map<String, Object> options) throws APIException {
@@ -121,7 +127,7 @@ public TaskEntity createTask(String codeName, Map<String, Object> params, Map<St
JsonObject tasks = api.tasksCreate(codeName, (new Gson()).toJson(Params.create("token", api.getToken(), "project_id", api.getProjectId(), "params", params)), options);
JsonObject task = tasks.get("tasks").getAsJsonArray().get(0).getAsJsonObject();
- return TaskEntity.fromJsonObject(task);
+ return (new Gson()).fromJson(task, TaskEntity.class);
}
public TaskEntity createTask(String codeName, Map<String, Object> params, TaskOptionsObject options) throws APIException {
@@ -1,35 +0,0 @@
-package io.iron.ironworker.client.entities;
-
-import com.google.gson.JsonObject;
-import org.joda.time.format.ISODateTimeFormat;
-
-import java.util.Date;
-
-public class BaseEntity {
- public static String parseString(JsonObject o, String field, String def) {
- return o.has(field) ? o.get(field).getAsString() : def;
- }
-
- public static String parseString(JsonObject o, String field) {
- return parseString(o, field, null);
- }
-
- public static int parseInt(JsonObject o, String field, int def) {
- return o.has(field) ? o.get(field).getAsInt() : def;
- }
-
- public static int parseInt(JsonObject o, String field) {
- return parseInt(o, field, 0);
- }
-
- public static Date parseDate(JsonObject o, String field, Date def) {
- return o.has(field) ? ISODateTimeFormat.dateTimeNoMillis().parseDateTime(o.get(field).getAsString()).toDate() : def;
- }
-
- public static Date parseDate(JsonObject o, String field) {
- return parseDate(o, field, null);
- }
-
- protected BaseEntity() {
- }
-}
@@ -1,38 +1,31 @@
package io.iron.ironworker.client.entities;
-import com.google.gson.JsonObject;
+import com.google.gson.annotations.SerializedName;
import java.util.Date;
-public class CodeEntity extends BaseEntity {
+public class CodeEntity {
+ @SerializedName("id")
String id;
+ @SerializedName("project_id")
String projectId;
+ @SerializedName("name")
String name;
+ @SerializedName("runtime")
String runtime;
+ @SerializedName("rev")
int revision;
+ @SerializedName("latest_history_id")
String latestHistoryId;
+ @SerializedName("latest_checksum")
String latestChecksum;
+ @SerializedName("latest_change")
Date latestChange;
+ @SerializedName("created_at")
Date createdAt;
+ @SerializedName("updated_at")
Date updatedAt;
- public static CodeEntity fromJsonObject(JsonObject o) {
- CodeEntity c = new CodeEntity();
-
- c.id = parseString(o, "id");
- c.projectId = parseString(o, "project_id");
- c.name = parseString(o, "name");
- c.runtime = parseString(o, "runtime");
- c.revision = parseInt(o, "rev");
- c.latestHistoryId = parseString(o, "latest_history_id");
- c.latestChecksum = parseString(o, "latest_checksum");
- c.latestChange = parseDate(o, "latest_change");
- c.createdAt = parseDate(o, "created_at");
- c.updatedAt = parseDate(o, "updated_at");
-
- return c;
- }
-
protected CodeEntity() {
}
@@ -1,51 +1,44 @@
package io.iron.ironworker.client.entities;
-import com.google.gson.JsonObject;
+import com.google.gson.annotations.SerializedName;
import java.util.Date;
-public class CodeRevisionEntity extends BaseEntity {
+public class CodeRevisionEntity {
+ @SerializedName("id")
String id;
- String codeId;
+ @SerializedName("project_id")
String projectId;
+ @SerializedName("code_id")
+ String codeId;
+ @SerializedName("name")
String name;
+ @SerializedName("runtime")
String runtime;
+ @SerializedName("file_name")
String runner;
+ @SerializedName("rev")
int revision;
+ @SerializedName("created_at")
Date createdAt;
+ @SerializedName("updated_at")
Date updatedAt;
- public static CodeRevisionEntity fromJsonObject(JsonObject o) {
- CodeRevisionEntity cr = new CodeRevisionEntity();
-
- cr.id = parseString(o, "id");
- cr.codeId = parseString(o, "code_id");
- cr.projectId = parseString(o, "project_id");
- cr.name = parseString(o, "name");
- cr.runtime = parseString(o, "runtime");
- cr.runner = parseString(o, "file_name");
- cr.revision = parseInt(o, "rev");
- cr.createdAt = parseDate(o, "created_at");
- cr.updatedAt = parseDate(o, "updated_at");
-
- return cr;
- }
-
protected CodeRevisionEntity() {
}
public String getId() {
return id;
}
- public String getCodeId() {
- return codeId;
- }
-
public String getProjectId() {
return projectId;
}
+ public String getCodeId() {
+ return codeId;
+ }
+
public String getName() {
return name;
}
@@ -1,48 +1,41 @@
package io.iron.ironworker.client.entities;
-import com.google.gson.JsonObject;
+import com.google.gson.annotations.SerializedName;
import java.util.Date;
-public class TaskEntity extends BaseEntity {
+public class TaskEntity extends {
+ @SerializedName("id")
String id;
+ @SerializedName("project_id")
String projectId;
+ @SerializedName("code_id")
String codeId;
+ @SerializedName("code_name")
String codeName;
+ @SerializedName("status")
String status;
+ @SerializedName("start_time")
Date startTime;
+ @SerializedName("end_time")
Date endTime;
+ @SerializedName("duration")
int duration;
+ @SerializedName("run_times")
int runTimes;
+ @SerializedName("timeout")
int timeout;
+ @SerializedName("payload")
String payload;
+ @SerializedName("percent")
int percent;
+ @SerializedName("msg")
String msg;
+ @SerializedName("created_at")
Date createdAt;
+ @SerializedName("updated_at")
Date updatedAt;
- public static TaskEntity fromJsonObject(JsonObject o) {
- TaskEntity t = new TaskEntity();
-
- t.id = parseString(o, "id");
- t.projectId = parseString(o, "project_id");
- t.codeId = parseString(o, "code_id");
- t.codeName = parseString(o, "code_name");
- t.status = parseString(o, "status");
- t.startTime = parseDate(o, "start_time");
- t.endTime = parseDate(o, "end_time");
- t.duration = parseInt(o, "duration");
- t.runTimes = parseInt(o, "run_times");
- t.timeout = parseInt(o, "timeout");
- t.payload = parseString(o, "payload");
- t.percent = parseInt(o, "percent");
- t.msg = parseString(o, "msg");
- t.createdAt = parseDate(o, "created_at");
- t.updatedAt = parseDate(o, "updated_at");
-
- return t;
- }
-
protected TaskEntity() {
}

1 comment on commit 7edb802

Maybe you can also create Codes that embed a list of codeEntity and just use Gson to wrap all.
Codes {
List codes;
}

Please sign in to comment.