/*======================================================================*
 * Copyright (c) 2011, OpenX Technologies, Inc. All rights reserved.    *
 *                                                                      *
 * Licensed under the New BSD License (the "License"); you may not use  *
 * this file except in compliance with the License. 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. See accompanying LICENSE file.        *
 *======================================================================*/

package org.openx.data.jsonserde.objectinspector;

import java.util.Map;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StandardMapObjectInspector;
import org.openx.data.jsonserde.json.JSONException;
import org.openx.data.jsonserde.json.JSONObject;

/**
 *
 * @author rcongiu
 */
public class JsonMapObjectInspector extends StandardMapObjectInspector {
  
    public JsonMapObjectInspector(ObjectInspector mapKeyObjectInspector, 
            ObjectInspector mapValueObjectInspector) {
        super(mapKeyObjectInspector, mapValueObjectInspector);
    }


  @Override
  public Map<?, ?> getMap(Object data) {
    if (data == null) {
      return null;
    }
    
    JSONObject jObj = (JSONObject) data;
    
    return new JSONObjectMapAdapter(jObj);
  }

  @Override
  public int getMapSize(Object data) {
    if (data == null) {
      return -1;
    }
     JSONObject jObj = (JSONObject) data;
    return jObj.length();
  }

  @Override
  public Object getMapValueElement(Object data, Object key) {
    if (data == null) {
      return -1;
    }
    
     JSONObject jObj = (JSONObject) data;
        try {
            return jObj.get(key.toString());
        } catch (JSONException ex) {
            // key does not exists -> like null
            return null;
        }
  }   
}

@@ -882,16 +882,101 @@
 il.Map;%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -1029,32 +1029,32 @@
 bjectInspector;%0A
-
 import org.apach
@@ -1323,18 +1323,100 @@
 ector %7B%0A
+%0A
   
+  public static final Log LOG = LogFactory.getLog(JsonMapObjectInspector.class);%0A
 %0A    pub
@@ -1480,17 +1480,16 @@
 spector,
- 
 %0A       
@@ -1705,28 +1705,186 @@
 null;%0A    %7D%0A
+%0A
     
+if (!data.getClass().isAssignableFrom(JSONObject.class)) %7B%0A        LOG.warn(%22Expected a JSON Map, got this: %22 + data.toString());%0A        return null;%0A    %7D%0A
 %0A    JSONObj
@@ -1913,21 +1913,17 @@
 ) data;%0A
-    
 %0A
+
     retu
@@ -2258,13 +2258,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2480,13 +2480,10 @@
   %7D%0A
+
   %7D
-   
 %0A%7D%0A

Verify castability before actually doing it