public class MyDateJsonBeanProcessor implements net.sf.json.processors.JsonBeanProcessor {
private static Logger logger = Logger.getLogger(MyDateJsonBeanProcessor .class) ;
public JSONObject processBean(Object bean, JsonConfig jsonConfig) {
JSONObject jsonObject = null;
System.out.println(bean);
logger.info("processor class name:" + bean.getClass().getName());
if (bean instanceof java.util.Date) {
System.out.println("HAHA");
jsonObject = new JSONObject();
jsonObject.element("time", ((Date) bean).getTime());
} else {
jsonObject = new JSONObject(true);
}
return jsonObject;
}
}
public class MyObjectUtil {
public MyObjectUtil() {
super();
}
public final static int sizeof(Object object){
try{
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(object);
byte[] byteArray=byteOut.toByteArray();
return byteArray.length;
}catch(Exception e){
try{
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HessianOutput out = new HessianOutput(byteOut);
out.writeObject(object);
byte[] byteArray=byteOut.toByteArray();
return byteArray.length;
}catch(Exception ex){
e.printStackTrace();
ex.printStackTrace();
}
}
return -1;
}
/**
* 将JAVA对象转换为JSON格式的字符串
* @param object
* @return String
*/
public static String convert2Json(Object object){
if(object instanceof String)
return object.toString();
if((object instanceof Object[])||(object instanceof List)){
JSONArray jsonObject = JSONArray.fromObject(object);
return jsonObject.toString()+'\n';
}else{
JSONObject jsonObject = JSONObject.fromObject(object);
return jsonObject.toString()+'\n';
}
}
/**
*
* @Description 将JAVA对象转换为JSON格式的字符串 升级版本
* @create Jul 5, 2010
* @auhtor 覃正平
* @param object : 对象
* @return String
*/
public static String convert2Json2(Object object){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor(){
private final String format = "yyyy-MM-dd hh:mm:ss" ;
public Object processArrayValue(Object object, JsonConfig jsonConfig) {
return null;
}
public Object processObjectValue(String string, Object object,JsonConfig jsonConfig) {
if(null == object ){
return "" ;
}else{
if(object instanceof java.util.Date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format) ;
String dateStr = simpleDateFormat.format(object) ;
return dateStr ;
}
}
return object.toString();
}
}) ;
if(object instanceof String)
return object.toString();
if((object instanceof Object[])||(object instanceof List)){
JSONArray jsonObject = JSONArray.fromObject(object,jsonConfig);
return jsonObject.toString()+'\n';
}else{
JSONObject jsonObject = JSONObject.fromObject(object,jsonConfig);
return jsonObject.toString()+'\n';
}
}
/**
*
* @Description 将JSON格式的字符串转换为JAVA对象 升级版本
* @create Jul 5, 2010
* @auhtor 覃正平
* @param clazz : 转换成的类型 (例如 Tree.class)
* @param json : 传入的JSON表达式 (例如 {"address":"湖南大厦","createDate":"2010-07-05 06:10:54","id":123})
* @return Object
*/
public static Object convert2Object2(Class clazz ,String json){
final String[] DATE_FORMAT = { "yyyy-MM-dd HH:mm:ss" };
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
morpherRegistry.registerMorpher(new DateMorpher(DATE_FORMAT));
JSONObject jsonObject = JSONObject.fromObject(json);
Object obj = JSONObject.toBean(jsonObject, clazz);
return obj ;
}
}
调用方式
CustomerOrderAskPojo pojo = new CustomerOrderAskPojo() ;
pojo.setStatusDate(new Date()) ;
String json = MyObjectUtil.convert2Json2(pojo);
System.out.println(json);
Thread.sleep(2000);
//
CustomerOrderAskPojo t = (CustomerOrderAskPojo)MyObjectUtil.convert2Object2(CustomerOrderAskPojo.class, json) ;
System.out.println(t.getStatusDate());[size=medium][/size]
[/size]