Json与Bean互转,Timestamp类型的问题

阅读更多
    Json与Java Bean互相转换时,Bean中的Timestamp字段是无法直接处理的,需要实现两个转换器。

    DateJsonValueProcessor的作用是Bean转换为Json时将Timepstamp转换为指定的时间格式。

import java.text.SimpleDateFormat;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class DateJsonBeanProcessor implements JsonValueProcessor {

	private String[] formats;

	public DateJsonBeanProcessor(String[] formats) {
		this.formats = formats;
	}

	@Override
	public Object processArrayValue(Object arg0, JsonConfig arg1) {
		return null;
	}

	@Override
	public Object processObjectValue(String key, Object value, JsonConfig arg2) {

		
		if (value instanceof java.sql.Timestamp) {

			SimpleDateFormat dateParser = null;
			for (int i = 0; i < formats.length; i++) {
				if (null == dateParser) {
					dateParser = new SimpleDateFormat(formats[i]);
				} else {
					dateParser.applyPattern(formats[i]);
				}
				try {
					return new SimpleDateFormat(formats[i]).format((java.sql.Timestamp) value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			return null;
		}
		return value.toString();
	}

}


TimestampMorpher的作用则与DateJsonValueProcessor相反,它是在Jsonl转换为Bean时,会把指定的时间格式转换为Timestamp
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import net.sf.ezmorph.MorphException;
import net.sf.ezmorph.object.AbstractObjectMorpher;

public class TimestampMorpher extends AbstractObjectMorpher {

	private String[] formats;

	public TimestampMorpher(String[] formats) {
		this.formats = formats;
	}

	@Override
	public Object morph(Object value) {

		if (value == null) {
			return null;
		}
		if (Timestamp.class.isAssignableFrom(value.getClass())) {
			return (Timestamp) value;
		}
		if (!supports(value.getClass())) {
			throw new MorphException(value.getClass() + " 是不支持的类型");
		}
		String strValue = (String) value;
		SimpleDateFormat dateParser = null;
		for (int i = 0; i < formats.length; i++) {
			if (null == dateParser) {
				dateParser = new SimpleDateFormat(formats[i]);
			} else {
				dateParser.applyPattern(formats[i]);
			}
			try {
				return new Timestamp(dateParser.parse(strValue.toLowerCase()).getTime());
			} catch (ParseException e) {
				// e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public Class morphsTo() {

		return Timestamp.class;
	}

}


测试代码:
public class JsonTest {  
public static void main(String[] args) {  
    String jsonStr="{\"id\":\"101\",\"name\":\"张三\",\"age\":\"20\",\"birthday\":\"1992-10-19 23:52:18\"}";  
    Student s = new Student();  
    Timestamp b = Timestamp.valueOf("1992-10-19 23:52:18");  
    s.setId(123456);  
    s.setName("李四");  
    s.setAge(20);  
    s.setBirthday(b); 

    String[] formats = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"};  
    JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(formats));  
    JSONObject jsonObject = JSONObject.fromObject(jsonStr); 
    Student s1 = (Student)JSONObject.toBean(jsonObject,Student.class);

    System.out.println(s1.getBirthday());

    JsonConfig config=new JsonConfig();  
    config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));  
    JSONObject json=JSONObject.fromObject(s1,config); 
    System.out.println(json.toString());
    }
}

你可能感兴趣的:(json,bean,Timestamp)