import java.sql.Timestamp;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Set;/** * <p> * <b> The base JSON object. </b> * </p> */public class JsonObject { /** * The string field map */ PRivate Map<String, String> stringFieldMap; /** * The integer field map */ private Map<String, Integer> intFieldMap; /** * The double field map */ private Map<String, Double> doubleFieldMap; /** * The date field map */ private Map<String, Date> dateFieldMap; /** * The timestamp field map */ private Map<String, Timestamp> timestampFieldMap; /** * The map<String, String> field map */ private Map<String, Map<String, String>> stringMapFieldMap; /** * The string set field map */ private Map<String, Set<String>> stringCollectionFieldMap; /** * The map<String, Set<String, String>> field map */ private Map<String, Map<String, Set<String>>> stringSetMapFieldMap; public JsonObject() { this.stringFieldMap = new HashMap<String, String>(); this.intFieldMap = new HashMap<String, Integer>(); this.doubleFieldMap = new HashMap<String, Double>(); this.dateFieldMap = new HashMap<String, Date>(); this.timestampFieldMap = new HashMap<String, Timestamp>(); this.stringMapFieldMap = new HashMap<String, Map<String, String>>(); this.stringCollectionFieldMap = new HashMap<String, Set<String>>(); this.stringSetMapFieldMap = new HashMap<String, Map<String, Set<String>>>(); } /** * @return the stringFieldMap */ public Map<String, String> getStringFieldMap() { return this.stringFieldMap; } /** * @return the intFieldMap */ public Map<String, Integer> getIntFieldMap() { return this.intFieldMap; } /** * @return the doubleFieldMap */ public Map<String, Double> getDoubleFieldMap() { return this.doubleFieldMap; } /** * @return the dateFieldMap */ public Map<String, Date> getDateFieldMap() { return this.dateFieldMap; } /** * @return the timestampFieldMap */ public Map<String, Timestamp> getTimestampFieldMap() { return this.timestampFieldMap; } /** * @return the stringMapFieldMap */ public Map<String, Map<String, String>> getStringMapFieldMap() { return this.stringMapFieldMap; } /** * @return the stringCollectionFieldMap */ public Map<String, Set<String>> getStringCollectionFieldMap() { return this.stringCollectionFieldMap; } /** * @return the stringSetMapFieldMap */ public Map<String, Map<String, Set<String>>> getStringSetMapFieldMap() { return this.stringSetMapFieldMap; } /** * @param stringFieldMap * the stringFieldMap to set */ public void setStringFieldMap(final Map<String, String> stringFieldMap) { this.stringFieldMap = stringFieldMap; } /** * @param intFieldMap * the intFieldMap to set */ public void setIntFieldMap(final Map<String, Integer> intFieldMap) { this.intFieldMap = intFieldMap; } /** * @param doubleFieldMap * the doubleFieldMap to set */ public void setDoubleFieldMap(final Map<String, Double> doubleFieldMap) { this.doubleFieldMap = doubleFieldMap; } /** * @param dateFieldMap * the dateFieldMap to set */ public void setDateFieldMap(final Map<String, Date> dateFieldMap) { this.dateFieldMap = dateFieldMap; } /** * @param timestampFieldMap * the timestampFieldMap to set */ public void setTimestampFieldMap(final Map<String, Timestamp> timestampFieldMap) { this.timestampFieldMap = timestampFieldMap; } /** * @param stringMapFieldMap * the stringMapFieldMap to set */ public void setStringMapFieldMap(final Map<String, Map<String, String>> stringMapFieldMap) { this.stringMapFieldMap = stringMapFieldMap; } /** * @param stringCollectionFieldMap * the stringCollectionFieldMap to set */ public void setStringCollectionFieldMap(final Map<String, Set<String>> stringCollectionFieldMap) { this.stringCollectionFieldMap = stringCollectionFieldMap; } /** * @param stringSetMapFieldMap * the stringSetMapFieldMap to set */ public void setStringSetMapFieldMap(final Map<String, Map<String, Set<String>>> stringSetMapFieldMap) { this.stringSetMapFieldMap = stringSetMapFieldMap; } /** * Put string value * * @param key * @param value */ public void putString(final String key, final String value) { this.stringFieldMap.put(key, value); } /** * put integer value * * @param key * @param value */ public void putInteger(final String key, final Integer value) { this.intFieldMap.put(key, value); } /** * Put double value * * @param key * @param value */ public void putDouble(final String key, final Double value) { this.doubleFieldMap.put(key, value); } /** * Put date value * * @param key * @param value */ public void putDate(final String key, final Date value) { this.dateFieldMap.put(key, value); } /** * Put timestamp value * * @param key * @param value */ public void putTimestamp(final String key, final Timestamp value) { this.timestampFieldMap.put(key, value); } /** * Put string map value * * @param key * @param value */ public void putStringMap(final String key, final Map<String, String> value) { this.stringMapFieldMap.put(key, value); } /** * Put string collection value * * @param key * @param value */ public void putStringCollection(final String key, final Set<String> value) { this.stringCollectionFieldMap.put(key, value); } /** * Put string set map value * * @param key * @param value */ public void putStringSetMap(final String key, final Map<String, Set<String>> value) { this.stringSetMapFieldMap.put(key, value); } /** * @return the stringMap */ public String getString(final String key) { return this.stringFieldMap.get(key); } /** * @return the integer */ public Integer getInteger(final String key) { return this.intFieldMap.get(key); } /** * @return the double */ public Double getDouble(final String key) { return this.doubleFieldMap.get(key); } /** * @return the date */ public Date getDate(final String key) { return this.dateFieldMap.get(key); } /** * @return the timestamp */ public Timestamp getTimestamp(final String key) { return this.timestampFieldMap.get(key); } /** * @return the string map */ public Map<String, String> getStringMap(final String key) { return this.stringMapFieldMap.get(key); } /** * @return the string collection */ public Collection<String> getStringCollection(final String key) { return this.stringCollectionFieldMap.get(key); } /** * @return the string set map */ public Map<String, Set<String>> getStringSetMap(final String key) { return this.stringSetMapFieldMap.get(key); }}
==========================================================
import java.io.IOException;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import com.fasterxml.jackson.core.JsonGenerationException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;import JsonObject;/** * <p> * <b> Insert description of the class's responsibility/role. </b> * </p> */public class JsonUtil { static private String GET = "get"; static private String IS = "is"; static private String SET = "set"; static public String convertFromJsonObjectToString(final Object obj) throws JsonGenerationException, JsonMappingException, IOException, IllegalArgumentException, IllegalaccessException, InvocationTargetException, InstantiationException { ObjectMapper mapper = new ObjectMapper(); if (obj instanceof JsonObject) { return mapper.writeValueAsString(obj); } else { return mapper.writeValueAsString(mapObjectToJsonObject(obj)); } } static public String convertFromObjectToString(final Object obj) throws JsonGenerationException, JsonMappingException, IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); } static public JsonObject convertFromStringToJsonObject(final String json) throws JsonGenerationException, JsonMappingException, IOException { if (json != null) { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(json, JsonObject.class); } else { return new JsonObject(); } } static public Object convertFromStringToObject(final String json, final Class clazz) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(json, clazz); } static public Object convertFromStringToObject(final String json, final Class clazz, final String dateformat) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); SimpleDateFormat df = new SimpleDateFormat(dateformat); mapper.setDateFormat(df); return mapper.readValue(json, clazz);} static private JsonObject mapObjectToJsonObject(final Object obj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { Map<String, Method> methods = getAllMethod(obj.getClass()); List<Field> fields = getAllField(obj.getClass()); JsonObject json = new JsonObject(); for (Field f : fields) { Object value = getFieldValue(methods, obj, f); if (value != null) { if (value instanceof String) { json.putString(f.getName(), (String) value); } else if (value instanceof Integer) { json.putInteger(f.getName(), (Integer) value); } else if (value instanceof Double) { json.putDouble(f.getName(), (Double) value); } else if (value instanceof Timestamp) { json.putTimestamp(f.getName(), (Timestamp) value); } else if (value instanceof Date) { json.putDate(f.getName(), (Date) value); } else if (value instanceof Map) { if (isMapKeyInstanceOf(f, new String()) && isMapValueInstanceOf(f, new String())) { json.putStringMap(f.getName(), (Map<String, String>) value); } } else if (value instanceof Set) { if (isGenericInstanceOf(f, new String())) { json.putStringCollection(f.getName(), new HashSet((Set<String>) value)); } } } } return json; } static private Object getFieldValue(final Map<String, Method> methods, final Object obj, final Field field) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Method getMethod = methods.get(accessMethodNameGet(field.getName())); Method isMethod = methods.get(accessMethodNameIs(field.getName())); if (getMethod == null && isMethod == null) { return null; } else if (isMethod != null) { return isMethod.invoke(obj, null); } else { return getMethod.invoke(obj, null); } } static private List<Field> getAllField(final Class cls) { List<Field> list = new ArrayList<Field>(); list.addAll(Arrays.asList(cls.getDeclaredFields())); if (cls.getSuperclass() != null) { list.addAll(getAllField(cls.getSuperclass())); } return list; } static private Map<String, Method> getAllMethod(final Class cls) { Map<String, Method> map = new HashMap<String, Method>(); for (Method method : cls.getDeclaredMethods()) { map.put(method.getName(), method); } if (cls.getSuperclass() != null) { map.putAll(getAllMethod(cls.getSuperclass())); } return map; } static private String accessMethodNameGet(final String instance) { StringBuffer result = new StringBuffer(instance); return JsonUtil.GET.concat(result.deleteCharAt(0).insert(0, instance.toUpperCase().charAt(0)).toString()); } static private String accessMethodNameIs(final String instance) { StringBuffer result = new StringBuffer(instance); return JsonUtil.IS.concat(result.deleteCharAt(0).insert(0, instance.toUpperCase().charAt(0)).toString()); } static private boolean isMapKeyInstanceOf(final Field field, final Object key) throws IllegalAccessException, InstantiationException { ParameterizedType type = (ParameterizedType) field.getGenericType(); Class<?> cls = (Class<?>) type.getActualTypeArguments()[0]; return cls.isInstance(key); } static private boolean isMapValueInstanceOf(final Field field, final Object value) throws IllegalAccessException, InstantiationException { ParameterizedType type = (ParameterizedType) field.getGenericType(); Class<?> cls = (Class<?>) type.getActualTypeArguments()[0]; return cls.isInstance(value); } static private boolean isGenericInstanceOf(final Field field, final Object genericObj) throws IllegalAccessException, InstantiationException { ParameterizedType type = (ParameterizedType) field.getGenericType(); Class<?> cls = (Class<?>) type.getActualTypeArguments()[0]; return cls.isInstance(genericObj); }}