Skip to content

Commit

Permalink
Merge pull request #226 from rcongiu/revert-210-develop
Browse files Browse the repository at this point in the history
Revert "#94 Fail when using sequence file"
  • Loading branch information
rcongiu committed Aug 11, 2020
2 parents 95c49af + de2a95c commit 8d5c1bf
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions json-serde/src/main/java/org/openx/data/jsonserde/JsonSerDe.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package org.openx.data.jsonserde;

import java.nio.charset.CharacterCodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -28,7 +27,6 @@
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.Writable;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -160,44 +158,41 @@ public void initialize(Configuration conf, Properties tbl) throws SerDeException
.getProperty(PROP_EXPLICIT_NULL, "false"));
}

JSONObject emptyJson = new JSONObject();
/**
* Deserializes the object. Reads a Writable and uses JSONObject to
* parse its text
*
*
* @param w the text to parse
* @return a JSONObject
* @throws SerDeException
* @throws SerDeException
*/
@Override
public Object deserialize(Writable w) throws SerDeException {
Text rowText = (Text) w;
deserializedDataSize = rowText.getBytes().length;

// Try parsing row into JSON object
Object jObj = null;
String txt = null;

try {
if (w instanceof Text) {
Text rowText = (Text) w;
deserializedDataSize = rowText.getLength();
txt = rowText.toString().trim();
} else if (w instanceof BytesWritable) {
byte[] rowByteArr = ((BytesWritable) w).getBytes();
deserializedDataSize = rowByteArr.length;
txt = Text.decode(rowByteArr,0,rowByteArr.length).trim();
}
if (txt.startsWith("{")) {
String txt = rowText.toString().trim();

if(txt.startsWith("{")) {
jObj = new JSONObject(txt);
} else if (txt.startsWith("[")) {
} else if (txt.startsWith("[")){
jObj = new JSONArray(txt);
}
} catch (JSONException e) {
// If row is not a JSON object, make the whole row NULL
onMalformedJson("Row is not a valid JSON Object - JSONException: " + e.getMessage());
return emptyJson;
} catch (CharacterCodingException e) {
onMalformedJson("unable to decode bytes to text : " + e.getMessage());
return emptyJson;
onMalformedJson("Row is not a valid JSON Object - JSONException: "
+ e.getMessage());
try {
jObj = new JSONObject("{}");
} catch (JSONException ex) {
onMalformedJson("Error parsing empty row. This should never happen.");
}
}

return jObj;
}

Expand Down

0 comments on commit 8d5c1bf

Please sign in to comment.