|
14 | 14 |
|
15 | 15 | package apijson.demo; |
16 | 16 |
|
| 17 | +import apijson.JSONParser; |
| 18 | +import apijson.framework.*; |
| 19 | +import com.alibaba.fastjson.JSON; |
| 20 | +import com.alibaba.fastjson.JSONArray; |
| 21 | +import com.alibaba.fastjson.JSONObject; |
| 22 | +import com.alibaba.fastjson.parser.Feature; |
17 | 23 | import org.springframework.boot.SpringApplication; |
18 | | -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
19 | 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
20 | 25 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
21 | 26 | import org.springframework.boot.web.server.WebServerFactoryCustomizer; |
|
26 | 31 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
27 | 32 |
|
28 | 33 | import apijson.Log; |
29 | | -import apijson.framework.APIJSONApplication; |
30 | | -import apijson.framework.APIJSONCreator; |
31 | | -import apijson.orm.SQLConfig; |
| 34 | + |
| 35 | +import java.util.List; |
32 | 36 |
|
33 | 37 |
|
34 | 38 | /** |
@@ -73,12 +77,83 @@ public void addCorsMappings(CorsRegistry registry) { |
73 | 77 | } |
74 | 78 |
|
75 | 79 | static { |
| 80 | + // 使用 fastjson |
| 81 | + apijson.JSON.JSON_OBJECT_CLASS = JSONObject.class; |
| 82 | + apijson.JSON.JSON_ARRAY_CLASS = JSONArray.class; |
| 83 | + |
| 84 | + final Feature[] DEFAULT_FASTJSON_FEATURES = {Feature.OrderedField, Feature.UseBigDecimal}; |
| 85 | + apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() { |
| 86 | + |
| 87 | + @Override |
| 88 | + public JSONObject createJSONObject() { |
| 89 | + return new JSONObject(true); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public JSONArray createJSONArray() { |
| 94 | + return new JSONArray(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String toJSONString(Object obj) { |
| 99 | + return obj == null || obj instanceof String ? (String) obj : JSON.toJSONString(obj); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Object parseJSON(Object json) { |
| 104 | + return JSON.parse(toJSONString(json), DEFAULT_FASTJSON_FEATURES); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public JSONObject parseObject(Object json) { |
| 109 | + return JSON.parseObject(toJSONString(json), DEFAULT_FASTJSON_FEATURES); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public <T> T parseObject(Object json, Class<T> clazz) { |
| 114 | + return JSON.parseObject(toJSONString(json), clazz, DEFAULT_FASTJSON_FEATURES); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public JSONArray parseArray(Object json) { |
| 119 | + return JSON.parseArray(toJSONString(json)); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public <T> List<T> parseArray(Object json, Class<T> clazz) { |
| 124 | + return JSON.parseArray(toJSONString(json), clazz); |
| 125 | + } |
| 126 | + |
| 127 | + }; |
| 128 | + |
76 | 129 | // 使用本项目的自定义处理类 |
77 | | - APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() { |
| 130 | + APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long, JSONObject, JSONArray>() { |
| 131 | + |
| 132 | + @Override |
| 133 | + public DemoParser createParser() { |
| 134 | + return new DemoParser(); |
| 135 | + } |
| 136 | + |
78 | 137 | @Override |
79 | | - public SQLConfig createSQLConfig() { |
| 138 | + public DemoFunctionParser createFunctionParser() { |
| 139 | + return new DemoFunctionParser(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public DemoVerifier createVerifier() { |
| 144 | + return new DemoVerifier(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public DemoSQLConfig createSQLConfig() { |
80 | 149 | return new DemoSQLConfig(); |
81 | 150 | } |
| 151 | + |
| 152 | + @Override |
| 153 | + public DemoSQLExecutor createSQLExecutor() { |
| 154 | + return new DemoSQLExecutor(); |
| 155 | + } |
| 156 | + |
82 | 157 | }; |
83 | 158 |
|
84 | 159 | // 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增 |
|
0 commit comments