Permalink
Browse files

Adding json list deserialization support

  • Loading branch information...
1 parent 81029b7 commit 9650f4e04d3d8980bf9609ce93cf9b5401a84e17 @billonahill billonahill committed May 25, 2012
Showing with 36 additions and 0 deletions.
  1. +36 −0 pig/src/main/java/com/twitter/ambrose/service/DAGNode.java
@@ -15,11 +15,18 @@
*/
package com.twitter.ambrose.service;
+import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnore;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.codehaus.jackson.map.DeserializationConfig;
+import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.codehaus.jackson.type.TypeReference;
+import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
+import java.util.List;
/**
* Class that represents a Job node in the DAG. The job name must not be null. At DAG creation time
@@ -49,6 +56,19 @@ public DAGNode(String name, String[] aliases, String[] features) {
this.features = features;
}
+ @JsonCreator
+ public DAGNode(@JsonProperty("name") String name,
+ @JsonProperty("aliases") String[] aliases,
+ @JsonProperty("features") String[] features,
+ @JsonProperty("jobId") String jobId,
+ @JsonProperty("successorNames") Collection<String> successorNames) {
+ this.name = name;
+ this.aliases = aliases;
+ this.features = features;
+ this.jobId = jobId;
+ this.successorNames = successorNames;
+ }
+
public String getName() { return name; }
public String[] getAliases() { return aliases == null ? new String[0] : aliases; }
public String[] getFeatures() { return features == null ? new String[0] : features; }
@@ -70,4 +90,20 @@ public synchronized void setSuccessors(Collection<DAGNode> successors) {
}
public synchronized Collection<String> getSuccessorNames() { return successorNames; }
+
+ /**
+ * Derializes a JSON List of DAG object into a List&lt;DAGNode>. Unrecognized properties will
+ * be ignored.
+ *
+ * @param dagListJson the string to convert into a JSON object.
+ * @return a List of DAGNode objects.
+ * @throws java.io.IOException
+ */
+ public static List<DAGNode> fromJSONList(String dagListJson) throws IOException {
+ ObjectMapper om = new ObjectMapper();
+ om.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+
+ // not currently setting successors, only successorNames
+ return om.readValue(dagListJson, new TypeReference<List<DAGNode>>() { });
+ }
}

0 comments on commit 9650f4e

Please sign in to comment.