Skip to content

Commit

Permalink
struct and map use the same logic to determine if an object is a null…
Browse files Browse the repository at this point in the history
…, considering also an empty string / string of only spaces as a null
  • Loading branch information
Roberto Congiu committed Aug 25, 2014
1 parent a387336 commit 38001f1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public JsonMapObjectInspector(ObjectInspector mapKeyObjectInspector,

@Override
public Map<?, ?> getMap(Object data) {
if (checkObject(data) == null) {
if (JsonObjectInspectorUtils.checkObject(data) == null) {
return null;
}

Expand All @@ -42,22 +42,11 @@ public JsonMapObjectInspector(ObjectInspector mapKeyObjectInspector,
return new JSONObjectMapAdapter(jObj);
}

protected Object checkObject(Object data) {
// just check for null first thing
if(data == null) return data;

if(data instanceof String ||
data instanceof Text) {
String str = (data instanceof String ? (String)data : ((Text)data).toString() );
if(str.trim().isEmpty())
return null;
}
return data;
}


@Override
public int getMapSize(Object data) {
if (checkObject(data) == null) {
if (JsonObjectInspectorUtils.checkObject(data) == null) {
return -1;
}
JSONObject jObj = (JSONObject) data;
Expand All @@ -66,7 +55,7 @@ public int getMapSize(Object data) {

@Override
public Object getMapValueElement(Object data, Object key) {
if (checkObject(data) == null) {
if (JsonObjectInspectorUtils.checkObject(data) == null) {
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2014 rcongiu.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openx.data.jsonserde.objectinspector;

import org.apache.hadoop.io.Text;

/**
*
* @author rcongiu
*/
public class JsonObjectInspectorUtils {
public static Object checkObject(Object data) {
// just check for null first thing
if(data == null) return data;

if(data instanceof String ||
data instanceof Text) {
String str = (data instanceof String ? (String)data : ((Text)data).toString() );
if(str.trim().isEmpty())
return null;
}
return data;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public JsonStructObjectInspector(List<String> structFieldNames,
*/
@Override
public Object getStructFieldData(Object data, StructField fieldRef) {
if (data == null) {
if (JsonObjectInspectorUtils.checkObject(data) == null) {
return null;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public Object getStructFieldDataFromList(List data, StructField fieldRef ) {


public Object getStructFieldDataFromJsonObject(JSONObject data, StructField fieldRef ) {
if (data == null) {
if (JsonObjectInspectorUtils.checkObject(data) == null) {
return null;
}

Expand Down Expand Up @@ -127,7 +127,9 @@ protected String getJsonField(StructField fr) {
List<Object> values = new ArrayList<Object>();
@Override
public List<Object> getStructFieldsDataAsList(Object o) {
if(o == null) return null;
if (JsonObjectInspectorUtils.checkObject(o) == null) {
return null;
}
JSONObject jObj = (JSONObject) o;
values.clear();

Expand Down

0 comments on commit 38001f1

Please sign in to comment.