Skip to content

Commit

Permalink
Added check for missing fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
guyrt committed Mar 30, 2013
1 parent 1fd17ae commit e2e8a8d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/main/java/org/openx/data/jsonserde/JsonSerDe.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public Object deserialize(Writable w) throws SerDeException {

// Try parsing row into JSON object
JSONObject jObj = null;


try {
jObj = new JSONObject(rowText.toString()) {
Expand All @@ -160,15 +161,14 @@ public Object deserialize(Writable w) throws SerDeException {
* java.lang.Object)
*/
@Override
public JSONObject put(String key, Object value)
throws JSONException {
public JSONObject put(String key, Object value) throws JSONException {

try {
if (rowTypeInfo.getStructFieldTypeInfo(key).getTypeName().equalsIgnoreCase("timestamp")) {
value = Timestamp.valueOf((String)value);
}
if (columnNames.contains(key) && rowTypeInfo.getStructFieldTypeInfo(key).getTypeName().equalsIgnoreCase("timestamp")) {
value = Timestamp.valueOf((String)value);
}
} catch (IllegalArgumentException e) {
throw new JSONException("Timestamp " + value + "improperly formatted.");
throw new JSONException("Timestamp " + value + "improperly formatted.");
}

return super.put(key.toLowerCase(), value);
Expand All @@ -195,7 +195,7 @@ public ObjectInspector getObjectInspector() throws SerDeException {

/**
* We serialize to Text
* @return
* @return
*
* @see org.apache.hadoop.io.Text
*/
Expand Down Expand Up @@ -228,7 +228,7 @@ public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDe

Text t = new Text(serializer.toString());

serializedDataSize = t.getBytes().length;
serializedDataSize = t.getBytes().length;
return t;
}

Expand Down Expand Up @@ -394,7 +394,7 @@ public void onMalformedJson(String msg) throws SerDeException {

@Override
public SerDeStats getSerDeStats() {
if(lastOperationSerialize) {
if(lastOperationSerialize) {
stats.setRawDataSize(serializedDataSize);
} else {
stats.setRawDataSize(deserializedDataSize);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/openx/data/jsonserde/JsonSerDeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ public void testDeserialize2() throws Exception {
assertEquals( ((JSONArray)result.get("three")).get(0),"red");
}

@Test
public void testDeserializePartialFieldSet() throws Exception {
Writable w = new Text("{\"missing\":\"whocares\",\"one\":true,\"three\":[\"red\",\"yellow\",[\"blue\",\"azure\",\"cobalt\",\"teal\"],\"orange\"],\"two\":19.5,\"four\":\"poop\"}");
Object expResult = null;
JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("four"),"poop");

assertTrue( result.get("three") instanceof JSONArray);

assertTrue( ((JSONArray)result.get("three")).get(0) instanceof String );
assertEquals( ((JSONArray)result.get("three")).get(0),"red");
}

/**
* Test of getSerializedClass method, of class JsonSerDe.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,14 @@ public void testTimestampDeSerialize() throws Exception {
assertEquals(result.get("five"), Timestamp.valueOf("2013-03-27 23:18:40.0"));
}

@Test
public void testTimestampDeSerializeWithNanoseconds() throws Exception {
// Test that timestamp object can be deserialized
Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27 23:18:40.123456\"}");

JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("five"), Timestamp.valueOf("2013-03-27 23:18:40.123456"));
}


}

0 comments on commit e2e8a8d

Please sign in to comment.