001 package net.sourceforge.javajson.converter; 002 003 import java.lang.reflect.InvocationTargetException; 004 import java.util.Collection; 005 import java.util.Date; 006 import java.util.Map; 007 008 import org.apache.log4j.Logger; 009 010 import net.sourceforge.javajson.JsonArray; 011 import net.sourceforge.javajson.JsonObject; 012 013 /** 014 * Maps objects to a json object TODO: addMapper as: String field, String alias 015 * to map to an existing mapper TODO: addMapper to map a field to 016 * objAtField.fieldName and maybe rename the field too 017 */ 018 public class Mapper { 019 protected final Logger log = Logger.getLogger(getClass()); 020 021 /** 022 * Default mapper that can be used to map object all the way down the tree. 023 */ 024 public static Mapper DefaultMapper = new Mapper(); 025 026 private Mapper() { 027 } 028 029 /** 030 * Maps an object to json based on a class, this method only works when 031 * using the default mapper and is meant to be called via convenience method 032 * on Converter. 033 * 034 * @param obj 035 * The object to convert to json 036 * @param cls 037 * @param locale 038 * @return 039 * @throws InvocationTargetException 040 * @throws IllegalAccessException 041 * @throws IllegalArgumentException 042 * @throws InvocationTargetException 043 * @throws IllegalAccessException 044 * @throws IllegalArgumentException 045 */ 046 protected JsonObject toJson(Object obj, Class cls) 047 throws IllegalArgumentException, IllegalAccessException, 048 InvocationTargetException { 049 return toJson(obj, cls, false); 050 } 051 052 protected JsonObject toJson(Object obj, Class cls, boolean flat) 053 throws IllegalArgumentException, IllegalAccessException, 054 InvocationTargetException { 055 JsonObject ret = null; 056 if (obj != null) { 057 if (cls == null) 058 ret = Utils.toJson(obj, obj.getClass()); 059 else 060 ret = Utils.toJson(obj, cls); 061 062 if (!flat) { 063 Map<String, Object> fields = Reflection.getFields(obj); 064 for (String key : fields.keySet()) { 065 Object val = fields.get(key); 066 if (!(val instanceof Number || val instanceof Date 067 || val instanceof String || val instanceof Boolean || val instanceof Class) 068 && val != null) { 069 // non basic types... 070 String jsonKey = Reflection.getFieldName(key); 071 if (val instanceof Collection) { 072 JsonArray array = toJsonArray((Collection) val); 073 ret.put(jsonKey, array); 074 } else { 075 ret.put(jsonKey, toJson(val)); 076 } 077 } 078 } 079 } 080 } 081 return ret; 082 } 083 084 public JsonObject toJson(Object obj, boolean flat) 085 throws IllegalArgumentException, IllegalAccessException, 086 InvocationTargetException { 087 if (obj != null) 088 return toJson(obj, obj.getClass(), flat); 089 else 090 return toJson(obj, null, flat); 091 } 092 093 public JsonObject toJson(Object obj) throws IllegalArgumentException, 094 IllegalAccessException, InvocationTargetException { 095 096 if (obj != null) 097 return toJson(obj, obj.getClass()); 098 else 099 return toJson(obj, null); 100 } 101 /** 102 * Maps a collection to jsonarray based on a class, this method only works 103 * when using the default mapper and is meant to be called via convenience 104 * method on Converter. 105 * 106 * @param col 107 * @param cls 108 * @return 109 * @throws InvocationTargetException 110 * @throws IllegalAccessException 111 * @throws IllegalArgumentException 112 * @throws SecurityException 113 * @throws IllegalArgumentException 114 * @throws NoSuchMethodException 115 * @throws IllegalAccessException 116 * @throws InvocationTargetException 117 */ 118 protected JsonArray toJsonArray(Collection col, Class cls) 119 throws IllegalArgumentException, IllegalAccessException, 120 InvocationTargetException { 121 return toJsonArray(col, cls, false); 122 } 123 /** 124 * Maps a collection to jsonarray based on a class, this method only works 125 * when using the default mapper and is meant to be called via convenience 126 * method on Converter. 127 * 128 * @param col 129 * @param cls 130 * @return 131 * @throws InvocationTargetException 132 * @throws IllegalAccessException 133 * @throws IllegalArgumentException 134 * @throws SecurityException 135 * @throws IllegalArgumentException 136 * @throws NoSuchMethodException 137 * @throws IllegalAccessException 138 * @throws InvocationTargetException 139 */ 140 protected JsonArray toJsonArray(Collection col, Class cls, boolean flat) 141 throws IllegalArgumentException, IllegalAccessException, 142 InvocationTargetException { 143 JsonArray arr = new JsonArray(); 144 145 if (col != null) { 146 for (Object o : col) { 147 Class cls2 = cls; 148 if (cls2 == null) 149 cls2 = o.getClass(); 150 if (!Utils.objectIntoJsonArray(arr, o)) { 151 JsonObject json = toJson(o, cls2, flat); 152 arr.add(json); 153 } 154 } 155 } 156 157 return arr; 158 } 159 160 public JsonArray toJsonArray(Collection col) 161 throws IllegalArgumentException, IllegalAccessException, 162 InvocationTargetException { 163 return toJsonArray(col, null); 164 } 165 166 public JsonArray toJsonArray(Collection col, boolean flat) 167 throws IllegalArgumentException, IllegalAccessException, 168 InvocationTargetException { 169 return toJsonArray(col, null, flat); 170 } 171 }