|
| 1 | +/*Copyright ©2025 APIJSON(https://github.com/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.fastjson2; |
| 16 | + |
| 17 | +import apijson.NotNull; |
| 18 | +import apijson.framework.APIJSONCreator; |
| 19 | +import com.alibaba.fastjson2.JSONArray; |
| 20 | +import com.alibaba.fastjson2.JSONObject; |
| 21 | +import com.alibaba.fastjson2.JSONWriter; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +/**启动入口 Application |
| 27 | + * 调用 APIJSONApplication.init |
| 28 | + * @author Lemon |
| 29 | + */ |
| 30 | +public class APIJSONApplication extends apijson.framework.APIJSONApplication { |
| 31 | + |
| 32 | + //static { |
| 33 | + // apijson.JSON.DEFAULT_JSON_PARSER = JSON.DEFAULT_JSON_PARSER; // 解决 DEFAULT_JSON_PARSER 初始化前就自测导致抛异常 |
| 34 | + //} |
| 35 | + |
| 36 | + static { |
| 37 | + JSON.DEFAULT_JSON_PARSER = new JSONParser() { |
| 38 | + @Override |
| 39 | + public JSONObject createJSONObject() { |
| 40 | + return JSONParser.super.createJSONObject(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public JSONArray createJSONArray() { |
| 45 | + return JSONParser.super.createJSONArray(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String toJSONString(Object obj, boolean format) { |
| 50 | + if (obj instanceof String) { |
| 51 | + if (! format) { |
| 52 | + return (String) obj; |
| 53 | + } |
| 54 | + |
| 55 | + obj = com.alibaba.fastjson2.JSON.parseObject((String) obj); |
| 56 | + } |
| 57 | + |
| 58 | + if (format) { |
| 59 | + return com.alibaba.fastjson2.JSON.toJSONString(obj, JSONWriter.Feature.PrettyFormat); |
| 60 | + } |
| 61 | + |
| 62 | + return com.alibaba.fastjson2.JSON.toJSONString(obj); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Object parse(Object json) { |
| 67 | + return com.alibaba.fastjson2.JSON.parse(toJSONString(json)); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public JSONObject parseObject(Object json) { |
| 72 | + return com.alibaba.fastjson2.JSON.parseObject(toJSONString(json)); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public <T> T parseObject(Object json, Class<T> clazz) { |
| 77 | + return com.alibaba.fastjson2.JSON.parseObject(toJSONString(json), clazz); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public JSONArray parseArray(Object json) { |
| 82 | + return com.alibaba.fastjson2.JSON.parseArray(toJSONString(json)); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public <T> List<T> parseArray(Object json, Class<T> clazz) { |
| 87 | + return com.alibaba.fastjson2.JSON.parseArray(toJSONString(json), clazz); |
| 88 | + } |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + //public static <T> APIJSONParser<T> createParser() { |
| 94 | + // return (APIJSONParser<T>) DEFAULT_APIJSON_CREATOR.createParser(); |
| 95 | + //} |
| 96 | + |
| 97 | + /**初始化,加载所有配置并校验 |
| 98 | + * @return |
| 99 | + * @throws Exception |
| 100 | + */ |
| 101 | + public static void init() throws Exception { |
| 102 | + init(true, DEFAULT_APIJSON_CREATOR); |
| 103 | + } |
| 104 | + /**初始化,加载所有配置并校验 |
| 105 | + * @param shutdownWhenServerError |
| 106 | + * @return |
| 107 | + * @throws Exception |
| 108 | + */ |
| 109 | + public static void init(boolean shutdownWhenServerError) throws Exception { |
| 110 | + init(shutdownWhenServerError, DEFAULT_APIJSON_CREATOR); |
| 111 | + } |
| 112 | + /**初始化,加载所有配置并校验 |
| 113 | + * @param creator |
| 114 | + * @return |
| 115 | + * @throws Exception |
| 116 | + */ |
| 117 | + public static <T, M extends Map<String, Object>, L extends List<Object>> void init( |
| 118 | + @NotNull apijson.framework.APIJSONCreator<T, M, L> creator) throws Exception { |
| 119 | + init(true, creator); |
| 120 | + } |
| 121 | + /**初始化,加载所有配置并校验 |
| 122 | + * @param shutdownWhenServerError |
| 123 | + * @param creator |
| 124 | + * @return |
| 125 | + * @throws Exception |
| 126 | + */ |
| 127 | + public static <T, M extends Map<String, Object>, L extends List<Object>> void init( |
| 128 | + boolean shutdownWhenServerError, @NotNull APIJSONCreator<T, M, L> creator) throws Exception { |
| 129 | + apijson.framework.APIJSONApplication.init(shutdownWhenServerError, creator); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments