| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 40 | |
|
| 41 | |
public class BeanJsonLibConverter implements BeanConverter { |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 1 | protected static final Log LOG = LogFactory.getLog(BeanJsonLibConverter.class); |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private Injector injector; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
private JsonConfig jsonConfig; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 10 | private boolean debugMode = false; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 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 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 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 | |
|
| 130 | |
|
| 131 | |
|
| 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 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
@SuppressWarnings("unchecked") |
| 158 | |
public void addMapping(String key, Class<?> class1) { |
| 159 | 2 | jsonConfig.getClassMap().put(key, class1); |
| 160 | 2 | } |
| 161 | |
|
| 162 | |
} |