View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-8-14 上午10:32:38
4    */
5   package gboat2.base.core.util;
6   
7   import gboat2.base.core.util.json.BigDecimalDefaultValueProcessor;
8   import gboat2.base.core.util.json.GboatJsonBigDecimalValueProcessor;
9   import gboat2.base.core.util.json.GboatJsonDateValueProcessor;
10  import gboat2.base.core.util.json.GboatJsonEnumValueProcessor;
11  import gboat2.base.core.util.json.GboatJsonFloatValueProcessor;
12  
13  import java.lang.reflect.Method;
14  import java.math.BigDecimal;
15  import java.util.LinkedList;
16  import java.util.List;
17  
18  import javax.persistence.Enumerated;
19  
20  import net.sf.json.JsonConfig;
21  
22  /**
23   * 
24   * Gboat平台jsonConfig统一配置类
25   * @author lysming
26   * @since jdk1.6
27   * @date 2012-8-14
28   *  
29   */
30  
31  public class JsonConfigUtil {
32  
33  	private static JsonConfig DEFAULT_CONFIG;
34  	
35  	public static JsonConfig getJsonConfig(List<Class<?>> enumClasses) {
36  		JsonConfig jsonConf = getDefaultJsonConfig();
37  		if (enumClasses != null) {
38  			for (Class<?> clazz : enumClasses) {
39  				jsonConf.registerJsonValueProcessor(clazz, new GboatJsonEnumValueProcessor());
40  			}
41  		}
42  		return jsonConf;
43  	}
44  
45  	public static List<Class<?>> getEnumClassInDomain(Class <?>domainClass) {
46  		List<Class<?>> clazzes = new LinkedList<Class<?>>();
47  		if (domainClass != null) {
48  			//@Entity
49  			Method[] methods = domainClass.getMethods();
50  			Class<?> enumClass;
51  			for (Method method : methods) {
52  				if (method.getAnnotation(Enumerated.class) != null) {
53  					enumClass = method.getReturnType();
54  					clazzes.add(enumClass);
55  				}
56  			}
57  
58  		}
59  
60  		return clazzes;
61  	}
62  	
63  	private static JsonConfig getDefaultJsonConfig() {
64  		JsonConfig jsonConf = new JsonConfig();
65  		jsonConf.registerDefaultValueProcessor(BigDecimal.class, new BigDecimalDefaultValueProcessor());
66  		jsonConf.registerJsonValueProcessor(java.util.Date.class, new GboatJsonDateValueProcessor(""));
67  		jsonConf.registerJsonValueProcessor(java.sql.Date.class, new GboatJsonDateValueProcessor(""));
68  		jsonConf.registerJsonValueProcessor(BigDecimal.class, new GboatJsonBigDecimalValueProcessor(""));
69  		jsonConf.registerJsonValueProcessor(float.class, new GboatJsonFloatValueProcessor());
70  		jsonConf.registerJsonValueProcessor(Enum.class, new GboatJsonEnumValueProcessor());
71  		
72  		return jsonConf;
73  	}
74  	
75  	public static JsonConfig getJsonConfig() {
76  		if(DEFAULT_CONFIG==null){
77  			DEFAULT_CONFIG = getDefaultJsonConfig();
78  		}
79  		
80  		return DEFAULT_CONFIG;
81  	}
82  
83  }