JSON 序列化 jsonconfig
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(java.sql.Date.class,
new SQLDateJsonValueProcessor("yyyy-MM-dd"));
config.registerJsonValueProcessor(java.sql.Timestamp.class,
new TimestampJsonValueProcessor("yyyy-MM-dd HH:mm:ss.SSS"));
config.registerJsonValueProcessor(java.math.BigDecimal.class,
new BigDecimalJsonValueProcessor());
config.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if (name.equals("dbFields")) {
return true;
} else {
return false;
}
}
});
//JSON 反序列化配置
JSONUtils.getMorpherRegistry().registerMorpher(
new SQLDateMorpher("yyyy-MM-dd"));// 重新定义日期格式
JSONUtils.getMorpherRegistry().registerMorpher(
new TimestampMorpher(new String[] { "yyyy-MM-dd",
"yyyy-MM-dd HH:mm:ss.SSS" }));// 重新定义日期格式
/**
*
* @author ZHANG
*/
public class SQLDateJsonValueProcessor implements JsonValueProcessor {
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
private DateFormat dateFormat;
/**
*
* @param datePattern
*/
public SQLDateJsonValueProcessor(String datePattern) {
try {
dateFormat = new SimpleDateFormat(datePattern);
} catch (Exception ex) {
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) {
if(value==null){
return null;
}
return dateFormat.format((Date) value);
}
}
------------------------------------------------------
public class SQLDateMorpher extends AbstractObjectMorpher {
private String DATE_TIME_FORMAT = "yyyy-MM-dd";
private SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
public SQLDateMorpher(){
}
public SQLDateMorpher(String param){
this.DATE_TIME_FORMAT=param;
}
@Override
public Object morph(Object value) {
if(value!=null && value!="" && value instanceof String) {
String s = (String) value;
try {
return java.sql.Date(((java.util.Date)dateTimeFormat.parse(s)).getTime());
} catch (ParseException e) {
return null;
}
}
return null;
}
@Override
public Class morphsTo() {
return java.sql.Date.class;
}
}
--------------------------------------------------------------------
public class TimestampJsonValueProcessor implements JsonValueProcessor {
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
private DateFormat dateFormat;
/**
*
* @param datePattern
*/
public TimestampJsonValueProcessor(String datePattern) {
try {
dateFormat = new SimpleDateFormat(datePattern);
} catch (Exception ex) {
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) {
if(value==null){
return null;
}
return dateFormat.format((Timestamp) value);
}
}
--------------------------------------------------------------------------
public class TimestampMorpher extends AbstractObjectMorpher {
private Timestamp defaultValue;
private String[] formats;
private boolean lenient;
private Locale locale;
/**
* @param formats a list of formats this morpher supports.
*/
public TimestampMorpher( String[] formats )
{
this( formats, Locale.getDefault(), false );
}
/**
* @param formats a list of formats this morpher supports.
* @param lenient if the parsing should be lenient or not.
*/
public TimestampMorpher( String[] formats, boolean lenient )
{
this( formats, Locale.getDefault(), lenient );
}
/**
* @param formats a list of formats this morpher supports.
* @param defaultValue return value if the value to be morphed is null.
*/
public TimestampMorpher( String[] formats, Timestamp defaultValue )
{
this( formats, defaultValue, Locale.getDefault(), false );
}
/**
* @param formats a list of formats this morpher supports.
* @param defaultValue return value if the value to be morphed is null.
* @param locale the Locale used to parse each format.
* @param lenient if the parsing should be lenient or not.
*/
public TimestampMorpher( String[] formats, Timestamp defaultValue, Locale locale, boolean lenient )
{
super( true );
if( formats == null || formats.length == 0 ){
throw new MorphException( "invalid array of formats" );
}
// should use defensive copying ?
this.formats = formats;
if( locale == null ){
this.locale = Locale.getDefault();
}else{
this.locale = locale;
}
this.lenient = lenient;
setDefaultValue( defaultValue );
}
/**
* @param formats a list of formats this morpher supports.
* @param locale the Locale used to parse each format.
*/
public TimestampMorpher( String[] formats, Locale locale )
{
this( formats, locale, false );
}
/**
* @param formats a list of formats this morpher supports.
* @param locale the Locale used to parse each format.
* @param lenient if the parsing should be lenient or not.
*/
public TimestampMorpher( String[] formats, Locale locale, boolean lenient )
{
if( formats == null || formats.length == 0 ){
throw new MorphException( "invalid array of formats" );
}
// should use defensive copying ?
this.formats = formats;
if( locale == null ){
this.locale = Locale.getDefault();
}else{
this.locale = locale;
}
this.lenient = lenient;
}
public boolean equals( Object obj )
{
if( this == obj ){
return true;
}
if( obj == null ){
return false;
}
if( !(obj instanceof TimestampMorpher) ){
return false;
}
TimestampMorpher other = (TimestampMorpher) obj;
EqualsBuilder builder = new EqualsBuilder();
builder.append( formats, other.formats );
builder.append( locale, other.locale );
builder.append( lenient, other.lenient );
if( isUseDefault() && other.isUseDefault() ){
builder.append( getDefaultValue(), other.getDefaultValue() );
return builder.isEquals();
}else if( !isUseDefault() && !other.isUseDefault() ){
return builder.isEquals();
}else{
return false;
}
}
/**
* Returns the default value for this Morpher.
*/
public Timestamp getDefaultValue()
{
return (Timestamp) defaultValue.clone();
}
public int hashCode()
{
HashCodeBuilder builder = new HashCodeBuilder();
builder.append( formats );
builder.append( locale );
builder.append( lenient );
if( isUseDefault() ){
builder.append( getDefaultValue() );
}
return builder.toHashCode();
}
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() + " is not supported" );
}
String strValue = (String) value;
SimpleDateFormat dateParser = null;
for( int i = 0; i < formats.length; i++ ){
if(strValue.isEmpty()){
return null;
}
if( dateParser == null ){
dateParser = new SimpleDateFormat( formats[i], locale );
}else{
dateParser.applyPattern( formats[i] );
}
dateParser.setLenient( lenient );
try{
return new java.sql.Timestamp(((java.util.Date)dateParser.parse( strValue.toLowerCase())).getTime());
}
catch( ParseException pe ){
// ignore exception, try the next format
}
}
// unable to parse the date
if( isUseDefault() ){
return defaultValue;
}else{
throw new MorphException( "Unable to parse the date " + value );
}
}
public Class morphsTo()
{
return Timestamp.class;
}
/**
* Sets the defaultValue to use if the value to be morphed is null.
*
* @param defaultValue return value if the value to be morphed is null
*/
public void setDefaultValue( Timestamp defaultValue )
{
this.defaultValue = (Timestamp) defaultValue.clone();
}
public boolean supports( Class clazz )
{
return String.class.isAssignableFrom( clazz );
}
}