Coverage Report - org.apache.shindig.social.core.util.BeanJsonLibConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanJsonLibConverter
84%
36/43
75%
12/16
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util;
 19  
 
 20  
 import org.apache.shindig.social.opensocial.service.BeanConverter;
 21  
 
 22  
 import net.sf.json.JSONArray;
 23  
 import net.sf.json.JSONException;
 24  
 import net.sf.json.JSONObject;
 25  
 import net.sf.json.JsonConfig;
 26  
 import net.sf.json.util.JSONUtils;
 27  
 
 28  
 import com.google.inject.Inject;
 29  
 import com.google.inject.Injector;
 30  
 import com.google.inject.name.Named;
 31  
 
 32  
 import org.apache.commons.logging.Log;
 33  
 import org.apache.commons.logging.LogFactory;
 34  
 
 35  
 import java.lang.reflect.Array;
 36  
 import java.util.List;
 37  
 
 38  
 /**
 39  
  * BeanConverter implementation us the net.sf.json-lib json library.
 40  
  */
 41  
 public class BeanJsonLibConverter implements BeanConverter {
 42  
 
 43  
 
 44  
 
 45  
   /**
 46  
    * The Logger.
 47  
    */
 48  1
   protected static final Log LOG = LogFactory.getLog(BeanJsonLibConverter.class);
 49  
   /**
 50  
    * The Guice injector used to create beans.
 51  
    */
 52  
   private Injector injector;
 53  
   /**
 54  
    * Json Config object used by each instance.
 55  
    */
 56  
   private JsonConfig jsonConfig;
 57  
   /**
 58  
    * in IDE debug flag.
 59  
    */
 60  10
   private boolean debugMode = false;
 61  
 
 62  
 
 63  
   /**
 64  
    * Create an BeanConverter with an injector.
 65  
    * @param injector the Guice injector to use for conversion
 66  
    * @param jsonConfig the Json Configuration
 67  
    */
 68  
   @Inject
 69  
   public BeanJsonLibConverter(Injector injector,
 70  10
       @Named("ShindigJsonConfig") JsonConfig jsonConfig) {
 71  10
     this.injector = injector;
 72  10
     this.jsonConfig = jsonConfig;
 73  10
   }
 74  
 
 75  
   public String getContentType() {
 76  0
     return "application/json";
 77  
   }
 78  
 
 79  
   /**
 80  
    * Convert the json string into a pojo based on the supplied root class.
 81  
    * @param string the json string
 82  
    * @param rootBeanClass the root class of the bean
 83  
    * @param <T> The typep of the pojo to be returned
 84  
    * @return A pojo of the same type as the rootBeanClass
 85  
    */
 86  
   @SuppressWarnings("unchecked")
 87  
   public <T> T convertToObject(String string, final Class<T> rootBeanClass) {
 88  
 
 89  11
     if ("".equals(string)) {
 90  1
       string = "{}";
 91  
     }
 92  11
     if (string.startsWith("[")) {
 93  4
       JSONArray jsonArray = JSONArray.fromObject(string, jsonConfig);
 94  4
       if (debugMode) {
 95  0
         JsonLibConverterUtils.dumpJsonArray(jsonArray, " ");
 96  
       }
 97  
 
 98  4
       if (rootBeanClass.isArray()) {
 99  4
         Class<?> componentType = rootBeanClass.getComponentType();
 100  4
         Object rootObject = injector.getInstance(componentType);
 101  4
         List<?> o = JSONArray.toList(jsonArray, rootObject, jsonConfig);
 102  4
         Object[] result = (Object[]) Array.newInstance(componentType, o.size());
 103  11
         for (int i = 0; i < o.size(); i++) {
 104  7
           result[i] = o.get(i);
 105  
         }
 106  4
         return (T) result;
 107  
 
 108  
       } else {
 109  0
         T rootObject = injector.getInstance(rootBeanClass);
 110  0
         Object o = JSONArray.toArray(jsonArray, rootObject, jsonConfig);
 111  0
         return (T) o;
 112  
       }
 113  
     } else {
 114  7
       JSONObject jsonObject = JSONObject.fromObject(string, jsonConfig);
 115  
 
 116  7
       if (debugMode) {
 117  0
         JsonLibConverterUtils.dumpJsonObject(jsonObject, " ");
 118  
       }
 119  
 
 120  7
       T rootObject = injector.getInstance(rootBeanClass);
 121  7
       Object o = JSONObject.toBean(jsonObject, rootObject, jsonConfig);
 122  7
       return (T) o;
 123  
 
 124  
     }
 125  
   }
 126  
 
 127  
 
 128  
   /**
 129  
    * Convert the pojo to a json string representation.
 130  
    * @param pojo the pojo to convert
 131  
    * @return the json string representation of the pojo.
 132  
    */
 133  
   public String convertToString(Object pojo) {
 134  7
     if ("".equals(pojo)) {
 135  1
       return "{}";
 136  
     }
 137  
 
 138  
     try {
 139  6
       JSONObject jsonObject = JSONObject.fromObject(pojo, jsonConfig);
 140  4
       return jsonObject.toString();
 141  2
     } catch (JSONException jse) {
 142  2
       Class<?> pojoClass = pojo.getClass();
 143  2
       if (JSONUtils.isArray(pojoClass)) {
 144  2
         JSONArray jsonArray = JSONArray.fromObject(pojo);
 145  2
         String result = jsonArray.toString();
 146  2
         return result;
 147  
       }
 148  0
       throw jse;
 149  
     }
 150  
   }
 151  
 
 152  
   /**
 153  
    * Add a mapping to the json -> pojo conversion map.
 154  
    * @param key the name of the json key to bind to
 155  
    * @param class1 the class that should be used to represent that key
 156  
    */
 157  
   @SuppressWarnings("unchecked")
 158  
   public void addMapping(String key, Class<?> class1) {
 159  2
     jsonConfig.getClassMap().put(key, class1);
 160  2
   }
 161  
 
 162  
 }