|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon) |
| 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.demo.ui; |
| 16 | + |
| 17 | +import static zuo.biao.apijson.StringUtil.bigAlphaPattern; |
| 18 | + |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import zuo.biao.apijson.JSON; |
| 22 | +import zuo.biao.apijson.JSONRequest; |
| 23 | +import zuo.biao.apijson.JSONResponse; |
| 24 | +import zuo.biao.apijson.Log; |
| 25 | +import android.app.Activity; |
| 26 | +import android.content.Context; |
| 27 | +import android.content.Intent; |
| 28 | +import android.os.Bundle; |
| 29 | +import android.view.View; |
| 30 | +import android.widget.EditText; |
| 31 | +import apijson.demo.R; |
| 32 | +import apijson.demo.StringUtil; |
| 33 | +import apijson.demo.model.User; |
| 34 | + |
| 35 | +import com.alibaba.fastjson.JSONObject; |
| 36 | + |
| 37 | +/**自动生成代码 |
| 38 | + * @author Lemon |
| 39 | + */ |
| 40 | +public class AutoActivity extends Activity { |
| 41 | + private static final String TAG = "AutoActivity"; |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * @param context |
| 46 | + * @return |
| 47 | + */ |
| 48 | + public static Intent createIntent(Context context) { |
| 49 | + return new Intent(context, AutoActivity.class); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + EditText tvAutoResult; |
| 55 | + @Override |
| 56 | + protected void onCreate(Bundle savedInstanceState) { |
| 57 | + super.onCreate(savedInstanceState); |
| 58 | + setContentView(R.layout.auto_activity); |
| 59 | + |
| 60 | + |
| 61 | + tvAutoResult = (EditText) findViewById(R.id.tvAutoResult); |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + String request = "{\"User\":{\"id\":1,\"@column\":\"id,name\"},\"Moment\":{\"userId@\":\"/User/id\"}}"; |
| 66 | + |
| 67 | + tvAutoResult.setText(StringUtil.getString(JSON.format(request))); |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + public void auto(View v) { |
| 76 | + auto(StringUtil.getString(tvAutoResult)); |
| 77 | + } |
| 78 | + public void auto(String request) { |
| 79 | + String response = parse("", JSON.parseObject(request)); //newObjectRequest(request); |
| 80 | + |
| 81 | + Log.d(TAG, "\n request = \n" + request); |
| 82 | + Log.d(TAG, "\n response = \n" + response); |
| 83 | + |
| 84 | + tvAutoResult.setText(StringUtil.getString(response)); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + public static final String NEWLINE = "\n"; |
| 89 | + |
| 90 | + private String newObjectRequest(String json) { |
| 91 | + JSONRequest request = new JSONRequest(); |
| 92 | + |
| 93 | + |
| 94 | + //单个JSONObject<<<<<<<<<<<<<<<<< |
| 95 | + JSONRequest userRequest = new JSONRequest(); |
| 96 | + |
| 97 | + User user = new User(); |
| 98 | + user.setId((long) 1); |
| 99 | + |
| 100 | + userRequest.add(JSON.parseObject(user)); |
| 101 | + |
| 102 | + userRequest.setColumn("id,name"); |
| 103 | + //单个JSONObject>>>>>>>>>>>>>>>>>> |
| 104 | + |
| 105 | + |
| 106 | + request.put("User", userRequest); |
| 107 | + |
| 108 | + |
| 109 | + return JSON.format(request); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + public static String parse(final String name, final JSONObject request) { |
| 114 | + Log.i(TAG, "parse request = \n" + JSON.toJSONString(request)); |
| 115 | + if (request == null || request.isEmpty()) { |
| 116 | + Log.i(TAG, "parse request == null || request.isEmpty() >> return request;"); |
| 117 | + return null; |
| 118 | + } |
| 119 | + String parentKey = getTableKey(name); |
| 120 | + |
| 121 | + |
| 122 | + String response = NEWLINE + "JSONRequest " + parentKey + " = new JSONRequest();"; |
| 123 | + |
| 124 | + Set<String> set = request.keySet(); |
| 125 | + if (set != null) { |
| 126 | + |
| 127 | + Object value; |
| 128 | + String pairKey; |
| 129 | + String arrayKey; |
| 130 | + for (String key : set) { |
| 131 | + value = request.get(key); |
| 132 | + pairKey = new String(key instanceof String ? "\"" + key + "\"" : key); |
| 133 | + if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray |
| 134 | + if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray |
| 135 | + // arrayKey = key.substring(0, key.indexOf(JSONResponse.KEY_ARRAY)); |
| 136 | + } else {//常规JSONObject,往下一级提取 |
| 137 | + |
| 138 | + response += NEWLINE + parse(key, (JSONObject) value); |
| 139 | + |
| 140 | + response += NEWLINE + NEWLINE + parentKey + ".put(" + pairKey + ", " + getTableKey(key) + ");" + NEWLINE; |
| 141 | + } |
| 142 | + } else {//其它Object,直接填充 |
| 143 | + value = value instanceof String ? new String("\"" + value + "\"") : value; |
| 144 | + |
| 145 | + response += NEWLINE + parentKey + ".put(" + pairKey + ", " + value + ");"; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + Log.i(TAG, "parse return response = \n" + response); |
| 151 | + return response; |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + /**获取Table变量名 |
| 159 | + * @param key => StringUtil.getNoBlankString(key) |
| 160 | + * @return empty ? "list" : key + "List" 且首字母小写 |
| 161 | + */ |
| 162 | + private static String getTableKey(String key) { |
| 163 | + key = StringUtil.getNoBlankString(key); |
| 164 | + if (key.isEmpty()) { |
| 165 | + return "request"; |
| 166 | + } |
| 167 | + |
| 168 | + String first = key.substring(0, 1); |
| 169 | + if (bigAlphaPattern.matcher(first).matches()) { |
| 170 | + key = first.toLowerCase() + key.substring(1, key.length()); |
| 171 | + } |
| 172 | + return key + "Request"; |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + private static String getSimpleName(String arrayKey) { |
| 177 | + return JSONResponse.getSimpleName(arrayKey); |
| 178 | + } |
| 179 | + |
| 180 | + private static boolean isArrayKey(String key) { |
| 181 | + return JSONResponse.isArrayKey(key); |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | +} |
0 commit comments