日期:2014-05-16 浏览次数:20420 次
package com.skmbw.yinlei.mongo; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.apache.commons.lang3.StringUtils; import org.springframework.cglib.beans.BeanMap; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.esotericsoftware.reflectasm.MethodAccess; public class JSONUtils { public static ConcurrentMap<String, BeanMap> beanMapCache = new ConcurrentHashMap<String, BeanMap>(); //JSONObject就是个Map,它实现了Map接口 public static Map<String, ?> toMap(Object object) { JSONObject jsonObject = (JSONObject)JSON.toJSON(object); return jsonObject; } //去掉null public static Map<String, ?> toMaps(Object object) { Map<String, Object> map = new HashMap<String, Object>(); JSONObject jsonObject = (JSONObject) JSON.toJSON(object); for (Entry<String, Object> entry : jsonObject.entrySet()) { if (entry.getValue() != null) { map.put(entry.getKey(), entry.getValue()); } } return map; } public static void main(String[] aa) { User user = new User(); user.setAccount("asdf"); user.setAge(22); user.setDate(new Date()); Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result2 = new HashMap<String, Object>(); long d2 = System.nanoTime(); JSONObject jsonObject = (JSONObject)JSON.toJSON(user); String json = jsonObject.toJSONString(); Map<String, Object> jsonMap = JSON.parseObject(json); System.out.println(System.nanoTime() - d2); long dd = System.nanoTime(); Map<String, Object> map = (Map<String, Object>)jsonObject; result.putAll(map); System.out.println(System.nanoTime() - dd); long d = System.nanoTime(); for (Entry<String, Object> entry : jsonObject.entrySet()) { if (entry.getValue() != null) { result2.put(entry.getKey(), entry.getValue()); } } System.out.println(System.nanoTime() - d); Map<String, Object> toMap = new HashMap<String, Object>(); beanToMap(user, toMap); Map<String, Object> toMap2 = beanToMap(user); System.out.println(toMap2); } public static void beanToMap(Object fromBean, Map<String, Object> toMap) { //MethodAccess要缓存 MethodAccess methodAccess = MethodAccess.get(fromBean.getClass()); String[] methodNames = methodAccess.getMethodNames(); for (String methodName : methodNames){ if (methodName.startsWith("get")){ Object value = methodAccess.invoke(fromBean, methodName, (Object[])null); toMap.put(StringUtils.uncapitalize(methodName.substring(3)), value); } } } public static BeanMap getBeanMap(Object object) { BeanMap beanMap = beanMapCache.get(object.getClass().getName()); if (beanMap == null) { beanMap = BeanMap.create(object); beanMapCache.put(object.getClass().getName(), beanMap); } return beanMap; } //如果使用BeanMap缓存,这个性能最好。 public static Map<String, Object> beanToMap(Object object) { BeanMap beanMap = getBeanMap(object); beanMap.setBean(object); @SuppressWarnings("unchecked") Map<String, Object> toMap = beanMap; for (Entry<String, Object> entry : toMap.entrySet()) { if (entry.getValue() != null) { toMap.put(entry.getKey(), entry.getValue()); } } return toMap; } }
因为mongodb的bosnobject,有一个putAll(Map)的方法,可以快速构建BsonObject。