自定义JSONUtil对象

 

Sturts2漏洞越来越多,所以博主特地放弃了Struts2,

 

开始使用Spring MVC,虽然各种不适应,但是还是解决了不少问题

 

为了解决Spring MVC和Json不和谐的问题,特地自己写了个JSONUtil对象,方便进行前台交互。

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class JSONUtil {
	public static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	public static String serialize(Object o,StringBuffer s,Class<?>... parseClazz) throws Exception{
		Class<?> clazz = o.getClass();
		
		List<Class<?>> context = new ArrayList<Class<?>>();
		for(Class<?> c :parseClazz){
			context.add(c);
		}
		
		if(clazz.isArray()){
			s.append("[");
			for(Object sub:(Object[])o){
				JSONUtil.covert(sub, context, s, parseClazz);
				s.append(",");
			}
			s.delete(s.length()-1, s.length());
			s.append("]");
		} else if(o instanceof Collection<?>){
			s.append("[");
			
			Collection<?> collection = (Collection<?>) o;
			for(Iterator<?> iterator = collection.iterator();iterator.hasNext();){
				JSONUtil.covert(iterator.next(), context, s, parseClazz);
				s.append(",");
			}
			s.delete(s.length()-1, s.length());
			s.append("]");
		} else {
			s=JSONUtil.covert(o, context, s, parseClazz);
		}
		return s.toString();
	}
	
	private static StringBuffer covert(Object o,List<Class<?>> context,StringBuffer s,Class<?>... parseClazz) throws Exception{
		Class<?> clazz = o.getClass();
		s.append("{");
		
		PropertyDescriptor[] props = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
		for (PropertyDescriptor prop : props) {
			
			if (prop.getReadMethod() != null && prop.getReadMethod().invoke(o) != null&& !"class".equals(prop.getDisplayName())) {
				
				s.append("\"").append(prop.getName()).append("\"").append(":");
				
				Object value = prop.getReadMethod().invoke(o);
				
				if(prop.getPropertyType() == String.class
						|| prop.getPropertyType() == Boolean.class
						|| prop.getPropertyType() == boolean.class
						|| prop.getPropertyType() == Character.class
						|| prop.getPropertyType() == char.class){
					s.append("\"").append(value).append("\"");
				} else if (isWrapClass(prop.getPropertyType())
						|| prop.getPropertyType().isPrimitive()){
					s.append(value);
				} else if(value.getClass() == java.util.Date.class || value.getClass() == java.sql.Date.class || value.getClass() == java.sql.Timestamp.class){
					s.append("\"").append(new SimpleDateFormat(DATE_FORMAT).format(value)).append("\"");
				} else if(prop.getPropertyType() == clazz){
					s.delete(s.lastIndexOf(","), s.length());
				} else if(context.contains(prop.getPropertyType())
						|| (value.getClass().isArray() && ((Object[])value).length>0 && context.contains(((Object[])value)[0].getClass()))
						|| value instanceof Collection<?>){
					JSONUtil.serialize(value,s,parseClazz);
				} else {
					s.delete(s.lastIndexOf(","), s.length());
				}
				s.append(",");
			}
		}
		
		s.delete(s.length()-1, s.length());
		s.append("}");
		
		return s;
	}
	
	private static boolean isWrapClass(Class<?> clazz){  
	    try {  
	        return ((Class<?>)clazz.getField("TYPE").get(null)).isPrimitive();
	    } catch (Exception e){  
	        return false;  
	    }  
	}
}

 

 

你可能感兴趣的:(json,spring mvc)