Author: ieb
Date: Wed Jul 23 09:52:11 2008
New Revision: 679128

URL: http://svn.apache.org/viewvc?rev=679128&view=rev
Log:
Simplified the operation of the BeanJsonLibConverter extracting the inner 
classes and creating a JsonConfig object for the social api.

Added:
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConfig.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/InjectorBeanInstanceStrategy.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullDefaultValueProcessor.java
      - copied, changed from r679109, 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullPropertyFilter.java
Modified:
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonConverter.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConverter.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonLibConverterUtils.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonObjectToMapMorpher.java
    
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JsonLibTestsGuiceModule.java
    
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/BeanJsonLibConverterTest.java
    
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/JsonConverterPerformancePerf.java

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonConverter.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonConverter.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonConverter.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonConverter.java
 Wed Jul 23 09:52:11 2008
@@ -40,6 +40,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.Map.Entry;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -94,17 +95,17 @@
 
     } else if (val instanceof List) {
       JSONArray list = new JSONArray();
-      for (Object item : (List) val) {
+      for (Object item : (List<?>) val) {
         list.put(translateObjectToJson(item));
       }
       return list;
 
     } else if (val instanceof Map) {
       JSONObject map = new JSONObject();
-      Map originalMap = (Map) val;
+      Map<?, ?> originalMap = (Map<?,?>) val;
 
-      for (Object item : originalMap.keySet()) {
-        map.put(item.toString(), translateObjectToJson(originalMap.get(item)));
+      for (Entry<?, ?> item : originalMap.entrySet()) {
+        map.put(item.getKey().toString(), 
translateObjectToJson(item.getValue()));
       }
       return map;
 
@@ -218,7 +219,7 @@
       Class<?> mapValueClass = String.class;
 
       JSONObject jsonObject = new JSONObject(json);
-      Iterator iterator = jsonObject.keys();
+      Iterator<?> iterator = jsonObject.keys();
       while (iterator.hasNext()) {
         String key = (String) iterator.next();
         Object value = convertToObject(jsonObject.getString(key), 
mapValueClass);
@@ -254,7 +255,7 @@
       ParameterizedType genericListType
           = (ParameterizedType) method.getGenericParameterTypes()[0];
       Class<?> listElementClass
-          = (Class) genericListType.getActualTypeArguments()[0];
+          = (Class<?>) genericListType.getActualTypeArguments()[0];
 
       List<Object> list = Lists.newArrayList();
       JSONArray jsonArray = jsonObject.getJSONArray(fieldName);
@@ -268,7 +269,7 @@
       ParameterizedType genericListType
           = (ParameterizedType) method.getGenericParameterTypes()[0];
       Type[] types = genericListType.getActualTypeArguments();
-      Class<?> valueClass = (Class) types[1];
+      Class<?> valueClass = (Class<?>) types[1];
 
       // We only support keys being typed as Strings.
       // Nothing else really makes sense in json.
@@ -287,7 +288,7 @@
     } else if (org.apache.shindig.social.opensocial.model.Enum.class
         .isAssignableFrom(expectedType)) {
       // TODO Need to stop using Enum as a class name :(
-      Class enumType = (Class) ((ParameterizedType) 
method.getGenericParameterTypes()[0])
+      Class<?> enumType = (Class<?>) ((ParameterizedType) 
method.getGenericParameterTypes()[0])
           .getActualTypeArguments()[0];
       // TODO This isnt injector friendly but perhaps implementors dont need 
it. If they do a
       // refactoring of the Enum handling in general is needed.

Added: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConfig.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConfig.java?rev=679128&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConfig.java
 (added)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConfig.java
 Wed Jul 23 09:52:11 2008
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shindig.social.core.util;
+
+import org.apache.shindig.social.opensocial.model.Address;
+import org.apache.shindig.social.opensocial.model.Email;
+import org.apache.shindig.social.opensocial.model.Enum;
+import org.apache.shindig.social.opensocial.model.MediaItem;
+import org.apache.shindig.social.opensocial.model.Organization;
+import org.apache.shindig.social.opensocial.model.Phone;
+import org.apache.shindig.social.opensocial.model.Url;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import net.sf.ezmorph.MorpherRegistry;
+import net.sf.json.JsonConfig;
+import net.sf.json.util.EnumMorpher;
+import net.sf.json.util.JSONUtils;
+
+/**
+ * A Json Config class suitable for serializing Shindig json and pojos.
+ */
+
+public class BeanJsonLibConfig extends JsonConfig {
+
+  /*
+   * Register the Enum Morphers so that JSON -> Bean works correctly for enums.
+   */
+  static {
+    MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
+    morpherRegistry.registerMorpher(new EnumMorpher(Address.Field.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(Phone.Field.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(Email.Field.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(MediaItem.Field.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(MediaItem.Type.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Drinker.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Field.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Gender.class));
+    morpherRegistry.registerMorpher(new 
EnumMorpher(Enum.NetworkPresence.class));
+    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Smoker.class));
+    morpherRegistry.registerMorpher(new JsonObjectToMapMorpher());
+  }
+
+  /**
+   * Construct the config with a Guice injector.
+   * @param injector the Guice injector
+   */
+  @Inject
+  public BeanJsonLibConfig(Injector injector) {
+    /*
+     * This hook deals with the creation of new beans in the JSON -> Java Bean
+     * conversion
+     */
+    setNewBeanInstanceStrategy(new InjectorBeanInstanceStrategy(injector));
+
+    /*
+     * We are expecting null for nulls
+     */
+    registerDefaultValueProcessor(String.class, new 
NullDefaultValueProcessor());
+
+    setJsonPropertyFilter(new NullPropertyFilter());
+    setJavaPropertyFilter(new NullPropertyFilter());
+    // the classMap deals with the basic json string to bean conversion
+
+    Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
+
+    /*
+     * mappings are required where there is a List of objects in the interface
+     * with no indication of what type the list should contain. At the moment,
+     * we are using 1 map for all json trees, as there is no conflict, but if
+     * there is a map could be selected on the basis of the root object. It
+     * would be better to do this with generics, but this is good enough and
+     * compact enough for the moment.
+     *
+     */
+    //
+    // activity
+    classMap.put("mediaItems", MediaItem.class);
+    // this may not be necessary
+    classMap.put("templateParams", Map.class);
+    // BodyType needs no mappings
+    // Message needs no mappings
+    // Name needs no mappings
+    // Organization needs no mappings
+    // Url needs no mappings
+    // Email needs no mappings
+    // Phone Needs no mappings
+    // Address Needs no mappings
+    // MediaItem needs no mappings
+
+    // Person map
+    classMap.put("addresses", Address.class);
+    classMap.put("phoneNumbers", Phone.class);
+    classMap.put("emails", Email.class);
+    classMap.put("mediaItems", MediaItem.class);
+    classMap.put("jobs", Organization.class);
+    classMap.put("schools", Organization.class);
+    classMap.put("urls", Url.class);
+    setClassMap(classMap);
+
+  }
+
+}

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java
 Wed Jul 23 09:52:11 2008
@@ -23,21 +23,40 @@
  */
 public class BeanJsonLibConversionException extends RuntimeException {
 
+  /**
+   * serial ID.
+   */
   private static final long serialVersionUID = -8609384443448202372L;
 
+  /**
+   * create the exception.
+   */
   public BeanJsonLibConversionException() {
   }
 
-  public BeanJsonLibConversionException(String arg0) {
-    super(arg0);
+  /**
+   * Constructor.
+   * @param message a message
+   */
+  public BeanJsonLibConversionException(String message) {
+    super(message);
   }
 
-  public BeanJsonLibConversionException(Throwable arg0) {
-    super(arg0);
+  /**
+   * create with a cause.
+   * @param cause the cause
+   */
+  public BeanJsonLibConversionException(Throwable cause) {
+    super(cause);
   }
 
-  public BeanJsonLibConversionException(String arg0, Throwable arg1) {
-    super(arg0, arg1);
+  /**
+   * create with a cause and a message.
+   * @param message a message
+   * @param cause the cause
+   */
+  public BeanJsonLibConversionException(String message, Throwable cause) {
+    super(message, cause);
   }
 
 }

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConverter.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConverter.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConverter.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConverter.java
 Wed Jul 23 09:52:11 2008
@@ -17,49 +17,35 @@
  */
 package org.apache.shindig.social.core.util;
 
-import java.lang.reflect.Array;
-import java.lang.reflect.InvocationTargetException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import net.sf.ezmorph.MorpherRegistry;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONException;
 import net.sf.json.JSONObject;
 import net.sf.json.JsonConfig;
-import net.sf.json.processors.DefaultValueProcessor;
-import net.sf.json.util.EnumMorpher;
 import net.sf.json.util.JSONUtils;
-import net.sf.json.util.NewBeanInstanceStrategy;
-import net.sf.json.util.PropertyFilter;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.name.Named;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import org.apache.shindig.social.opensocial.model.Address;
-import org.apache.shindig.social.opensocial.model.Email;
-import org.apache.shindig.social.opensocial.model.Enum;
-import org.apache.shindig.social.opensocial.model.MediaItem;
-import org.apache.shindig.social.opensocial.model.Organization;
-import org.apache.shindig.social.opensocial.model.Phone;
-import org.apache.shindig.social.opensocial.model.Url;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
+import java.lang.reflect.Array;
+import java.util.List;
 
 /**
  * BeanConverter implementation us the net.sf.json-lib json library.
  */
 public class BeanJsonLibConverter implements BeanConverter {
 
+
+
   /**
-   * The Logger
+   * The Logger.
    */
   protected static final Log LOG = 
LogFactory.getLog(BeanJsonLibConverter.class);
   /**
-   * The Guice injector used to create beans
+   * The Guice injector used to create beans.
    */
   private Injector injector;
   /**
@@ -67,35 +53,30 @@
    */
   private JsonConfig jsonConfig;
   /**
-   * in IDE debug flag
+   * in IDE debug flag.
    */
   private boolean debugMode = false;
 
-  /*
-   * Register the Enum Morphers so that JSON -> Bean works correctly for enums.
-   */
-  static {
-    MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
-    morpherRegistry.registerMorpher(new EnumMorpher(Address.Field.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(Phone.Field.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(Email.Field.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(MediaItem.Field.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(MediaItem.Type.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Drinker.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Field.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Gender.class));
-    morpherRegistry.registerMorpher(new 
EnumMorpher(Enum.NetworkPresence.class));
-    morpherRegistry.registerMorpher(new EnumMorpher(Enum.Smoker.class));
-
-    morpherRegistry.registerMorpher(new JsonObjectToMapMorpher());
-  }
 
+  /**
+   * Create an BeanConverter with an injector.
+   * @param injector the Guice injector to use for conversion
+   * @param jsonConfig the Json Configuration
+   */
   @Inject
-  public BeanJsonLibConverter(Injector injector) {
+  public BeanJsonLibConverter(Injector injector,
+      @Named("ShindigJsonConfig") JsonConfig jsonConfig) {
     this.injector = injector;
-    createJsonConfig();
+    this.jsonConfig = jsonConfig;
   }
 
+  /**
+   * Convert the json string into a pojo based on the supplied root class.
+   * @param string the json string
+   * @param rootBeanClass the root class of the bean
+   * @param <T> The typep of the pojo to be returned
+   * @return A pojo of the same type as the rootBeanClass
+   */
   @SuppressWarnings("unchecked")
   public <T> T convertToObject(String string, final Class<T> rootBeanClass) {
 
@@ -137,139 +118,6 @@
     }
   }
 
-  /**
-   * @return
-   */
-  private void createJsonConfig() {
-
-    jsonConfig = new JsonConfig();
-
-    /*
-     * This hook deals with the creation of new beans in the JSON -> Java Bean
-     * conversion
-     */
-    jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {
-
-      @SuppressWarnings("unchecked")
-      @Override
-      public Object newInstance(Class beanClass, JSONObject jsonObject)
-          throws InstantiationException, IllegalAccessException, 
NoSuchMethodException,
-          InvocationTargetException {
-        if (beanClass != null) {
-          Object o = BeanJsonLibConverter.this.injector.getInstance(beanClass);
-          if (debugMode) {
-            LOG.info("Created Object " + o + " for " + beanClass + " with [" + 
jsonObject + "]");
-          }
-          return o;
-        }
-        return DEFAULT.newInstance(beanClass, jsonObject);
-      }
-
-    });
-
-    /*
-     * We are expecting null for nulls
-     */
-    jsonConfig.registerDefaultValueProcessor(String.class, new 
DefaultValueProcessor() {
-      @SuppressWarnings("unchecked")
-      public Object getDefaultValue(Class target) {
-        return null;
-      }
-    });
-
-    jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
-
-      public boolean apply(Object source, String name, Object value) {
-        return filterProperty(source, name, value);
-      }
-
-    });
-
-    jsonConfig.setJavaPropertyFilter(new PropertyFilter() {
-
-      public boolean apply(Object source, String name, Object value) {
-        return filterProperty(source, name, value);
-      }
-
-    });
-
-    // the classMap deals with the basic json string to bean conversion
-
-    Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
-
-    /*
-     * mappings are required where there is a List of objects in the interface
-     * with no indication of what type the list should contain. At the moment,
-     * we are using 1 map for all json trees, as there is no conflict, but if
-     * there is a map could be selected on the basis of the root object. It
-     * would be better to do this with generics, but this is good enough and
-     * compact enough for the moment.
-     *
-     */
-    //
-    // activity
-    classMap.put("mediaItems", MediaItem.class);
-    // this may not be necessary
-    classMap.put("templateParams", Map.class);
-    // BodyType needs no mappings
-    // Message needs no mappings
-    // Name needs no mappings
-    // Organization needs no mappings
-    // Url needs no mappings
-    // Email needs no mappings
-    // Phone Needs no mappings
-    // Address Needs no mappings
-    // MediaItem needs no mappings
-
-    // Person map
-    classMap.put("addresses", Address.class);
-    classMap.put("phoneNumbers", Phone.class);
-    classMap.put("emails", Email.class);
-    classMap.put("mediaItems", MediaItem.class);
-    classMap.put("jobs", Organization.class);
-    classMap.put("schools", Organization.class);
-    classMap.put("urls", Url.class);
-    jsonConfig.setClassMap(classMap);
-
-  }
-
-  /**
-   * Filter the output of a property, if it should be emitted return false.
-   * @param source The object containing the value
-   * @param name the name of the key in the output structure
-   * @param value the value of the object
-   * @return true if the property should be filtered, false if not.
-   */
-  protected boolean filterProperty(Object source, String name, Object value) {
-    if (value == null) {
-      return true;
-    }
-    if (value instanceof JSONArray) {
-      JSONArray array = (JSONArray) value;
-      if (array.size() == 0) {
-        return true;
-      }
-    }
-    if (value instanceof JSONObject) {
-      JSONObject object = (JSONObject) value;
-      if (object.isNullObject() || object.isEmpty()) {
-        return true;
-      }
-    }
-    if (value instanceof Collection) {
-      Collection<?> collection = (Collection<?>) value;
-      if (collection.size() == 0) {
-        return true;
-      }
-    }
-    if (value instanceof Object[]) {
-      Object[] oarray = (Object[]) value;
-      if (oarray.length == 0) {
-        return true;
-      }
-    }
-    return false;
-  }
 
   /**
    * Convert the pojo to a json string representation.

Added: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/InjectorBeanInstanceStrategy.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/InjectorBeanInstanceStrategy.java?rev=679128&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/InjectorBeanInstanceStrategy.java
 (added)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/InjectorBeanInstanceStrategy.java
 Wed Jul 23 09:52:11 2008
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shindig.social.core.util;
+
+import com.google.inject.Injector;
+
+import java.lang.reflect.InvocationTargetException;
+
+import net.sf.json.JSONObject;
+import net.sf.json.util.NewBeanInstanceStrategy;
+
+/**
+ * An Injector based NewBeanInstance strategy that will use Guice to create new
+ * beans.
+ */
+public class InjectorBeanInstanceStrategy extends NewBeanInstanceStrategy {
+  /**
+   * The injector to use to create beans.
+   */
+  private Injector injector;
+
+  /**
+   * Constructor.
+   *
+   * @param injector
+   *                The injector to use.
+   */
+  public InjectorBeanInstanceStrategy(Injector injector) {
+    this.injector = injector;
+  }
+
+  /**
+   * create a new instance of the requested bean using Guice.
+   *
+   * @param beanClass
+   *                The bean class to create and instance of
+   * @param jsonObject
+   *                the json object to base that instance on
+   * @return a new instance of the request class
+   * @throws InstantiationException when the object cant be created
+   * @throws IllegalAccessException when permission is denied to the class
+   * @throws NoSuchMethodException when the constructor or one of the 
injectors has gone missing
+   * @throws InvocationTargetException when the invocation method fails to 
invoke
+   */
+  @SuppressWarnings("unchecked")
+  @Override
+  public Object newInstance(Class beanClass, JSONObject jsonObject) throws 
InstantiationException,
+      IllegalAccessException, NoSuchMethodException, InvocationTargetException 
{
+    if (beanClass != null) {
+      return injector.getInstance(beanClass);
+    }
+    return DEFAULT.newInstance(null, jsonObject);
+  }
+
+}

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonLibConverterUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonLibConverterUtils.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonLibConverterUtils.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonLibConverterUtils.java
 Wed Jul 23 09:52:11 2008
@@ -26,18 +26,16 @@
 /**
  * Some utility functions to simpilfy handling SF json-lib objects.
  */
-public class JsonLibConverterUtils {
+public final class JsonLibConverterUtils {
+
   /**
-   * This class is a utility class and can't be constructed.
+   * The Logger.
    */
-  private JsonLibConverterUtils() {
-  }
-
   protected static final Log LOG = 
LogFactory.getLog(JsonLibConverterUtils.class);
 
   /**
    * Dumps a JSON Object out to the log at info level.
-   * 
+   *
    * @param jsonObject
    *                The object to dump
    * @param indent
@@ -63,8 +61,9 @@
   }
 
   /**
-   * @param value
-   * @param string
+   * Dump a json object to the log.
+   * @param array a json array to dump
+   * @param indent the indent for each level of nesting
    */
   public static void dumpJsonArray(JSONArray array, String indent) {
     for (Object value : array) {

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonObjectToMapMorpher.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonObjectToMapMorpher.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonObjectToMapMorpher.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/JsonObjectToMapMorpher.java
 Wed Jul 23 09:52:11 2008
@@ -25,19 +25,30 @@
 import net.sf.json.JSONObject;
 
 /**
- *
+ * A morpher that converts objects into maps
  */
 public class JsonObjectToMapMorpher implements Morpher, ObjectMorpher {
 
+  /**
+   * @return the class that the morper will morph to
+   */
   public Class<?> morphsTo() {
     return Map.class;
   }
 
+  /**
+   * @param clazz the class being checked
+   * @return true if this morpher supports the class
+   */
   @SuppressWarnings("unchecked")
   public boolean supports(Class clazz) {
     return (JSONObject.class.equals(clazz));
   }
 
+  /**
+   * @param the bean to be morphed
+   * @return the morphed bean (a map)
+   */
   public Object morph(Object bean) {
     Map<Object, Object> result = new HashMap<Object, Object>();
     JSONObject jsonObject = (JSONObject) bean;

Copied: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullDefaultValueProcessor.java
 (from r679109, 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java)
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullDefaultValueProcessor.java?p2=incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullDefaultValueProcessor.java&p1=incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java&r1=679109&r2=679128&rev=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonLibConversionException.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullDefaultValueProcessor.java
 Wed Jul 23 09:52:11 2008
@@ -17,27 +17,17 @@
  */
 package org.apache.shindig.social.core.util;
 
+import net.sf.json.processors.DefaultValueProcessor;
+
 /**
- * Where a conversion exception happens in the Json Lib conversion, this
- * exception is thrown.
+ * Null are serialized as Nulls.
  */
-public class BeanJsonLibConversionException extends RuntimeException {
-
-  private static final long serialVersionUID = -8609384443448202372L;
-
-  public BeanJsonLibConversionException() {
-  }
-
-  public BeanJsonLibConversionException(String arg0) {
-    super(arg0);
+public class NullDefaultValueProcessor implements DefaultValueProcessor {
+  /**
+   * returns the object to be serialized
+   */
+  @SuppressWarnings("unchecked")
+  public Object getDefaultValue(Class target) {
+    return null;
   }
-
-  public BeanJsonLibConversionException(Throwable arg0) {
-    super(arg0);
-  }
-
-  public BeanJsonLibConversionException(String arg0, Throwable arg1) {
-    super(arg0, arg1);
-  }
-
 }

Added: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullPropertyFilter.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullPropertyFilter.java?rev=679128&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullPropertyFilter.java
 (added)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/util/NullPropertyFilter.java
 Wed Jul 23 09:52:11 2008
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shindig.social.core.util;
+
+import java.util.Collection;
+
+import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
+import net.sf.json.util.PropertyFilter;
+
+/**
+ * A property filter that rejects null values.
+ */
+public class NullPropertyFilter implements PropertyFilter {
+
+  /**
+   * Filter the output of a property, if it should be emitted return false.
+   * @param source The object containing the value
+   * @param name the name of the key in the output structure
+   * @param value the value of the object
+   * @return true if the property should be filtered, false if not.
+   */
+  public boolean apply(Object source, String name, Object value) {
+    if (value == null) {
+      return true;
+    }
+    if (value instanceof JSONArray) {
+      JSONArray array = (JSONArray) value;
+      if (array.size() == 0) {
+        return true;
+      }
+    }
+    if (value instanceof JSONObject) {
+      JSONObject object = (JSONObject) value;
+      if (object.isNullObject() || object.isEmpty()) {
+        return true;
+      }
+    }
+    if (value instanceof Collection) {
+      Collection<?> collection = (Collection<?>) value;
+      if (collection.size() == 0) {
+        return true;
+      }
+    }
+    if (value instanceof Object[]) {
+      Object[] oarray = (Object[]) value;
+      if (oarray.length == 0) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+}

Modified: 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JsonLibTestsGuiceModule.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JsonLibTestsGuiceModule.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JsonLibTestsGuiceModule.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JsonLibTestsGuiceModule.java
 Wed Jul 23 09:52:11 2008
@@ -18,12 +18,18 @@
  */
 package org.apache.shindig.social;
 
+import org.apache.shindig.social.core.util.BeanJsonLibConfig;
+
+import net.sf.json.JsonConfig;
+
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import com.google.inject.AbstractModule;
+import com.google.inject.name.Named;
+import com.google.inject.name.Names;
 
 /**
  * Provides social api component injection for all large tests
@@ -35,6 +41,8 @@
     bind(Map.class).to(HashMap.class);
     bind(List.class).to(ArrayList.class);
     bind(Map[].class).to(HashMap[].class);
+    bind(JsonConfig.class).annotatedWith(Names.named("ShindigJsonConfig")).to(
+        BeanJsonLibConfig.class);
   }
 
 }

Modified: 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/BeanJsonLibConverterTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/BeanJsonLibConverterTest.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/BeanJsonLibConverterTest.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/BeanJsonLibConverterTest.java
 Wed Jul 23 09:52:11 2008
@@ -45,6 +45,7 @@
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.inject.Guice;
+import com.google.inject.Injector;
 
 public class BeanJsonLibConverterTest extends TestCase {
 
@@ -95,8 +96,8 @@
     activity.setMediaItems(Lists.<MediaItem> newArrayList(new MediaItemImpl(
         "image/jpg", MediaItem.Type.IMAGE, "http://foo.bar";)));
 
-    beanJsonConverter = new BeanJsonLibConverter(Guice
-        .createInjector(new JsonLibTestsGuiceModule()));
+    Injector injector = Guice.createInjector(new JsonLibTestsGuiceModule());
+    beanJsonConverter = injector.getInstance(BeanJsonLibConverter.class);
 
     apiValidator = new ApiValidator();
 

Modified: 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/JsonConverterPerformancePerf.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/JsonConverterPerformancePerf.java?rev=679128&r1=679127&r2=679128&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/JsonConverterPerformancePerf.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/util/JsonConverterPerformancePerf.java
 Wed Jul 23 09:52:11 2008
@@ -43,6 +43,7 @@
 
 import com.google.common.collect.Lists;
 import com.google.inject.Guice;
+import com.google.inject.Injector;
 
 public class JsonConverterPerformancePerf extends TestCase {
 
@@ -75,8 +76,8 @@
     activity.setMediaItems(Lists.<MediaItem> newArrayList(new MediaItemImpl(
         "image/jpg", MediaItem.Type.IMAGE, "http://foo.bar";)));
 
-    beanJsonLibConverter = new BeanJsonLibConverter(Guice
-        .createInjector(new JsonLibTestsGuiceModule()));
+    Injector injector = Guice.createInjector(new JsonLibTestsGuiceModule());
+    beanJsonLibConverter = injector.getInstance(BeanJsonLibConverter.class);
 
     beanJsonConverter = new BeanJsonConverter(Guice
         .createInjector(new SocialApiTestsGuiceModule()));


Reply via email to