001 package net.sourceforge.javajson.converter;
002
003 import java.lang.reflect.InvocationTargetException;
004 import java.util.Collection;
005
006 import org.apache.log4j.Logger;
007
008 import net.sourceforge.javajson.JsonArray;
009 import net.sourceforge.javajson.JsonObject;
010
011 /**
012 * Converts java objects to json based on bean-style fields and
013 * classes/interfaces (to filter)
014 *
015 * @author miguel de anda
016 */
017 public class Converter {
018 protected final Logger log = Logger.getLogger(getClass());
019
020 public static final Converter instance = new Converter();
021
022 private Converter() {
023 }
024
025 public static Converter getInstance() {
026 return instance;
027 }
028
029 public Object fromJson(JsonObject json) throws ClassNotFoundException,
030 InstantiationException, IllegalAccessException,
031 IllegalArgumentException, InvocationTargetException {
032 Object o = null;
033 Class cls = Class.forName(json.getString("class"));
034 o = cls.newInstance();
035 Utils.fromJson(o, json);
036 return o;
037 }
038
039 /**
040 * Convenience method for converting objects without a predefined mapper.
041 * This method does not allow objects to be filtered in any way and will
042 * inspect every public getter and return its value, if the value is not a
043 * Number, String or Date, it will then do the conversion on that object as
044 * well. If the return type is a Collection, it will be converted to a
045 * JsonArray
046 *
047 * @param o
048 * @return
049 * @throws SecurityException
050 * @throws IllegalArgumentException
051 * @throws NoSuchMethodException
052 * @throws IllegalAccessException
053 * @throws InvocationTargetException
054 */
055 public JsonObject toJson(Object o) throws SecurityException,
056 IllegalArgumentException, NoSuchMethodException,
057 IllegalAccessException, InvocationTargetException {
058 return Mapper.DefaultMapper.toJson(o);
059 }
060
061 /**
062 * Convenience method for converting objects without a predefined mapper.
063 * This method does not allow objects to be filtered in any way and will
064 * inspect every public getter and return its value, if the value is not a
065 * Number, String or Date, it will then do the conversion on that object as
066 * well. If the return type is a Collection, it will be converted to a
067 * JsonArray
068 *
069 * @param o
070 * @param flat
071 * Don't look at non basic fields (numbers, strings, dates,
072 * boolean)
073 * @return
074 * @throws SecurityException
075 * @throws IllegalArgumentException
076 * @throws NoSuchMethodException
077 * @throws IllegalAccessException
078 * @throws InvocationTargetException
079 */
080 public JsonObject toJson(Object o, boolean flat) throws SecurityException,
081 IllegalArgumentException, NoSuchMethodException,
082 IllegalAccessException, InvocationTargetException {
083 return Mapper.DefaultMapper.toJson(o, flat);
084 }
085
086 /**
087 * Convenience method for converting objects without a predefined mapper.
088 * This method does not allow objects to be filtered in any way and will
089 * inspect every public getter and return its value, if the value is not a
090 * Number, String or Date, it will be skipped.
091 *
092 * @param o
093 * @param cls
094 * The class to read the fields from
095 * @return
096 * @throws SecurityException
097 * @throws IllegalArgumentException
098 * @throws NoSuchMethodException
099 * @throws IllegalAccessException
100 * @throws InvocationTargetException
101 */
102 public JsonObject toJson(Object o, Class cls) throws SecurityException,
103 IllegalArgumentException, NoSuchMethodException,
104 IllegalAccessException, InvocationTargetException {
105 return Mapper.DefaultMapper.toJson(o, cls);
106 }
107
108 /**
109 * Convenience method for converting objects without a predefined mapper.
110 * This method does not allow objects to be filtered in any way and will
111 * inspect every public getter and return its value, if the value is not a
112 * Number, String or Date, it will then do the conversion on that object as
113 * well. If the return type is a Collection, it will be converted to a
114 * JsonArray
115 *
116 * @param o
117 * @return
118 * @throws SecurityException
119 * @throws IllegalArgumentException
120 * @throws NoSuchMethodException
121 * @throws IllegalAccessException
122 * @throws InvocationTargetException
123 */
124 public JsonArray toJsonArray(Collection c) throws SecurityException,
125 IllegalArgumentException, NoSuchMethodException,
126 IllegalAccessException, InvocationTargetException {
127 return Mapper.DefaultMapper.toJsonArray(c);
128 }
129
130 /**
131 * Convenience method for converting objects without a predefined mapper.
132 * This method does not allow objects to be filtered in any way and will
133 * inspect every public getter and return its value, if the value is not a
134 * Number, String or Date, it will then do the conversion on that object as
135 * well. If the return type is a Collection, it will be converted to a
136 * JsonArray
137 *
138 * @param o
139 * @return
140 * @throws SecurityException
141 * @throws IllegalArgumentException
142 * @throws NoSuchMethodException
143 * @throws IllegalAccessException
144 * @throws InvocationTargetException
145 */
146 public JsonArray toJsonArray(Collection c, boolean flat) throws SecurityException,
147 IllegalArgumentException, NoSuchMethodException,
148 IllegalAccessException, InvocationTargetException {
149 return Mapper.DefaultMapper.toJsonArray(c, flat);
150 }
151
152 /**
153 * Convenience method for converting collections without a predefined
154 * mapper. This method does not allow objects to be filtered in any way and
155 * will inspect every public getter and return its value, if the value is
156 * not a Number, String or Date, it will be skipped.
157 *
158 * @param o
159 * @param cls
160 * The class to read the fields from
161 * @return
162 * @throws SecurityException
163 * @throws IllegalArgumentException
164 * @throws NoSuchMethodException
165 * @throws IllegalAccessException
166 * @throws InvocationTargetException
167 */
168 public JsonArray toJsonArray(Collection c, Class cls)
169 throws SecurityException, IllegalArgumentException,
170 NoSuchMethodException, IllegalAccessException,
171 InvocationTargetException {
172 return Mapper.DefaultMapper.toJsonArray(c, cls);
173 }
174
175 /**
176 * Convenience method for converting collections without a predefined
177 * mapper. This method does not allow objects to be filtered in any way and
178 * will inspect every public getter and return its value, if the value is
179 * not a Number, String or Date, it will be skipped.
180 *
181 * @param o
182 * @param cls
183 * The class to read the fields from
184 * @return
185 * @throws SecurityException
186 * @throws IllegalArgumentException
187 * @throws NoSuchMethodException
188 * @throws IllegalAccessException
189 * @throws InvocationTargetException
190 */
191 public JsonArray toJsonArray(Collection c, Class cls, boolean flat)
192 throws SecurityException, IllegalArgumentException,
193 NoSuchMethodException, IllegalAccessException,
194 InvocationTargetException {
195 return Mapper.DefaultMapper.toJsonArray(c, cls, flat);
196 }
197 }