在项目中JavaBean和Json互转时候,因为bean中有timestamp类型的字段,所以需要处理一下。
创建两个转换器,DateJsonValueProcessor和TimestampMorpher。
DateJsonValueProcessor:用于Json转JavaBean
import net.sf.ezmorph.MorphException; import net.sf.ezmorph.object.AbstractObjectMorpher; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; /** * Created with IntelliJ IDEA. * User: huangzhang * Date: 19-05-28 * Time: 下午01:02 * Desc: 用于Json转换成JavaBean */ public class TimestampMorpher extends AbstractObjectMorpher { /** * 日期字符串格式 */ private String[] formats; public TimestampMorpher(String[] formats) { this.formats = formats; } 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 boolean supports( Class clazz ){ return String.class.isAssignableFrom( clazz ); } }
DateJsonValueProcessor:用于JavaBean转Json
import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * Created with IntelliJ IDEA. * User: huangzhang * Date: 19-05-28 * Time: 下午01:02 * Desc: 用于JavaBean转Json */ public class DateJsonValueProcessor implements JsonValueProcessor { public static final String Default_DATE_PATTERN ="yyyy-MM-dd"; private DateFormat dateFormat ; public DateJsonValueProcessor(String datePattern){ try{ dateFormat = new SimpleDateFormat(datePattern); }catch(Exception e ){ dateFormat = new SimpleDateFormat(Default_DATE_PATTERN); } } public Object processArrayValue(Object value, JsonConfig jsonConfig) { return process(value); } public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) { return process(value); } private Object process(Object value){ return dateFormat.format((Date)value); } }
代码示例:
//JavaBean转Json JsonConfig config=new JsonConfig(); config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss")); JSONObject jsonObject = JSONObject.fromObject(JavaBean,config); //Json转JavaBean String[] formats={"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"}; JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(formats)); JSONObject jsonObject=JSONObject.fromObject(message.getWparam()); branchTpm = (BranchTpm)JSONObject.toBean(jsonObject,JavaBean.class);
只是这样还不够,我这里还报了一个错,在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常。
意思是出现了死循环,也就是Model之间有循环包含关系;
在JavaBean转Json的时候需要加入一些配置,config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
示例:
JsonConfig config=new JsonConfig(); config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss")); config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONObject jsonObject = JSONObject.fromObject(JavaBean,config);