diff --git a/.github/ISSUE_TEMPLATE/--bug.md b/.github/ISSUE_TEMPLATE/--bug.md
deleted file mode 100755
index 3683b0f3e..000000000
--- a/.github/ISSUE_TEMPLATE/--bug.md
+++ /dev/null
@@ -1,40 +0,0 @@
----
-name: 提交问题
-about: Create a report to help us improve
-
----
-
-**提 bug 请发请求和响应的【完整截屏】,没图的自行解决!
-开发者有限的时间和精力主要放在【维护项目源码和文档】上!
-【描述不详细】 或 【文档/常见问题 已有答案】 的问题可能会被忽略!!
-【态度 不文明/不友善】的可能会被拉黑,问题也可能不予解答!!!**
-
-请求参数 JSON 中表名、字段名、关键词及对应的值都是大小写敏感、逗号敏感、分号敏感、空格敏感、换行敏感,
-大部分情况都不允许空格和换行,表名以大写字母开头,不要想当然,请严格按照 设计规范 来调用 API #181
-
-1.尝试在 [常见问题](https://github.com/Tencent/APIJSON/issues/36) 和 [历史问题](https://github.com/TommyLemon/APIJSON/issues?q=is%3Aissue) 搜索答案。
-2.尝试阅读 [通用文档](https://github.com/TommyLemon/APIJSON/blob/master/Document.md) 或看 [视频教程](https://search.bilibili.com/all?keyword=APIJSON) 找到答案。
-3.尝试阅读 [Demo 示例代码](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLConfig.java) 以找到答案。
-4.尝试自己 [检查或试验](http://apijson.cn/api) 以找到答案。
-5.尝试阅读 [源码和注释](https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java) 以找到答案。
-
-如果以上都尝试过了请填写以下模板提一个新的issue。
-强烈推荐阅读 [《如何向开源社区提问题》](https://github.com/seajs/seajs/issues/545)、[《如何有效地报告 Bug》](http://www.chiark.greenend.org.uk/~sgtatham/bugs-cn.html)、[《如何向开源项目提交无法解答的问题》](https://zhuanlan.zhihu.com/p/25795393)
-和 [《提问的智慧》](https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way),更好的问题更容易获得帮助。
-
-
-
-**环境信息**
- - 系统:
- - JDK:
- - 数据库:
- - APIJSON:
-
-**问题描述**
-
-可以输入具体的步骤,代码信息,或者截图,能帮助我们更快的解决您的问题
-
-**错误信息**
-
-运行日志面板错误信息,错误截图,或者【帮助>切换开发者工具】日志,以及【帮助>查看运行日志】log.log 文件
-
diff --git a/.github/ISSUE_TEMPLATE/--custom.md b/.github/ISSUE_TEMPLATE/--custom.md
deleted file mode 100755
index 5a47bb25e..000000000
--- a/.github/ISSUE_TEMPLATE/--custom.md
+++ /dev/null
@@ -1,7 +0,0 @@
----
-name: 其它反馈
-about: Describe this issue template's purpose here.
-
----
-
-
diff --git a/.github/ISSUE_TEMPLATE/--feature_request.md b/.github/ISSUE_TEMPLATE/--feature_request.md
deleted file mode 100755
index f8c0c4c7d..000000000
--- a/.github/ISSUE_TEMPLATE/--feature_request.md
+++ /dev/null
@@ -1,11 +0,0 @@
----
-name: 功能改进
-about: Suggest an idea for this project
-
----
-
-**具体说下**
-希望改进...希望做成...希望新增...
-
-**为什么**
-说明它在哪些情况下会带来哪些效果。
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
new file mode 100644
index 000000000..f0d9515f5
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug_report.yml
@@ -0,0 +1,83 @@
+name: Bug Report/报告 bug
+description: "Create a report to help us improve, please read FAQ first./帮助我们更好地改进项目,但请先阅读常见问题与提问前必看,不要提已有的重复问题!"
+title: "[Bug] "
+labels: [kind/bug]
+body:
+- type: markdown
+ attributes:
+ value: "如果你已经知道问题所在、怎么解决,请直接 提交 Pull Request 为社区做贡献,非常感谢。\n开发者也是人,也需要工作、休息、恋爱、陪伴家人、走亲会友等,也有心情不好和身体病痛,\n往往没有额外的时间精力顾及一些小问题,请理解和支持,开源要大家参与贡献才会更美好~\n少数个人的热情终有被耗尽的一天,只有大家共同建设和繁荣社区,才能让开源可持续发展! "
+
+- type: input
+ attributes:
+ label: APIJSON Version/APIJSON 版本号
+ placeholder: |
+ e.g./例如 5.4.0 ,如果不是最新版请用最新版,复现问题再来,原则上不更新旧版,而是只维护一个最新版
+
+ validations:
+ required: true
+
+- type: input
+ attributes:
+ label: Database Type & Version/数据库类型及版本号
+ placeholder: |
+ e.g./例如 MySQL 5.7.34
+
+ validations:
+ required: true
+
+- type: textarea
+ attributes:
+ label: Environment/环境信息
+ description: |
+ e.g./例如:
+ - **JDK/基础库**: 1.8.0_17
+ - **OS/系统**: MacOS Monterey 12.6 (21G115) M1
+ value: |
+ - JDK/基础库:
+ - OS/系统:
+ render: markdown
+
+ validations:
+ required: true
+
+- type: input
+ attributes:
+ label: APIAuto Screenshots/APIAuto 请求与结果完整截屏
+ description: "Upload by copy and paste image file or url./复制图片文件或 URL 再粘贴到输入框(用 APIAuto 能静态检查出很多问题,甚至还有修复建议,不用浪费你我的时间)\n https://github.com/TommyLemon/APIAuto "
+ value:
+
+ validations:
+ required: true
+
+- type: textarea
+ attributes:
+ label: Current Behavior/问题描述
+ description: "A concise description of what you're experiencing. Must contains screenshots./\n\n**提 bug 请发请求和响应的【完整截屏】,没图的自行解决!\n开发者有限的时间和精力主要放在【维护项目源码和文档】上!\n【描述不详细】 或 【文档/常见问题 已有答案】 的问题可能会被忽略!!\n【态度 不文明/不友善】的可能会被拉黑,问题也可能不予解答!!!**\n\n请求参数 JSON 中表名、字段名、关键词及对应的值都是大小写敏感、逗号敏感、分号敏感、空格敏感、换行敏感,\n大部分情况都不允许空格和换行,表名以大写字母开头,不要想当然,请严格按照 设计规范 来调用 API \n https://github.com/Tencent/APIJSON/issues/181 "
+ render: markdown
+
+ validations:
+ required: true
+
+- type: textarea
+ attributes:
+ label: Expected Behavior/期望结果
+ description: A concise description of what you expected to happen./具体描述你期望返回什么样的结果或者达到什么样的效果?
+ render: markdown
+
+ validations:
+ required: false
+
+
+- type: textarea
+ attributes:
+ label: Any additional comments?/其它补充说明?
+ description: |
+ e.g. some background/context of how you ran into this bug./例如:一些背景或上下文信息,包括复现步骤、相关日志等
+ render: markdown
+
+ validations:
+ required: false
+
+- type: markdown
+ attributes:
+ value: "Please follow the rules to fulfil all required inputs. You can add screenshots by comment after submit this issue./\n请按要求填写所有必填项,未填完将提交不了!\n如果随意填写敷衍了事,将直接关闭 issue,问题不会得到解答!\n可以提交后再通过回复评论来补充上传截屏图片(复制粘贴文件)。\n如果是网页 bug 等与你无关的原因导致提交不了,可以改为填问卷:\n https://wj.qq.com/s2/10971431/2a09 "
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
new file mode 100644
index 000000000..3ba13e0ce
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -0,0 +1 @@
+blank_issues_enabled: false
diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml
new file mode 100644
index 000000000..6f14d47b3
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/feature_request.yml
@@ -0,0 +1,17 @@
+name: Feature Request/期望新增功能
+description: Request a new feature/期望新增什么样的功能或特性,或者做哪些方面的改进?
+title: "[Feature] "
+labels: [kind/feature]
+body:
+- type: textarea
+ attributes:
+ label: Description
+ description: |
+ Please describe what this feature does./具体描述下是什么样的功能或特性,以及你为什么想要它,用在什么场景,碰到了什么痛点,有什么解决思路,尝试过哪些,效果怎样?
+
+ validations:
+ required: true
+
+- type: markdown
+ attributes:
+ value: 推荐去建议收集箱提问,也方便 统一检索和管理、投票决定优先级、更新处理进度 等: https://github.com/Tencent/APIJSON/issues/37
diff --git a/.github/ISSUE_TEMPLATE/other_issues.yml b/.github/ISSUE_TEMPLATE/other_issues.yml
new file mode 100644
index 000000000..9f9a6d0ea
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/other_issues.yml
@@ -0,0 +1,16 @@
+name: Other Issues/其它反馈
+description: For questions, suggestions, improvements and others./问题(非 bug)、建议(非新增功能) 或 其它
+title: "[xxx] "
+body:
+- type: textarea
+ attributes:
+ label: Description
+ description: |
+ Please describe the issue./请具体描述,包括是什么、为什么、如何做
+
+ validations:
+ required: true
+
+- type: markdown
+ attributes:
+ value: "Bug 反馈请使用正确的模板,用错模板将直接关闭 issue,不予解答:\n https://github.com/Tencent/APIJSON/issues/new?assignees=&labels=kind%2Fbug&template=bug_report.yml&title=%5BBug%5D+ \n有建议请去建议收集箱提问,也方便 统一检索和管理、投票决定优先级、更新处理进度 等:\n https://github.com/Tencent/APIJSON/issues/37 "
diff --git a/.gitignore b/.gitignore
index 204504dc5..a06bbd0a8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,3 +34,5 @@ build/
### VS Code ###
.vscode/
+APIJSONORM/bin
+*.DS_Store
diff --git a/APIJSONORM/README.md b/APIJSONORM/README.md
index 7e0850527..0cb431e27 100644
--- a/APIJSONORM/README.md
+++ b/APIJSONORM/README.md
@@ -1,4 +1,4 @@
-# APIJSONORM [](https://jitpack.io/#Tencent/APIJSON)
+# APIJSONORM [](https://jitpack.io/#Tencent/APIJSON) [Ask DeepWiki.com](https://deepwiki.com/Tencent/APIJSON)
腾讯 [APIJSON](https://github.com/Tencent/APIJSON) ORM 库,可通过 Maven, Gradle 等远程依赖。
Tencent [APIJSON](https://github.com/Tencent/APIJSON) ORM library for remote dependencies with Maven, Gradle, etc.
@@ -25,7 +25,6 @@ Tencent [APIJSON](https://github.com/Tencent/APIJSON) ORM library for remote dep
```
-
@@ -49,3 +48,18 @@ Tencent [APIJSON](https://github.com/Tencent/APIJSON) ORM library for remote dep
implementation 'com.github.Tencent:APIJSON:latest'
}
```
+
+
+
+
+### FASTJSON 2
+#### Code
+https://github.com/Tencent/APIJSON/tree/fastjson2
+
+#### Maven
+https://mvnrepository.com/artifact/com.github.linushp/zikai-apijson/1.0
+
+
+
+### Unit Test
+http://apijson.cn/unit
diff --git a/APIJSONORM/pom.xml b/APIJSONORM/pom.xml
old mode 100755
new mode 100644
index c1bdf23dc..8b36d0da4
--- a/APIJSONORM/pom.xml
+++ b/APIJSONORM/pom.xml
@@ -3,9 +3,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
- apijson.orm
- apijson-orm
- 5.1.0
+ com.github.Tencent
+ APIJSON
+ 8.1.3
jar
APIJSONORM
@@ -15,19 +15,12 @@
UTF-8
UTF-8
1.8
+ UTF-8
+ 1.8
+ 1.8
-
- com.alibaba
- fastjson
- 1.2.79
-
-
- javax.activation
- activation
- 1.1.1
-
@@ -35,11 +28,26 @@
org.apache.maven.plugins
maven-compiler-plugin
+ 3.12.1
1.8
1.8
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
+ 3.2.1
+
+
+ package
+
+ jar-no-fork
+
+
+
+
diff --git a/APIJSONORM/src/main/java/apijson/JSON.java b/APIJSONORM/src/main/java/apijson/JSON.java
index 28c124cab..0a14e0420 100755
--- a/APIJSONORM/src/main/java/apijson/JSON.java
+++ b/APIJSONORM/src/main/java/apijson/JSON.java
@@ -1,288 +1,691 @@
-/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
+/*Copyright (C) 2020 Tencent. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson;
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.parser.Feature;
-import com.alibaba.fastjson.serializer.SerializerFeature;
-
+import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
-/**阿里FastJSON封装类 防止解析时异常
+/**JSON工具类 防止解析时异常
* @author Lemon
*/
public class JSON {
- private static final String TAG = "JSON";
- /**判断json格式是否正确
- * @param s
- * @return
- */
- public static boolean isJsonCorrect(String s) {
- //太长 Log.i(TAG, "isJsonCorrect <<<< " + s + ">>>>>>>");
- if (s == null
- // || s.equals("[]")
- // || s.equals("{}")
- || s.equals("")
- || s.equals("[null]")
- || s.equals("{null}")
- || s.equals("null")) {
- return false;
- }
- return true;
+ static final String TAG = "JSON";
+
+ public static JSONParser extends Map, ? extends List> DEFAULT_JSON_PARSER;
+
+ static {
+ //DEFAULT_JSON_PARSER = new JSONParser, List>() {
+ //
+ // @Override
+ // public LinkedHashMap createJSONObject() {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ // @Override
+ // public List createJSONArray() {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ // @Override
+ // public String toJSONString(Object obj, boolean format) {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ // @Override
+ // public Object parse(Object json) {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ // @Override
+ // public LinkedHashMap parseObject(Object json) {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ // @Override
+ // public T parseObject(Object json, Class clazz) {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ // @Override
+ // public List parseArray(Object json) {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ // @Override
+ // public List parseArray(Object json, Class clazz) {
+ // throw new UnsupportedOperationException();
+ // }
+ //
+ //};
+
}
- /**获取有效的json
- * @param s
- * @return
- */
- public static String getCorrectJson(String s) {
- return getCorrectJson(s, false);
+// public static JSONCreator extends Map, ? extends List> DEFAULT_JSON_CREATOR = DEFAULT_JSON_PARSER;
+// public static > M newObj() {
+// return createJSONObject();
+// }
+// public static > M newObj(String key, Object value) {
+// return createJSONObject(key, value);
+// }
+// public static > M newObj(Map extends String, ?> map) {
+// return createJSONObject(map);
+// }
+
+ public static > M createJSONObject() {
+ return (M) DEFAULT_JSON_PARSER.createJSONObject();
}
- /**获取有效的json
- * @param s
- * @param isArray
- * @return
- */
- public static String getCorrectJson(String s, boolean isArray) {
- s = StringUtil.getTrimedString(s);
- // if (isArray) {
- // while (s.startsWith("\"")) {
- // s = s.substring(1);
- // }
- // while (s.endsWith("\"")) {
- // s = s.substring(0, s.length() - 1);
- // }
- // }
- return s;//isJsonCorrect(s) ? s : null;
+ public static > M createJSONObject(String key, Object value) {
+ return (M) DEFAULT_JSON_PARSER.createJSONObject(key, value);
+ }
+ public static > M createJSONObject(Map extends String, ?> map) {
+ return (M) DEFAULT_JSON_PARSER.createJSONObject(map);
+ }
+
+ //public static L newArr() {
+ // return createJSONArray();
+ //}
+ //public static L newArr(Object obj) {
+ // return createJSONArray(obj);
+ //}
+ //public static L newArr(List> list) {
+ // return createJSONArray(list);
+ //}
+
+ public static L createJSONArray() {
+ return (L) DEFAULT_JSON_PARSER.createJSONArray();
+ }
+ public static L createJSONArray(Object obj) {
+ return (L) DEFAULT_JSON_PARSER.createJSONArray(obj);
+ }
+ public static L createJSONArray(Collection> list) {
+ return (L) DEFAULT_JSON_PARSER.createJSONArray(list);
+ }
+
+ public static Object parse(Object json) {
+ return DEFAULT_JSON_PARSER.parse(json);
+ }
+
+
+ public static > M parseObject(Object json) {
+ String s = toJSONString(json);
+ if (StringUtil.isEmpty(s, true)) {
+ return null;
+ }
+
+ return (M) DEFAULT_JSON_PARSER.parseObject(s);
+ }
+
+ public static T parseObject(Object json, Class clazz) {
+ String s = toJSONString(json);
+ if (StringUtil.isEmpty(s, true)) {
+ return null;
+ }
+
+ return DEFAULT_JSON_PARSER.parseObject(s, clazz);
}
/**
* @param json
* @return
*/
- public static Object parse(Object obj) {
- int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
- features |= Feature.OrderedField.getMask();
+ public static L parseArray(Object json) {
+ String s = toJSONString(json);
+ if (StringUtil.isEmpty(s, true)) {
+ return null;
+ }
+
try {
- return com.alibaba.fastjson.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), features);
+ L arr = (L) DEFAULT_JSON_PARSER.parseArray(s);
+ return arr;
} catch (Exception e) {
- Log.i(TAG, "parse catch \n" + e.getMessage());
+ Log.i(TAG, "parseArray catch \n" + e.getMessage());
}
return null;
}
- /**obj转JSONObject
- * @param json
- * @return
- */
- public static JSONObject parseObject(Object obj) {
- if (obj instanceof JSONObject) {
- return (JSONObject) obj;
+
+ public static List parseArray(Object json, Class clazz) {
+ String s = toJSONString(json);
+ if (StringUtil.isEmpty(s, true)) {
+ return null;
}
- return parseObject(toJSONString(obj));
- }
- /**json转JSONObject
- * @param json
- * @return
- */
- public static JSONObject parseObject(String json) {
- int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
- features |= Feature.OrderedField.getMask();
- return parseObject(json, features);
- }
- /**json转JSONObject
- * @param json
- * @param features
- * @return
- */
- public static JSONObject parseObject(String json, int features) {
+
try {
- return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
+ return DEFAULT_JSON_PARSER.parseArray(s, clazz);
} catch (Exception e) {
- Log.i(TAG, "parseObject catch \n" + e.getMessage());
+ Log.i(TAG, "parseArray catch \n" + e.getMessage());
}
return null;
}
- /**JSONObject转实体类
- * @param object
- * @param clazz
+ /**
+ * @param obj
* @return
*/
- public static T parseObject(JSONObject object, Class clazz) {
- return parseObject(toJSONString(object), clazz);
+ public static String format(Object obj) {
+ return toJSONString(obj, true);
}
- /**json转实体类
- * @param json
- * @param clazz
+ /**
+ * @param obj
* @return
*/
- public static T parseObject(String json, Class clazz) {
- if (clazz == null) {
- Log.e(TAG, "parseObject clazz == null>> return null;");
- } else {
- try {
- int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
- features |= Feature.OrderedField.getMask();
- return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
- } catch (Exception e) {
- Log.i(TAG, "parseObject catch \n" + e.getMessage());
- }
+ public static String toJSONString(Object obj) {
+ return toJSONString(obj, false);
+ }
+ public static String toJSONString(Object obj, boolean format) {
+ if (obj == null) {
+ return null;
}
- return null;
+
+ if (obj instanceof String) {
+ return (String) obj;
+ }
+
+ //if (obj instanceof Map) {
+ // // Simple JSON object format
+ // StringBuilder sb = new StringBuilder("{");
+ // @SuppressWarnings("unchecked")
+ // Map map = (Map) obj;
+ // boolean first = true;
+ // for (Map.Entry entry : map.entrySet()) {
+ // if (! first) {
+ // sb.append(",");
+ // }
+ //
+ // first = false;
+ // sb.append("\"").append(entry.getKey()).append("\":");
+ // Object value = entry.getValue();
+ // if (value instanceof String) {
+ // sb.append("\"").append(value).append("\"");
+ // } else {
+ // sb.append(toJSONString(value));
+ // }
+ // }
+ // sb.append("}");
+ // return sb.toString();
+ //}
+ //
+ //if (obj instanceof List) {
+ // StringBuilder sb = new StringBuilder("[");
+ // @SuppressWarnings("unchecked")
+ // List list = (List) obj;
+ // boolean first = true;
+ // for (Object item : list) {
+ // if (! first) {
+ // sb.append(",");
+ // }
+ // first = false;
+ // if (item instanceof String) {
+ // sb.append("\"").append(item).append("\"");
+ // } else {
+ // sb.append(toJSONString(item));
+ // }
+ // }
+ // sb.append("]");
+ // return sb.toString();
+ //}
+
+ return DEFAULT_JSON_PARSER.toJSONString(obj, format);
}
- /**list转JSONArray
- * @param list
+
+ /**判断是否为JSONObject或JSONArray的isXxx方法名
+ * @param key
* @return
*/
- public static JSONArray parseArray(List list) {
- return new JSONArray(list);
+ public static boolean isJSONType(String key) {
+ return key != null && key.startsWith("is") && key.length()> 2 && key.contains("JSON");
}
- /**obj转JSONArray
- * @param obj
- * @return
+
+ public static boolean isBoolOrNumOrStr(Object obj) {
+ return obj instanceof Boolean || obj instanceof Number || obj instanceof String;
+ }
+
+ /**
+ * Get a value from a Map and convert to the specified type
+ * @param map Source map
+ * @param key The key
+ * @param Target type
+ * @return The converted value
+ */
+ @SuppressWarnings("unchecked")
+ public static T get(Map map, String key) {
+ return map == null || key == null ? null : (T) map.get(key);
+ }
+
+ /**
+ * Get a value from a Map and convert to the specified type
+ * @param map Source map
+ * @param key The key
+ * @param Target type
+ * @return The converted value
+ */
+ @SuppressWarnings("unchecked")
+ public static > M getJSONObject(Map map, String key) {
+ Object obj = get(map, key);
+ return (M) obj;
+ }
+
+ /**
+ * Get a value from a Map and convert to the specified type
+ * @param map Source map
+ * @param key The key
+ * @param Target type
+ * @return The converted value
*/
- public static JSONArray parseArray(Object obj) {
- if (obj instanceof JSONArray) {
- return (JSONArray) obj;
+ @SuppressWarnings("unchecked")
+ public static L getJSONArray(Map map, String key) {
+ Object obj = get(map, key);
+ return (L) obj;
+ }
+
+ /**
+ * Get a value from a Map and convert to the specified type
+ * @param list Source map
+ * @param index The key
+ * @param Target type
+ * @return The converted value
+ */
+ @SuppressWarnings("unchecked")
+ public static T get(List list, int index) {
+ return list == null || index < 0 || index>= list.size() ? null : (T) list.get(index);
+ }
+
+ @SuppressWarnings("unchecked")
+ public static > M getJSONObject(List list, int index) {
+ Object obj = get(list, index);
+ return (M) obj;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static L getJSONArray(List list, int index) {
+ Object obj = get(list, index);
+ return (L) obj;
+ }
+
+// /**
+// * Get a value from a Map and convert to the specified type
+// * @param map Source map
+// * @param key The key
+// * @param Target type
+// * @return The converted value
+// */
+// @SuppressWarnings("unchecked")
+// public static T get(List list, int index) {
+// return list == null || index < 0 || index>= list.size() ? null : list.get(index);
+// }
+
+ /**
+ * Get a Map value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The Map value
+ * @throws IllegalArgumentException If value is not a Map and cannot be converted
+ */
+ @SuppressWarnings("unchecked")
+ public static Map getMap(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
+ }
+
+ if (value instanceof Map) {
+ return (Map) value;
}
- return parseArray(toJSONString(obj));
+
+ throw new IllegalArgumentException("Value for key '" + key + "' is not a Map: " + value.getClass().getName());
}
- /**json转JSONArray
- * @param json
- * @return
+
+ /**
+ * Get a List value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The List value
+ * @throws IllegalArgumentException If value is not a List and cannot be converted
*/
- public static JSONArray parseArray(String json) {
- try {
- return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true));
- } catch (Exception e) {
- Log.i(TAG, "parseArray catch \n" + e.getMessage());
+ @SuppressWarnings("unchecked")
+ public static List getList(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
}
- return null;
+
+ if (value instanceof List) {
+ return (List) value;
+ }
+
+ throw new IllegalArgumentException("Value for key '" + key + "' is not a List: " + value.getClass().getName());
}
- /**JSONArray转实体类列表
- * @param array
- * @param clazz
- * @return
+
+ /**
+ * Get an int value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The int value
+ * @throws IllegalArgumentException If value cannot be converted to int
*/
- public static List parseArray(JSONArray array, Class clazz) {
- return parseArray(toJSONString(array), clazz);
+ public static Integer getInteger(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
+ }
+
+ if (value instanceof Number) {
+ return ((Number) value).intValue();
+ }
+
+ if (value instanceof String) {
+ try {
+ return Integer.parseInt((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to int: " + e.getMessage());
+ }
+ }
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to int");
}
- /**json转实体类列表
- * @param json
- * @param clazz
- * @return
+
+ /**
+ * Get an int value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The int value
+ * @throws IllegalArgumentException If value cannot be converted to int
*/
- public static List parseArray(String json, Class clazz) {
- if (clazz == null) {
- Log.e(TAG, "parseArray clazz == null>> return null;");
- } else {
+ public static int getIntValue(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return 0;
+ }
+
+ if (value instanceof Number) {
+ return ((Number) value).intValue();
+ }
+
+ if (value instanceof String) {
try {
- return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz);
- } catch (Exception e) {
- Log.i(TAG, "parseArray catch \n" + e.getMessage());
+ return Integer.parseInt((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to int: " + e.getMessage());
}
}
- return null;
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to int");
}
- /**实体类转json
- * @param obj
- * @return
+ /**
+ * Get an int value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The int value
+ * @throws IllegalArgumentException If value cannot be converted to int
*/
- public static String toJSONString(Object obj) {
- if (obj instanceof String) {
- return (String) obj;
+ public static Long getLong(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
}
- try {
- return com.alibaba.fastjson.JSON.toJSONString(obj);
- } catch (Exception e) {
- Log.e(TAG, "toJSONString catch \n" + e.getMessage());
+
+ if (value instanceof Number) {
+ return ((Number) value).longValue();
}
- return null;
+
+ if (value instanceof String) {
+ try {
+ return Long.parseLong((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to int: " + e.getMessage());
+ }
+ }
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to int");
}
- /**实体类转json
- * @param obj
- * @param features
- * @return
+ /**
+ * Get a long value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The long value
+ * @throws IllegalArgumentException If value cannot be converted to long
*/
- public static String toJSONString(Object obj, SerializerFeature... features) {
- if (obj instanceof String) {
- return (String) obj;
+ public static long getLongValue(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return 0;
}
- try {
- return com.alibaba.fastjson.JSON.toJSONString(obj, features);
- } catch (Exception e) {
- Log.e(TAG, "parseArray catch \n" + e.getMessage());
+
+ if (value instanceof Number) {
+ return ((Number) value).longValue();
}
- return null;
+
+ if (value instanceof String) {
+ try {
+ return Long.parseLong((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to long: " + e.getMessage());
+ }
+ }
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to long");
}
- /**格式化,显示更好看
- * @param json
- * @return
+ /**
+ * Get a double value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The double value
+ * @throws IllegalArgumentException If value cannot be converted to double
*/
- public static String format(String json) {
- return format(parse(json));
+ public static Float getFloat(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
+ }
+
+ if (value instanceof Number) {
+ return ((Number) value).floatValue();
+ }
+
+ if (value instanceof String) {
+ try {
+ return Float.parseFloat((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to double: " + e.getMessage());
+ }
+ }
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to double");
}
- /**格式化,显示更好看
- * @param object
- * @return
+
+ /**
+ * Get a double value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The double value
+ * @throws IllegalArgumentException If value cannot be converted to double
*/
- public static String format(Object object) {
- return toJSONString(object, SerializerFeature.PrettyFormat);
+ public static float getFloatValue(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return 0;
+ }
+
+ if (value instanceof Number) {
+ return ((Number) value).floatValue();
+ }
+
+ if (value instanceof String) {
+ try {
+ return Float.parseFloat((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to double: " + e.getMessage());
+ }
+ }
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to double");
}
- /**判断是否为JSONObject
- * @param obj instanceof String ? parseObject
- * @return
+
+ /**
+ * Get a double value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The double value
+ * @throws IllegalArgumentException If value cannot be converted to double
*/
- public static boolean isJSONObject(Object obj) {
- if (obj instanceof JSONObject) {
- return true;
+ public static Double getDouble(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
}
- if (obj instanceof String) {
+
+ if (value instanceof Number) {
+ return ((Number) value).doubleValue();
+ }
+
+ if (value instanceof String) {
try {
- JSONObject json = parseObject((String) obj);
- return json != null && json.isEmpty() == false;
- } catch (Exception e) {
- Log.e(TAG, "isJSONObject catch \n" + e.getMessage());
+ return Double.parseDouble((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to double: " + e.getMessage());
}
}
- return false;
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to double");
}
- /**判断是否为JSONArray
- * @param obj instanceof String ? parseArray
- * @return
+
+ /**
+ * Get a double value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The double value
+ * @throws IllegalArgumentException If value cannot be converted to double
*/
- public static boolean isJSONArray(Object obj) {
- if (obj instanceof JSONArray) {
- return true;
+ public static double getDoubleValue(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return 0;
}
- if (obj instanceof String) {
+
+ if (value instanceof Number) {
+ return ((Number) value).doubleValue();
+ }
+
+ if (value instanceof String) {
try {
- JSONArray json = parseArray((String) obj);
- return json != null && json.isEmpty() == false;
- } catch (Exception e) {
- Log.e(TAG, "isJSONArray catch \n" + e.getMessage());
+ return Double.parseDouble((String) value);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to double: " + e.getMessage());
}
}
- return false;
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to double");
}
- /**判断是否为 Boolean,Number,String 中的一种
- * @param obj
- * @return
+
+ /**
+ * Get a boolean value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The boolean value
+ * @throws IllegalArgumentException If value cannot be converted to boolean
*/
- public static boolean isBooleanOrNumberOrString(Object obj) {
- return obj instanceof Boolean || obj instanceof Number || obj instanceof String;
+ public static Boolean getBoolean(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
+ }
+
+ if (value instanceof Boolean) {
+ return (Boolean) value;
+ }
+
+ if (value instanceof String) {
+ String str = ((String) value).toLowerCase();
+ if (str.equals("true") || str.equals("false")) {
+ return Boolean.parseBoolean(str);
+ }
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to boolean");
+ }
+
+ if (value instanceof Number) {
+ int intValue = ((Number) value).intValue();
+ if (intValue == 0 || intValue == 1) {
+ return intValue != 0;
+ }
+ throw new IllegalArgumentException("Cannot convert Number value '" + value + "' to boolean. Only 0 and 1 are supported.");
+ }
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to boolean");
+ }
+
+ /**
+ * Get a boolean value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The boolean value
+ * @throws IllegalArgumentException If value cannot be converted to boolean
+ */
+ public static boolean getBooleanValue(Map map, String key) throws IllegalArgumentException {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return false;
+ }
+
+ if (value instanceof Boolean) {
+ return (Boolean) value;
+ }
+
+ if (value instanceof String) {
+ String str = ((String) value).toLowerCase();
+ if (str.equals("true") || str.equals("false")) {
+ return Boolean.parseBoolean(str);
+ }
+ throw new IllegalArgumentException("Cannot convert String value '" + value + "' to boolean");
+ }
+
+ if (value instanceof Number) {
+ int intValue = ((Number) value).intValue();
+ if (intValue == 0 || intValue == 1) {
+ return intValue != 0;
+ }
+ throw new IllegalArgumentException("Cannot convert Number value '" + value + "' to boolean. Only 0 and 1 are supported.");
+ }
+
+ throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to boolean");
+ }
+
+ /**
+ * Get a string value from a Map
+ * @param map Source map
+ * @param key The key
+ * @return The string value
+ */
+ public static String getString(Map map, String key) {
+ Object value = map == null || key == null ? null : map.get(key);
+ if (value == null) {
+ return null;
+ }
+
+ return value.toString();
+ }
+
+
+ public static Object getFromObjOrArr(Object parent, String k) {
+ if (parent instanceof Map, ?>) {
+ return ((Map) parent).get(k);
+ }
+
+ if (parent instanceof List>) {
+ int j = Integer.valueOf(k);
+ return ((List>) parent).get(j);
+ }
+
+ return null;
}
}
diff --git a/APIJSONORM/src/main/java/apijson/JSONCreator.java b/APIJSONORM/src/main/java/apijson/JSONCreator.java
new file mode 100755
index 000000000..df0d9066a
--- /dev/null
+++ b/APIJSONORM/src/main/java/apijson/JSONCreator.java
@@ -0,0 +1,54 @@
+/*Copyright (C) 2020 Tencent. All rights reserved.
+
+This source code is licensed under the Apache License Version 2.0.*/
+
+
+package apijson;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+/**JSON相关创建器
+ * @author Lemon
+ */
+public interface JSONCreator, L extends List> {
+
+ @NotNull
+ M createJSONObject();
+
+ @NotNull
+ default M createJSONObject(String key, Object value) {
+ M obj = createJSONObject();
+ obj.put(key, value);
+ return obj;
+ }
+
+ @NotNull
+ default M createJSONObject(Map extends String, ?> map) {
+ M obj = createJSONObject();
+ if (map != null && ! map.isEmpty()) {
+ obj.putAll(map);
+ }
+ return obj;
+ }
+
+ @NotNull
+ L createJSONArray();
+
+ @NotNull
+ default L createJSONArray(Object obj){
+ L arr = createJSONArray();
+ arr.add(obj);
+ return arr;
+ }
+
+ @NotNull
+ default L createJSONArray(Collection> list){
+ L arr = createJSONArray();
+ if (list != null && ! list.isEmpty()) {
+ arr.addAll(list);
+ }
+ return arr;
+ }
+}
diff --git a/APIJSONORM/src/main/java/apijson/JSONList.java b/APIJSONORM/src/main/java/apijson/JSONList.java
new file mode 100644
index 000000000..092bf9f39
--- /dev/null
+++ b/APIJSONORM/src/main/java/apijson/JSONList.java
@@ -0,0 +1,312 @@
+/*Copyright (C) 2020 Tencent. All rights reserved.
+
+This source code is licensed under the Apache License Version 2.0.*/
+
+package apijson;
+
+import java.util.*;
+
+/**
+ * Custom JSONList implementation based on ArrayList to replace com.alibaba.fastjson.JSONList
+ * Maintains same API as fastjson but uses standard Java List implementation
+ * @author Lemon
+ */
+public interface JSONList, L extends List> extends List {
+ public static final String TAG = "JSONList";
+
+ ///**
+ // * Create an empty JSONList
+ // */
+ //default JSONList() {
+ // super();
+ //}
+ //
+ //private int initialCapacity = 10;
+ ///**
+ // * Create a JSONList with initial capacity
+ // * @param initialCapacity the initial capacity
+ // */
+ //default JSONList(int initialCapacity) {
+ // super(initialCapacity);
+ //}
+ //
+ ///**
+ // * Create a JSONList from a Collection
+ // * @param collection the collection to copy from
+ // */
+ //default JSONList(Collection> collection) {
+ // super(collection);
+ //}
+ //
+ ///**
+ // * Create a JSONList from a JSON string
+ // * @param json JSON string
+ // */
+ //default JSONList(String json) {
+ // this();
+ // List list = JSON.parseArray(json);
+ // if (list != null) {
+ // addAll(list);
+ // }
+ //}
+ //
+ /**
+ * Get a JSONMap at the specified index
+ * @param index the index
+ * @return the JSONMap or null if not a JSONMap
+ */
+ default M getJSONObject(int index) {
+ if (index < 0 || index>= size()) {
+ return null;
+ }
+
+ Object obj = get(index);
+ if (obj instanceof Map, ?>) {
+ return JSON.createJSONObject((Map extends String, ?>) obj);
+ }
+
+ return null;
+ }
+
+ /**
+ * Get a JSONList at the specified index
+ * @param index the index
+ * @return the JSONList or null if not a JSONList
+ */
+ default L getJSONArray(int index) {
+ if (index < 0 || index>= size()) {
+ return null;
+ }
+
+ Object obj = get(index);
+ if (obj instanceof List>) {
+ return JSON.createJSONArray((List>) obj);
+ }
+
+ return null;
+ }
+
+ /**
+ * Get a boolean value at the specified index
+ * @param index the index
+ * @return the boolean value or false if not found
+ */
+ default boolean getBooleanValue(int index) {
+ if (index < 0 || index>= size()) {
+ return false;
+ }
+
+ Object obj = get(index);
+ if (obj instanceof Boolean) {
+ return (Boolean) obj;
+ } else if (obj instanceof Number) {
+ return ((Number) obj).intValue() != 0;
+ } else if (obj instanceof String) {
+ return Boolean.parseBoolean((String) obj);
+ }
+
+ return false;
+ }
+
+ /**
+ * Get an integer value at the specified index
+ * @param index the index
+ * @return the integer value or 0 if not found
+ */
+ default int getIntValue(int index) {
+ if (index < 0 || index>= size()) {
+ return 0;
+ }
+
+ Object obj = get(index);
+ if (obj instanceof Number) {
+ return ((Number) obj).intValue();
+ } else if (obj instanceof String) {
+ try {
+ return Integer.parseInt((String) obj);
+ } catch (NumberFormatException e) {
+ // Ignore
+ }
+ }
+ return 0;
+ }
+
+ /**
+ * Get a long value at the specified index
+ * @param index the index
+ * @return the long value or 0 if not found
+ */
+ default long getLongValue(int index) {
+ if (index < 0 || index>= size()) {
+ return 0L;
+ }
+
+ Object obj = get(index);
+ if (obj instanceof Number) {
+ return ((Number) obj).longValue();
+ } else if (obj instanceof String) {
+ try {
+ return Long.parseLong((String) obj);
+ } catch (NumberFormatException e) {
+ // Ignore
+ }
+ }
+ return 0L;
+ }
+
+ /**
+ * Get a double value at the specified index
+ * @param index the index
+ * @return the double value or 0 if not found
+ */
+ default double getDoubleValue(int index) {
+ if (index < 0 || index>= size()) {
+ return 0.0;
+ }
+
+ Object obj = get(index);
+ if (obj instanceof Number) {
+ return ((Number) obj).doubleValue();
+ } else if (obj instanceof String) {
+ try {
+ return Double.parseDouble((String) obj);
+ } catch (NumberFormatException e) {
+ // Ignore
+ }
+ }
+ return 0.0;
+ }
+
+ /**
+ * Get a string value at the specified index
+ * @param index the index
+ * @return the string value or null if not found
+ */
+ default String getString(int index) {
+ if (index < 0 || index>= size()) {
+ return null;
+ }
+
+ Object obj = get(index);
+ return obj != null ? obj.toString() : null;
+ }
+
+
+ default String toJSONString() {
+ return JSON.toJSONString(this);
+ }
+
+ //@Override
+ //default boolean containsAll(Collection> c) {
+ // if (c == null || c.isEmpty()) {
+ // return true;
+ // }
+ // return super.containsAll(c);
+ //}
+ //
+ //@Override
+ //default boolean addAll(Collection> c) {
+ // if (c == null || c.isEmpty()) {
+ // return true;
+ // }
+ // return super.addAll(c);
+ //}
+ //
+ //@Override
+ //default boolean addAll(int index, Collection> c) {
+ // if (c == null || c.isEmpty()) {
+ // return true;
+ // }
+ //
+ // int sz = size();
+ // if (index < 0 || index>= sz) {
+ // index += sz;
+ // }
+ //
+ // return super.addAll(index, c);
+ //}
+ //
+ //@Override
+ //default boolean removeAll(Collection> c) {
+ // if (c == null || c.isEmpty()) {
+ // return true;
+ // }
+ // return super.removeAll(c);
+ //}
+ //
+ //@Override
+ //default boolean retainAll(Collection> c) {
+ // if (c == null || c.isEmpty()) {
+ // return true;
+ // }
+ // return super.retainAll(c);
+ //}
+ //
+ //
+ //@Override
+ //default Object get(int index) {
+ // int sz = size();
+ // if (index < 0 || index>= sz) {
+ // index += sz;
+ // }
+ //
+ // return super.get(index);
+ //}
+ //
+ //@Override
+ //default Object set(int index, Object element) {
+ // int sz = size();
+ // if (index < 0 || index>= sz) {
+ // index += sz;
+ // }
+ //
+ // return super.set(index, element);
+ //}
+ //
+ //@Override
+ //default void add(int index, Object element) {
+ // int sz = size();
+ // if (index < 0 || index>= sz) {
+ // index += sz;
+ // }
+ //
+ // super.add(index, element);
+ //}
+ //
+ //@Override
+ //default Object remove(int index) {
+ // int sz = size();
+ // if (index < 0 && index>= -sz) {
+ // index += sz;
+ // }
+ // if (index < 0 || index>= sz) {
+ // return null;
+ // }
+ //
+ // return super.remove(index);
+ //}
+ //
+ //@Override
+ //default ListIterator listIterator(int index) {
+ // int sz = size();
+ // if (index < 0 && index>= -sz) {
+ // index += sz;
+ // }
+ //
+ // return super.listIterator(index);
+ //}
+ //
+ //@Override
+ //default List subList(int fromIndex, int toIndex) {
+ // int sz = size();
+ // if (fromIndex < 0 && fromIndex>= -sz) {
+ // fromIndex += sz;
+ // }
+ // if (toIndex < 0 && toIndex>= -sz) {
+ // toIndex += sz;
+ // }
+ //
+ // return super.subList(fromIndex, toIndex);
+ //}
+
+}
\ No newline at end of file
diff --git a/APIJSONORM/src/main/java/apijson/JSONMap.java b/APIJSONORM/src/main/java/apijson/JSONMap.java
new file mode 100755
index 000000000..3b93d69d2
--- /dev/null
+++ b/APIJSONORM/src/main/java/apijson/JSONMap.java
@@ -0,0 +1,833 @@
+/*Copyright (C) 2020 Tencent. All rights reserved.
+
+This source code is licensed under the Apache License Version 2.0.*/
+
+
+package apijson;
+
+import java.util.*;
+
+
+/**use this class instead of com.alibaba.fastjson.JSONMap
+ * @author Lemon
+ * @see #put
+ * @see #puts
+ * @see #putsAll
+ */
+//default class JSONMap extends LinkedHashMap {
+public interface JSONMap, L extends List> extends Map {
+ static final String TAG = "JSONMap";
+
+ // 只能是 static public Map map = new LinkedHashMap();
+
+ ///**ordered
+ // */
+ //default JSONMap() {
+ // super();
+ //}
+ ///**transfer Object to JSONMap
+ // * @param object
+ // * @see {@link #JSONMap(Object)}
+ // */
+ //default JSONMap(Object object) {
+ // this();
+ // if (object instanceof Map) {
+ // @SuppressWarnings("unchecked")
+ // Map map = (Map) object;
+ // putAll(map);
+ // } else if (object != null) {
+ // String json = JSON.toJSONString(object);
+ // if (json != null) {
+ // Map map = JSON.parseObject(json);
+ // if (map != null) {
+ // putAll(map);
+ // }
+ // }
+ // }
+ //}
+ ///**parse JSONMap with JSON String
+ // * @param json
+ // * @see {@link #JSONMap(String)}
+ // */
+ //default JSONMap(String json) {
+ // this();
+ // Map map = JSON.parseObject(json);
+ // if (map != null) {
+ // putAll(map);
+ // }
+ //}
+ ///**transfer com.alibaba.fastjson.JSONMap to JSONMap
+ // * @param object
+ // * @see {@link #putsAll(Map extends String, ? extends Object>)}
+ // */
+ //default JSONMap(Map object) {
+ // this();
+ // putsAll(object);
+ //}
+
+ //public static JSONMap valueOf(Object obj) {
+ // JSONMap req = new JSONMap() {};
+ // Map m = JSON.parseObject(obj);
+ // if (m != null && ! m.isEmpty()) {
+ // req.map.putAll(m);
+ // }
+ // return req;
+ //}
+
+ //judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + String KEY_ARRAY = "[]"; + + /**判断是否为Array的key + * @param key + * @return + */ + public static boolean isArrayKey(String key) { + return key != null && key.endsWith(KEY_ARRAY); + } + /**判断是否为对应Table的key + * @param key + * @return + */ + public static boolean isTableKey(String key) { + return StringUtil.isBigName(key); + } + /**判断是否为对应Table数组的 key + * @param key + * @return + */ + public static boolean isTableArray(String key) { + return isArrayKey(key) && isTableKey(key.substring(0, key.length() - KEY_ARRAY.length())); + } + //judge>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+
+
+ //JSONObject内关键词 key <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + + public static String KEY_ID = "id"; + public static String KEY_ID_IN = KEY_ID + "{}"; + public static String KEY_USER_ID = "userId"; + public static String KEY_USER_ID_IN = KEY_USER_ID + "{}"; + + default String getIdKey() { + return KEY_ID; + } + default String getIdInKey() { + return KEY_ID_IN; + } + default String getUserIdKey() { + return KEY_USER_ID; + } + default String getUserIdInKey() { + return KEY_USER_ID_IN; + } + + /**set "id":id in Table layer + * @param id + * @return + */ + default JSONMap setId(Long id) {
+ return puts(getIdKey(), id);
+ }
+ /**set "id{}":[] in Table layer
+ * @param list
+ * @return
+ */
+ default JSONMap setIdIn(List list) {
+ return puts(getIdInKey(), list);
+ }
+
+ /**set "userId":userId in Table layer
+ * @param id
+ * @return
+ */
+ default JSONMap setUserId(Long id) {
+ return puts(getUserIdKey(), id);
+ }
+ /**set "userId{}":[] in Table layer
+ * @param list
+ * @return
+ */
+ default JSONMap setUserIdIn(List list) {
+ return puts(getUserIdInKey(), list);
+ }
+
+
+ int CACHE_ALL = 0;
+ int CACHE_ROM = 1;
+ int CACHE_RAM = 2;
+
+ String CACHE_ALL_STRING = "ALL";
+ String CACHE_ROM_STRING = "ROM";
+ String CACHE_RAM_STRING = "RAM";
+
+
+ //@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<< + String KEY_TRY = "@try"; // 尝试,忽略异常 + String KEY_CATCH = "@catch"; // TODO 捕捉到异常后,处理方式 null-不处理;DEFAULT-返回默认值;ORIGIN-返回请求里的原始值 + String KEY_DROP = "@drop"; // 丢弃,不返回,TODO 应该通过 fastjson 的 ignore 之类的机制来处理,避免导致下面的对象也不返回 + // String KEY_KEEP = "@keep"; // 一定会返回,为 null 或 空对象时,会使用默认值(非空),解决其它对象因为不关联的第一个对为空导致也不返回 + String KEY_DEFAULT = "@default"; // TODO 自定义默认值 { "@default":true },@default 可完全替代 @keep + String KEY_NULL = "@null"; // 值为 null 的键值对 "@null":"tag,pictureList",允许 is NULL 条件判断, SET tag = NULL 修改值为 NULL 等 + String KEY_CAST = "@cast"; // 类型转换 cast(date AS DATE) + + String KEY_ROLE = "@role"; // 角色,拥有对某些数据的某些操作的权限 + String KEY_DATABASE = "@database"; // 数据库类型,默认为MySQL + String KEY_DATASOURCE = "@datasource"; // 数据源 + String KEY_NAMESPACE = "@namespace"; // 命名空间,Table 在非默认 namespace 内时需要声明 + String KEY_CATALOG = "@catalog"; // 目录,Table 在非默认 catalog 内时需要声明 + String KEY_SCHEMA = "@schema"; // 数据库,Table 在非默认 schema 内时需要声明 + String KEY_EXPLAIN = "@explain"; // 分析 true/false + String KEY_CACHE = "@cache"; // 缓存 RAM/ROM/ALL + String KEY_COLUMN = "@column"; // 查询的 Table 字段或 SQL 函数 + String KEY_FROM = "@from"; // FROM语句 + String KEY_COMBINE = "@combine"; // 条件组合,每个条件 key 前面可以放 &,|,! 逻辑关系 "id!{},&sex,!name&$" + String KEY_GROUP = "@group"; // 分组方式 + String KEY_HAVING = "@having"; // 聚合函数条件,一般和 @group 一起用 + String KEY_HAVING_AND = "@having&"; // 聚合函数条件,一般和 @group 一起用 + String KEY_SAMPLE = "@sample"; // 取样方式 + String KEY_LATEST = "@latest"; // 最近方式 + String KEY_PARTITION = "@partition"; // 分区方式 + String KEY_FILL = "@fill"; // 填充方式 + String KEY_ORDER = "@order"; // 排序方式 + String KEY_KEY = "@key"; // key 映射,year:left(date,4);name_tag:(name,tag) + String KEY_RAW = "@raw"; // 自定义原始 SQL 片段 + String KEY_JSON = "@json"; // 把字段转为 JSON 输出 + String KEY_STRING = "@string"; // 把字段转为 String 输入 + String KEY_METHOD = "@method"; // json 对象配置操作方法 + String KEY_GET = "@get"; // json 对象配置操作方法 + String KEY_GETS = "@gets"; // json 对象配置操作方法 + String KEY_HEAD = "@head"; // json 对象配置操作方法 + String KEY_HEADS = "@heads"; // json 对象配置操作方法 + String KEY_POST = "@post"; // json 对象配置操作方法 + String KEY_PUT = "@put"; // json 对象配置操作方法 + String KEY_DELETE = "@delete"; // json 对象配置操作方法 + + List TABLE_KEY_LIST = new ArrayList(Arrays.asList(
+ KEY_ROLE,
+ KEY_DATABASE,
+ KEY_DATASOURCE,
+ KEY_NAMESPACE,
+ KEY_CATALOG,
+ KEY_SCHEMA,
+ KEY_EXPLAIN,
+ KEY_CACHE,
+ KEY_COLUMN,
+ KEY_FROM,
+ KEY_NULL,
+ KEY_CAST,
+ KEY_COMBINE,
+ KEY_GROUP,
+ KEY_HAVING,
+ KEY_HAVING_AND,
+ KEY_SAMPLE,
+ KEY_LATEST,
+ KEY_PARTITION,
+ KEY_FILL,
+ KEY_ORDER,
+ KEY_KEY,
+ KEY_RAW,
+ KEY_JSON,
+ KEY_STRING,
+ KEY_METHOD,
+ KEY_GET,
+ KEY_GETS,
+ KEY_HEAD,
+ KEY_HEADS,
+ KEY_POST,
+ KEY_PUT,
+ KEY_DELETE
+ ));
+
+ //@key关键字都放这个类>>>>>>>>>>>>>>>>>>>>>>
+
+
+ /**set try, ignore exceptions
+ * @param tri
+ * @return this
+ */
+ default JSONMap setTry(Boolean tri) {
+ return puts(KEY_TRY, tri);
+ }
+
+ /**set catch
+ * @param isCatch
+ * @return this
+ */
+ default JSONMap setCatch(String isCatch) {
+ return puts(KEY_CATCH, isCatch);
+ }
+ /**set drop, data dropped will not return
+ * @param drop
+ * @return this
+ */
+ default JSONMap setDrop(Boolean drop) {
+ return puts(KEY_DROP, drop);
+ }
+
+ /**set if has default
+ * @param hasDefault
+ * @return this
+ */
+ default JSONMap setDefault(Boolean hasDefault) {
+ return puts(KEY_DEFAULT, hasDefault);
+ }
+
+
+ /**set role of request sender
+ * @param role
+ * @return this
+ */
+ default JSONMap setRole(String role) {
+ return puts(KEY_ROLE, role);
+ }
+ /**set database where table was puts
+ * @param database
+ * @return this
+ */
+ default JSONMap setDatabase(String database) {
+ return puts(KEY_DATABASE, database);
+ }
+ /**set datasource where table was puts
+ * @param datasource
+ * @return this
+ */
+ default JSONMap setDatasource(String datasource) {
+ return puts(KEY_DATASOURCE, datasource);
+ }
+ /**set namespace where table was puts
+ * @param namespace
+ * @return this
+ */
+ default JSONMap setNamespace(String namespace) {
+ return puts(KEY_NAMESPACE, namespace);
+ }
+ /**set catalog where table was puts
+ * @param catalog
+ * @return this
+ */
+ default JSONMap setCatalog(String catalog) {
+ return puts(KEY_CATALOG, catalog);
+ }
+ /**set schema where table was puts
+ * @param schema
+ * @return this
+ */
+ default JSONMap setSchema(String schema) {
+ return puts(KEY_SCHEMA, schema);
+ }
+ /**set if return explain informations
+ * @param explain
+ * @return
+ */
+ default JSONMap setExplain(Boolean explain) {
+ return puts(KEY_EXPLAIN, explain);
+ }
+ /**set cache type
+ * @param cache
+ * @return
+ * @see {@link #CACHE_ALL}
+ * @see {@link #CACHE_RAM}
+ * @see {@link #CACHE_ROM}
+ */
+ default JSONMap setCache(Integer cache) {
+ return puts(KEY_CACHE, cache);
+ }
+ /**set cache type
+ * @param cache
+ * @return
+ * @see {@link #CACHE_ALL_STRING}
+ * @see {@link #CACHE_RAM_STRING}
+ * @see {@link #CACHE_ROM_STRING}
+ */
+ default JSONMap setCache(String cache) {
+ return puts(KEY_CACHE, cache);
+ }
+
+ /**set keys need to be returned
+ * @param keys key0, key1, key2 ...
+ * @return {@link #setColumn(String)}
+ */
+ default JSONMap setColumn(String... keys) {
+ return setColumn(StringUtil.get(keys, true));
+ }
+ /**set keys need to be returned
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setColumn(String keys) {
+ return puts(KEY_COLUMN, keys);
+ }
+
+ /**set keys whose value is null
+ * @param keys key0, key1, key2 ...
+ * @return {@link #setNull(String)}
+ */
+ default JSONMap setNull(String... keys) {
+ return setNull(StringUtil.get(keys, true));
+ }
+ /**set keys whose value is null
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setNull(String keys) {
+ return puts(KEY_NULL, keys);
+ }
+
+ /**set keys and types whose value should be cast to type, cast(value AS DATE)
+ * @param keyTypes key0:type0, key1:type1, key2:type2 ...
+ * @return {@link #setCast(String)}
+ */
+ default JSONMap setCast(String... keyTypes) {
+ return setCast(StringUtil.get(keyTypes, true));
+ }
+ /**set keys and types whose value should be cast to type, cast(value AS DATE)
+ * @param keyTypes "key0:type0,key1:type1,key2:type2..."
+ * @return
+ */
+ default JSONMap setCast(String keyTypes) {
+ return puts(KEY_CAST, keyTypes);
+ }
+
+ /**set combination of keys for conditions
+ * @param keys key0,&key1,|key2,!key3 ... TODO or key0> | (key1{} & !key2)...
+ * @return {@link #setColumn(String)}
+ */
+ default JSONMap setCombine(String... keys) {
+ return setCombine(StringUtil.get(keys, true));
+ }
+ /**set combination of keys for conditions
+ * @param keys key0,&key1,|key2,!key3 ... TODO or key0> | (key1{} & !key2)...
+ * @return
+ */
+ default JSONMap setCombine(String keys) {
+ return puts(KEY_COMBINE, keys);
+ }
+
+ /**set keys for group by
+ * @param keys key0, key1, key2 ...
+ * @return {@link #setGroup(String)}
+ */
+ default JSONMap setGroup(String... keys) {
+ return setGroup(StringUtil.get(keys, true));
+ }
+ /**set keys for group by
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setGroup(String keys) {
+ return puts(KEY_GROUP, keys);
+ }
+
+ /**set keys for having
+ * @param keys count(key0)> 1, sum(key1) <= 5, function2(key2) ? value2 ... + * @return {@link #setHaving(String)} + */ + default JSONMap setHaving(String... keys) {
+ return setHaving(StringUtil.get(keys, true));
+ }
+ /**set keys for having
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setHaving(String keys) {
+ return setHaving(keys, false);
+ }
+ /**set keys for having
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setHaving(String keys, boolean isAnd) {
+ return puts(isAnd ? KEY_HAVING_AND : KEY_HAVING, keys);
+ }
+
+ /**set keys for sample by
+ * @param keys key0, key1, key2 ...
+ * @return {@link #setSample(String)}
+ */
+ default JSONMap setSample(String... keys) {
+ return setSample(StringUtil.get(keys, true));
+ }
+ /**set keys for sample by
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setSample(String keys) {
+ return puts(KEY_SAMPLE, keys);
+ }
+
+ /**set keys for latest on
+ * @param keys key0, key1, key2 ...
+ * @return {@link #setLatest(String)}
+ */
+ default JSONMap setLatest(String... keys) {
+ return setLatest(StringUtil.get(keys, true));
+ }
+ /**set keys for latest on
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setLatest(String keys) {
+ return puts(KEY_LATEST, keys);
+ }
+
+ /**set keys for partition by
+ * @param keys key0, key1, key2 ...
+ * @return {@link #setPartition(String)}
+ */
+ default JSONMap setPartition(String... keys) {
+ return setPartition(StringUtil.get(keys, true));
+ }
+ /**set keys for partition by
+ * @param keys key0, key1, key2 ...
+ * @return
+ */
+ default JSONMap setPartition(String keys) {
+ return puts(KEY_PARTITION, keys);
+ }
+
+ /**set keys for fill(key): fill(null), fill(linear), fill(prev)
+ * @param keys key0, key1, key2 ...
+ * @return {@link #setFill(String)}
+ */
+ default JSONMap setFill(String... keys) {
+ return setFill(StringUtil.get(keys, true));
+ }
+ /**set keys for fill(key): fill(null), fill(linear), fill(prev)
+ * @param keys key0, key1, key2 ...
+ * @return
+ */
+ default JSONMap setFill(String keys) {
+ return puts(KEY_FILL, keys);
+ }
+
+ /**set keys for order by
+ * @param keys key0, key1+, key2- ...
+ * @return {@link #setOrder(String)}
+ */
+ default JSONMap setOrder(String... keys) {
+ return setOrder(StringUtil.get(keys, true));
+ }
+ /**set keys for order by
+ * @param keys "key0,key1+,key2-..."
+ * @return
+ */
+ default JSONMap setOrder(String keys) {
+ return puts(KEY_ORDER, keys);
+ }
+
+ /**set key map
+ * @param keyMap "name_tag:(name,tag);year:left(date,1,5)..."
+ * @return
+ */
+ default JSONMap setKey(String keyMap) {
+ return puts(KEY_KEY, keyMap);
+ }
+
+ /**set keys to raw
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setRaw(String keys) {
+ return puts(KEY_RAW, keys);
+ }
+
+ /**set keys to cast to json
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setJson(String keys) {
+ return puts(KEY_JSON, keys);
+ }
+
+ /**set keys to cast to string
+ * @param keys "key0,key1,key2..."
+ * @return
+ */
+ default JSONMap setString(String keys) {
+ return puts(KEY_STRING, keys);
+ }
+
+ //JSONObject内关键词 key>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+
+
+
+ //Request <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + + /** + * @param key + * @param keys path = keys[0] + "/" + keys[1] + "/" + keys[2] + ... + * @return {@link #puts(String, Object)} + */ + default JSONMap putsPath(String key, String... keys) {
+ return puts(key+"@", StringUtil.get(keys, "/"));
+ }
+
+ /**
+ * @param key
+ * @param isNull
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap putsNull(String key, boolean isNull) {
+ return puts(key+"{}", SQL.isNull(isNull));
+ }
+ /**
+ * trim = false
+ * @param key
+ * @param isEmpty
+ * @return {@link #putsEmpty(String, boolean, boolean)}
+ */
+ default JSONMap putsEmpty(String key, boolean isEmpty) {
+ return putsEmpty(key, isEmpty, false);
+ }
+ /**
+ * @param key
+ * @param isEmpty
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap putsEmpty(String key, boolean isEmpty, boolean trim) {
+ return puts(key+"{}", SQL.isEmpty(key, isEmpty, trim));
+ }
+ /**
+ * @param key
+ * @param compare <=0,>5 ...
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap putsLength(String key, String compare) {
+ return puts(key+"{}", SQL.length(key) + compare);
+ }
+ /**
+ * @param key
+ * @param compare <=,> ...
+ * @param value 1, 5, 3.14, -99 ...
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap putsLength(String key, String compare, Object value) {
+ return puts(key+"["+(StringUtil.isEmpty(compare) || "=".equals(compare) ? "" : ("!=".equals(compare) ? "!" : compare)), value);
+ }
+ /**
+ * @param key
+ * @param compare <=0,>5 ...
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap putsJSONLength(String key, String compare) {
+ return puts(key+"{}", SQL.json_length(key) + compare);
+ }
+ /**
+ * @param key
+ * @param compare <=0,>5 ...
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap putsJSONLength(String key, String compare, Object value) {
+ return puts(key + "{" + (StringUtil.isEmpty(compare) || "=".equals(compare) ? "" : ("!=".equals(compare) ? "!" : compare)), value);
+ }
+
+ /**设置搜索
+ * type = SEARCH_TYPE_CONTAIN_FULL
+ * @param key
+ * @param value
+ * @return {@link #putsSearch(String, String, int)}
+ */
+ default JSONMap putsSearch(String key, String value) {
+ return putsSearch(key, value, SQL.SEARCH_TYPE_CONTAIN_FULL);
+ }
+ /**设置搜索
+ * @param key
+ * @param value
+ * @param type
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap putsSearch(String key, String value, int type) {
+ return puts(key+"$", SQL.search(value, type));
+ }
+
+ //Request>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+
+ /**put and return this
+ * @param value must be annotated by {@link MethodAccess}
+ * @return {@link #puts(String, Object)}
+ */
+ default JSONMap puts(Object value) {
+ put(value);
+ return this;
+ }
+ /**put and return this
+ * @param key
+ * @param value
+ * @return this
+ */
+ default JSONMap puts(String key, Object value) {
+ put(key, value);
+ return this;
+ }
+
+ /**put and return value
+ * @param value must be annotated by {@link MethodAccess}
+ */
+ default Object put(Object value) {
+ Class> clazz = value.getClass(); //should not return null
+ if (clazz.getAnnotation(MethodAccess.class) == null) {
+ throw new IllegalArgumentException("puts StringUtil.isEmpty(key, true)" +
+ " clazz.getAnnotation(MethodAccess.class) == null" +
+ " \n key为空时仅支持 类型被@MethodAccess注解 的value !!!" +
+ " \n 如果一定要这么用,请对 " + clazz.getName() + " 注解!" +
+ " \n 如果是类似 key[]:{} 结构的请求,建议用 putsAll(...) !");
+ }
+ return put(clazz.getSimpleName(), value);
+ }
+
+ /**puts key-value in object into this
+ * @param map
+ * @return this
+ */
+ default JSONMap putsAll(Map extends String, ? extends Object> map) {
+ putAll(map);
+ return this;
+ }
+
+
+ /**
+ * Get a boolean value from the JSONMap
+ * @param key the key
+ * @return the boolean value or false if not found
+ */
+ default boolean getBooleanValue(String key) {
+ return JSON.getBooleanValue(this, key);
+ }
+
+ /**
+ * Get an integer value from the JSONMap
+ * @param key the key
+ * @return the integer value or 0 if not found
+ */
+ default int getIntValue(String key) {
+ return JSON.getIntValue(this, key);
+ }
+
+ /**
+ * Get a long value from the JSONMap
+ * @param key the key
+ * @return the long value or 0 if not found
+ */
+ default long getLongValue(String key) {
+ return JSON.getLongValue(this, key);
+ }
+
+ /**
+ * Get a double value from the JSONMap
+ * @param key the key
+ * @return the double value or 0 if not found
+ */
+ default double getDoubleValue(String key) {
+ return JSON.getDoubleValue(this, key);
+ }
+
+ /**
+ * Get a string value from the JSONMap
+ * @param key the key
+ * @return the string value or null if not found
+ */
+ default String getString(String key) {
+ Object value = get(key);
+ return value != null ? value.toString() : null;
+ }
+
+ /**
+ * Get a JSONMap value from the JSONMap
+ * @param key the key
+ * @return the JSONMap value or null if not found
+ */
+ default M getJSONObject(String key) {
+ Map map = JSON.getMap(this, key);
+ return map != null ? JSON.createJSONObject(map) : null;
+ }
+
+ /**
+ * Get a JSONList value from the JSONMap
+ * @param key the key
+ * @return the JSONList value or null if not found
+ */
+ default L getJSONArray(String key) {
+ List list = JSON.getList(this, key);
+ return list != null ? JSON.createJSONArray(list) : null;
+ }
+
+ @Override
+ default void putAll(Map extends String, ? extends Object> map) {
+ Set extends Map.Entry extends String, ?>> set = map == null ? null : map.entrySet();
+ if (set != null || set.isEmpty()) {
+ return;
+ }
+
+ for (Map.Entry extends String, ?> entry : set) {
+ put(entry.getKey(), entry.getValue());
+ }
+ }
+
+ default String toJSONString() {
+ return JSON.toJSONString(this);
+ }
+
+ //@Override
+ //default int size() {
+ // return map.size();
+ //}
+ //
+ //@Override
+ //default boolean isEmpty() {
+ // return map.isEmpty();
+ //}
+ //
+ //@Override
+ //default boolean containsKey(Object key) {
+ // return map.containsKey(key);
+ //}
+ //
+ //@Override
+ //default boolean containsValue(Object value) {
+ // return map.containsValue(value);
+ //}
+ //
+ //@Override
+ //default Object get(Object key) {
+ // return map.get(key);
+ //}
+ //
+ //@Override
+ //default Object put(String key, Object value) {
+ // return map.put(key, value);
+ //}
+ //
+ //@Override
+ //default Object remove(Object key) {
+ // return map.remove(key);
+ //}
+
+
+ //@Override
+ //default void clear() {
+ // map.clear();
+ //}
+ //
+ //@Override
+ //default Set keySet() {
+ // return map.keySet();
+ //}
+ //
+ //@Override
+ //default Collection values() {
+ // return map.values();
+ //}
+ //
+ //@Override
+ //default Set> entrySet() {
+ // return map.entrySet();
+ //}
+
+ //@Override
+ //default String toString() {
+ // return JSON.toJSONString(this);
+ //}
+
+}
diff --git a/APIJSONORM/src/main/java/apijson/JSONObject.java b/APIJSONORM/src/main/java/apijson/JSONObject.java
deleted file mode 100755
index 6825a02de..000000000
--- a/APIJSONORM/src/main/java/apijson/JSONObject.java
+++ /dev/null
@@ -1,550 +0,0 @@
-/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
-
-This source code is licensed under the Apache License Version 2.0.*/
-
-
-package apijson;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-/**use this class instead of com.alibaba.fastjson.JSONObject
- * @author Lemon
- * @see #put
- * @see #puts
- * @see #putsAll
- */
-public class JSONObject extends com.alibaba.fastjson.JSONObject {
- private static final long serialVersionUID = 1L;
-
- private static final String TAG = "JSONObject";
-
-
- /**ordered
- */
- public JSONObject() {
- super(true);
- }
- /**transfer Object to JSONObject
- * @param object
- * @see {@link #JSONObject(Object)}
- */
- public JSONObject(Object object) {
- this(toJSONString(object));
- }
- /**parse JSONObject with JSON String
- * @param json
- * @see {@link #JSONObject(String)}
- */
- public JSONObject(String json) {
- this(parseObject(json));
- }
- /**transfer com.alibaba.fastjson.JSONObject to JSONObject
- * @param object
- * @see {@link #putsAll(Map extends String, ? extends Object>)}
- */
- public JSONObject(com.alibaba.fastjson.JSONObject object) {
- this();
- putsAll(object);
- }
-
-
-
-
- //judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< - public static final String KEY_ARRAY = "[]"; - - /**判断是否为Array的key - * @param key - * @return - */ - public static boolean isArrayKey(String key) { - return key != null && key.endsWith(KEY_ARRAY); - } - /**判断是否为对应Table的key - * @param key - * @return - */ - public static boolean isTableKey(String key) { - return StringUtil.isBigName(key); - } - /**判断是否为对应Table数组的 key - * @param key - * @return - */ - public static boolean isTableArray(String key) { - return isArrayKey(key) && isTableKey(key.substring(0, key.length() - KEY_ARRAY.length())); - } - //judge>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-
-
- //JSONObject内关键词 key <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< - - - public static String KEY_ID = "id"; - public static String KEY_ID_IN = KEY_ID + "{}"; - public static String KEY_USER_ID = "userId"; - public static String KEY_USER_ID_IN = KEY_USER_ID + "{}"; - - /**set "id":id in Table layer - * @param id - * @return - */ - public JSONObject setId(Long id) { - return puts(KEY_ID, id); - } - /**set "id{}":[] in Table layer - * @param list - * @return - */ - public JSONObject setIdIn(List list) {
- return puts(KEY_ID_IN, list);
- }
-
- /**set "userId":userId in Table layer
- * @param id
- * @return
- */
- public JSONObject setUserId(Long id) {
- return puts(KEY_USER_ID, id);
- }
- /**set "userId{}":[] in Table layer
- * @param list
- * @return
- */
- public JSONObject setUserIdIn(List list) {
- return puts(KEY_USER_ID_IN, list);
- }
-
-
- public static final int CACHE_ALL = 0;
- public static final int CACHE_ROM = 1;
- public static final int CACHE_RAM = 2;
-
- public static final String CACHE_ALL_STRING = "ALL";
- public static final String CACHE_ROM_STRING = "ROM";
- public static final String CACHE_RAM_STRING = "RAM";
-
-
- //@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<< - public static final String KEY_TRY = "@try"; //尝试,忽略异常 - public static final String KEY_CATCH = "@catch"; //TODO 捕捉到异常后,处理方式 null-不处理;DEFAULT-返回默认值;ORIGIN-返回请求里的原始值 - public static final String KEY_DROP = "@drop"; //丢弃,不返回,TODO 应该通过 fastjson 的 ignore 之类的机制来处理,避免导致下面的对象也不返回 - // public static final String KEY_KEEP = "@keep"; //一定会返回,为 null 或 空对象时,会使用默认值(非空),解决其它对象因为不关联的第一个对为空导致也不返回 - public static final String KEY_DEFULT = "@default"; //TODO 自定义默认值 { "@default":true },@default 可完全替代 @keep - public static final String KEY_NULL = "@null"; //TODO 值为 null 的键值对 "@null":"tag,pictureList",允许 is NULL 条件判断, SET tag = NULL 修改值为 NULL 等 - public static final String KEY_CAST = "@cast"; //TODO 类型转换 cast(date AS DATE) - - public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限 - public static final String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL - public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明 - public static final String KEY_DATASOURCE = "@datasource"; //数据源 - public static final String KEY_EXPLAIN = "@explain"; //分析 true/false - public static final String KEY_CACHE = "@cache"; //缓存 RAM/ROM/ALL - public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数 - public static final String KEY_FROM = "@from"; //FROM语句 - public static final String KEY_COMBINE = "@combine"; //条件组合,每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$" - public static final String KEY_GROUP = "@group"; //分组方式 - public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用 - public static final String KEY_HAVING_AND = "@having&"; //聚合函数条件,一般和@group一起用 - public static final String KEY_ORDER = "@order"; //排序方式 - public static final String KEY_RAW = "@raw"; // 自定义原始 SQL 片段 - public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出 - - public static final List TABLE_KEY_LIST;
- static {
- TABLE_KEY_LIST = new ArrayList();
- TABLE_KEY_LIST.add(KEY_ROLE);
- TABLE_KEY_LIST.add(KEY_DATABASE);
- TABLE_KEY_LIST.add(KEY_SCHEMA);
- TABLE_KEY_LIST.add(KEY_DATASOURCE);
- TABLE_KEY_LIST.add(KEY_EXPLAIN);
- TABLE_KEY_LIST.add(KEY_CACHE);
- TABLE_KEY_LIST.add(KEY_COLUMN);
- TABLE_KEY_LIST.add(KEY_FROM);
- TABLE_KEY_LIST.add(KEY_NULL);
- TABLE_KEY_LIST.add(KEY_CAST);
- TABLE_KEY_LIST.add(KEY_COMBINE);
- TABLE_KEY_LIST.add(KEY_GROUP);
- TABLE_KEY_LIST.add(KEY_HAVING);
- TABLE_KEY_LIST.add(KEY_HAVING_AND);
- TABLE_KEY_LIST.add(KEY_ORDER);
- TABLE_KEY_LIST.add(KEY_RAW);
- TABLE_KEY_LIST.add(KEY_JSON);
- }
-
- //@key关键字都放这个类>>>>>>>>>>>>>>>>>>>>>>
-
-
- /**set try, ignore exceptions
- * @param tri
- * @return this
- */
- public JSONObject setTry(Boolean tri) {
- return puts(KEY_TRY, tri);
- }
-
- /**set catch
- * @param isCatch
- * @return this
- */
- public JSONObject setCatch(String isCatch) {
- return puts(KEY_CATCH, isCatch);
- }
- /**set drop, data dropped will not return
- * @param drop
- * @return this
- */
- public JSONObject setDrop(Boolean drop) {
- return puts(KEY_DROP, drop);
- }
-
- /**set if has default
- * @param hasDefault
- * @return this
- */
- public JSONObject setDefault(Boolean hasDefault) {
- return puts(KEY_DEFULT, hasDefault);
- }
-
-
- /**set role of request sender
- * @param role
- * @return this
- */
- public JSONObject setRole(String role) {
- return puts(KEY_ROLE, role);
- }
- /**set database where table was puts
- * @param database
- * @return this
- */
- public JSONObject setDatabase(String database) {
- return puts(KEY_DATABASE, database);
- }
- /**set schema where table was puts
- * @param schema
- * @return this
- */
- public JSONObject setSchema(String schema) {
- return puts(KEY_SCHEMA, schema);
- }
- /**set datasource where table was puts
- * @param datasource
- * @return this
- */
- public JSONObject setDatasource(String datasource) {
- return puts(KEY_DATASOURCE, datasource);
- }
- /**set if return explain informations
- * @param explain
- * @return
- */
- public JSONObject setExplain(Boolean explain) {
- return puts(KEY_EXPLAIN, explain);
- }
- /**set cache type
- * @param cache
- * @return
- * @see {@link #CACHE_ALL}
- * @see {@link #CACHE_RAM}
- * @see {@link #CACHE_ROM}
- */
- public JSONObject setCache(Integer cache) {
- return puts(KEY_CACHE, cache);
- }
- /**set cache type
- * @param cache
- * @return
- * @see {@link #CACHE_ALL_STRING}
- * @see {@link #CACHE_RAM_STRING}
- * @see {@link #CACHE_ROM_STRING}
- */
- public JSONObject setCache(String cache) {
- return puts(KEY_CACHE, cache);
- }
-
- /**set keys need to be returned
- * @param keys key0, key1, key2 ...
- * @return {@link #setColumn(String)}
- */
- public JSONObject setColumn(String... keys) {
- return setColumn(StringUtil.getString(keys, true));
- }
- /**set keys need to be returned
- * @param keys "key0,key1,key2..."
- * @return
- */
- public JSONObject setColumn(String keys) {
- return puts(KEY_COLUMN, keys);
- }
-
- /**set keys whose value is null
- * @param keys key0, key1, key2 ...
- * @return {@link #setNull(String)}
- */
- public JSONObject setNull(String... keys) {
- return setNull(StringUtil.getString(keys, true));
- }
- /**set keys whose value is null
- * @param keys "key0,key1,key2..."
- * @return
- */
- public JSONObject setNull(String keys) {
- return puts(KEY_NULL, keys);
- }
-
- /**set keys and types whose value should be cast to type, cast(value AS DATE)
- * @param keyTypes key0:type0, key1:type1, key2:type2 ...
- * @return {@link #setCast(String)}
- */
- public JSONObject setCast(String... keyTypes) {
- return setCast(StringUtil.getString(keyTypes, true));
- }
- /**set keys and types whose value should be cast to type, cast(value AS DATE)
- * @param keyTypes "key0:type0,key1:type1,key2:type2..."
- * @return
- */
- public JSONObject setCast(String keyTypes) {
- return puts(KEY_CAST, keyTypes);
- }
-
- /**set combination of keys for conditions
- * @param keys key0,&key1,|key2,!key3 ... TODO or key0> | (key1{} & !key2)...
- * @return {@link #setColumn(String)}
- */
- public JSONObject setCombine(String... keys) {
- return setCombine(StringUtil.getString(keys, true));
- }
- /**set combination of keys for conditions
- * @param keys key0,&key1,|key2,!key3 ... TODO or key0> | (key1{} & !key2)...
- * @return
- */
- public JSONObject setCombine(String keys) {
- return puts(KEY_COMBINE, keys);
- }
-
- /**set keys for group by
- * @param keys key0, key1, key2 ...
- * @return {@link #setGroup(String)}
- */
- public JSONObject setGroup(String... keys) {
- return setGroup(StringUtil.getString(keys, true));
- }
- /**set keys for group by
- * @param keys "key0,key1,key2..."
- * @return
- */
- public JSONObject setGroup(String keys) {
- return puts(KEY_GROUP, keys);
- }
-
- /**set keys for having
- * @param keys count(key0)> 1, sum(key1) <= 5, function2(key2) ? value2 ... - * @return {@link #setHaving(String)} - */ - public JSONObject setHaving(String... keys) { - return setHaving(StringUtil.getString(keys, true)); - } - /**set keys for having - * @param keys "key0,key1,key2..." - * @return - */ - public JSONObject setHaving(String keys) { - return setHaving(keys, false); - } - /**set keys for having - * @param keys "key0,key1,key2..." - * @return - */ - public JSONObject setHaving(String keys, boolean isAnd) { - return puts(isAnd ? KEY_HAVING_AND : KEY_HAVING, keys); - } - - /**set keys for order by - * @param keys key0, key1+, key2- ... - * @return {@link #setOrder(String)} - */ - public JSONObject setOrder(String... keys) { - return setOrder(StringUtil.getString(keys, true)); - } - /**set keys for order by - * @param keys "key0,key1+,key2-..." - * @return - */ - public JSONObject setOrder(String keys) { - return puts(KEY_ORDER, keys); - } - - /**set keys to raw - * @param keys "key0,key1,key2..." - * @return - */ - public JSONObject setRaw(String keys) { - return puts(KEY_RAW, keys); - } - - /**set keys to cast to json - * @param keys "key0,key1,key2..." - * @return - */ - public JSONObject setJson(String keys) { - return puts(KEY_JSON, keys); - } - - /**用 setJson 替代。 - * set keys to cast to json - * @param keys "key0,key1,key2..." - * @return - * @see #{@link #setJson(String)} - */ - @Deprecated - public JSONObject setJSON(String keys) { - return puts(KEY_JSON, keys); - } - - - //JSONObject内关键词 key>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-
-
-
- //Request <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< - - - /** - * @param key - * @param keys path = keys[0] + "/" + keys[1] + "/" + keys[2] + ... - * @return {@link #puts(String, Object)} - */ - public JSONObject putsPath(String key, String... keys) { - return puts(key+"@", StringUtil.getString(keys, "/")); - } - - /** - * @param key - * @param isNull - * @return {@link #puts(String, Object)} - */ - public JSONObject putsNull(String key, boolean isNull) { - return puts(key+"{}", SQL.isNull(isNull)); - } - /** - * trim = false - * @param key - * @param isEmpty - * @return {@link #putsEmpty(String, boolean, boolean)} - */ - public JSONObject putsEmpty(String key, boolean isEmpty) { - return putsEmpty(key, isEmpty, false); - } - /** - * @param key - * @param isEmpty - * @return {@link #puts(String, Object)} - */ - public JSONObject putsEmpty(String key, boolean isEmpty, boolean trim) { - return puts(key+"{}", SQL.isEmpty(key, isEmpty, trim)); - } - /** - * @param key - * @param compare <=0,>5 ...
- * @return {@link #puts(String, Object)}
- */
- public JSONObject putsLength(String key, String compare) {
- return puts(key+"{}", SQL.length(key) + compare);
- }
-
- /**设置搜索
- * type = SEARCH_TYPE_CONTAIN_FULL
- * @param key
- * @param value
- * @return {@link #putsSearch(String, String, int)}
- */
- public JSONObject putsSearch(String key, String value) {
- return putsSearch(key, value, SQL.SEARCH_TYPE_CONTAIN_FULL);
- }
- /**设置搜索
- * @param key
- * @param value
- * @param type
- * @return {@link #puts(String, Object)}
- */
- public JSONObject putsSearch(String key, String value, int type) {
- return puts(key+"$", SQL.search(value, type));
- }
-
- //Request>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-
-
-
- /**puts key-value in object into this
- * @param map
- * @return this
- */
- public JSONObject putsAll(Map extends String, ? extends Object> map) {
- putAll(map);
- return this;
- }
- @Override
- public void putAll(Map extends String, ? extends Object> map) {
- if (map != null && map.isEmpty() == false) {
- super.putAll(map);
- }
- }
-
-
-
- /**put and return this
- * @param value must be annotated by {@link MethodAccess}
- * @return {@link #puts(String, Object)}
- */
- public JSONObject puts(Object value) {
- return puts(null, value);
- }
- /**put and return this
- * @param key
- * @param value
- * @return this
- * @see {@link #put(String, Object)}
- */
- public JSONObject puts(String key, Object value) {
- put(key, value);
- return this;
- }
-
- /**put and return value
- * @param value must be annotated by {@link MethodAccess}
- * @return {@link #put(String, Object)}
- */
- public Object put(Object value) {
- return put(null, value);
- }
- /**put and return value
- * @param key StringUtil.isEmpty(key, true) ? key = value.getClass().getSimpleName();
- * @param value
- * @return value
- */
- @Override
- public Object put(String key, Object value) {
- if (value == null) {
- Log.e(TAG, "put value == null>> return null;");
- return null;
- }
- if (StringUtil.isEmpty(key, true)) {
- Class> clazz = value.getClass(); //should not return null
- if (clazz.getAnnotation(MethodAccess.class) == null) {
- throw new IllegalArgumentException("puts StringUtil.isEmpty(key, true)" +
- " clazz.getAnnotation(MethodAccess.class) == null" +
- " \n key为空时仅支持 类型被@MethodAccess注解 的value !!!" +
- " \n 如果一定要这么用,请对 " + clazz.getName() + " 注解!" +
- " \n 如果是类似 key[]:{} 结构的请求,建议用 putsAll(...) !");
- }
- key = value.getClass().getSimpleName();
- }
- return super.put(key, value);
- }
-
-
-
-}
diff --git a/APIJSONORM/src/main/java/apijson/JSONParser.java b/APIJSONORM/src/main/java/apijson/JSONParser.java
new file mode 100755
index 000000000..7c38a39df
--- /dev/null
+++ b/APIJSONORM/src/main/java/apijson/JSONParser.java
@@ -0,0 +1,33 @@
+/*Copyright (C) 2020 Tencent. All rights reserved.
+
+This source code is licensed under the Apache License Version 2.0.*/
+
+
+package apijson;
+
+import java.util.List;
+import java.util.Map;
+
+/**JSON 相关解析器
+ * @author Lemon
+ */
+public interface JSONParser, L extends List> extends JSONCreator {
+
+ Object parse(Object json);
+
+ M parseObject(Object json);
+
+ T parseObject(Object json, Class clazz);
+
+ L parseArray(Object json);
+
+ List parseArray(Object json, Class clazz);
+
+ default String format(Object obj) {
+ return toJSONString(obj, true);
+ }
+ default String toJSONString(Object obj) {
+ return toJSONString(obj, false);
+ }
+ String toJSONString(Object obj, boolean format);
+}
diff --git a/APIJSONORM/src/main/java/apijson/JSONRequest.java b/APIJSONORM/src/main/java/apijson/JSONRequest.java
index 8707cc2c0..c74dfe349 100755
--- a/APIJSONORM/src/main/java/apijson/JSONRequest.java
+++ b/APIJSONORM/src/main/java/apijson/JSONRequest.java
@@ -1,4 +1,4 @@
-/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
+/*Copyright (C) 2020 Tencent. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
@@ -6,42 +6,50 @@
package apijson;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.Map;
+import static apijson.StringUtil.PATTERN_ALPHA_BIG;
+
/**wrapper for request
* @author Lemon
* @see #puts
* @see #toArray
- * @use JSONRequest request = new JSONRequest(...);
+ * @use JSONRequest request = JSON.createJSONObject(...);
*
request.puts(...);//not a must
*
request.toArray(...);//not a must
*/
-public class JSONRequest extends JSONObject {
- private static final long serialVersionUID = 1L;
-
- public JSONRequest() {
- super();
- }
- /**
- * @param object must be annotated by {@link MethodAccess}
- * @see {@link #JSONRequest(String, Object)}
- */
- public JSONRequest(Object object) {
- this(null, object);
- }
- /**
- * @param name
- * @param object
- * @see {@link #puts(String, Object)}
- */
- public JSONRequest(String name, Object object) {
- this();
- puts(name, object);
- }
-
+public interface JSONRequest, L extends List> extends JSONMap {
+ //default JSONRequest() {
+ // super();
+ //}
+ ///**
+ // * @param object must be annotated by {@link MethodAccess}
+ // * @see {@link #JSONRequest(String, Object)}
+ // */
+ //default JSONRequest(Object object) {
+ // this(null, object);
+ //}
+ ///**
+ // * @param name
+ // * @param object
+ // * @see {@link #puts(String, Object)}
+ // */
+ //default JSONRequest(String name, Object object) {
+ // this();
+ // puts(name, object);
+ //}
+ //public static JSONRequest valueOf(Object obj) {
+ // JSONRequest req = new JSONRequest() {};
+ // Map m = JSON.parseObject(obj);
+ // if (m != null && ! m.isEmpty()) {
+ // req.map.putAll(m);
+ // }
+ // return req;
+ //}
public static final String KEY_TAG = "tag";//只在最外层,最外层用JSONRequest
public static final String KEY_VERSION = "version";//只在最外层,最外层用JSONRequest
@@ -52,23 +60,25 @@ public JSONRequest(String name, Object object) {
* @param tag
* @return
*/
- public JSONRequest setTag(String tag) {
+ default JSONRequest setTag(String tag) {
return puts(KEY_TAG, tag);
}
+
/**set "version":version in outermost layer
* for target version of request
* @param version
* @return
*/
- public JSONRequest setVersion(Integer version) {
+ default JSONRequest setVersion(Integer version) {
return puts(KEY_VERSION, version);
}
+
/**set "format":format in outermost layer
* for format APIJSON special keys to normal keys of response
* @param format
* @return
*/
- public JSONRequest setFormat(Boolean format) {
+ default JSONRequest setFormat(Boolean format) {
return puts(KEY_FORMAT, format);
}
@@ -78,14 +88,14 @@ public JSONRequest setFormat(Boolean format) {
public static final int QUERY_TABLE = 0;
public static final int QUERY_TOTAL = 1;
public static final int QUERY_ALL = 2;
-
+
public static final String QUERY_TABLE_STRING = "TABLE";
public static final String QUERY_TOTAL_STRING = "TOTAL";
public static final String QUERY_ALL_STRING = "ALL";
public static final String SUBQUERY_RANGE_ALL = "ALL";
public static final String SUBQUERY_RANGE_ANY = "ANY";
-
+
public static final String KEY_QUERY = "query";
public static final String KEY_COMPAT = "compat";
public static final String KEY_COUNT = "count";
@@ -94,17 +104,9 @@ public JSONRequest setFormat(Boolean format) {
public static final String KEY_SUBQUERY_RANGE = "range";
public static final String KEY_SUBQUERY_FROM = "from";
- public static final List ARRAY_KEY_LIST;
- static {
- ARRAY_KEY_LIST = new ArrayList();
- ARRAY_KEY_LIST.add(KEY_QUERY);
- ARRAY_KEY_LIST.add(KEY_COMPAT);
- ARRAY_KEY_LIST.add(KEY_COUNT);
- ARRAY_KEY_LIST.add(KEY_PAGE);
- ARRAY_KEY_LIST.add(KEY_JOIN);
- ARRAY_KEY_LIST.add(KEY_SUBQUERY_RANGE);
- ARRAY_KEY_LIST.add(KEY_SUBQUERY_FROM);
- }
+ public static final List ARRAY_KEY_LIST = new ArrayList(Arrays.asList(
+ KEY_QUERY, KEY_COMPAT ,KEY_COUNT, KEY_PAGE, KEY_JOIN, KEY_SUBQUERY_RANGE, KEY_SUBQUERY_FROM
+ ));
/**set what to query in Array layer
* @param query what need to query, Table,total,ALL?
@@ -113,87 +115,161 @@ public JSONRequest setFormat(Boolean format) {
* @see {@link #QUERY_TOTAL}
* @see {@link #QUERY_ALL}
*/
- public JSONRequest setQuery(int query) {
+ default JSONRequest setQuery(int query) {
return puts(KEY_QUERY, query);
}
+
/**set maximum count of Tables to query in Array layer
* @param count <= 0 ||>= max ? max : count
* @return
*/
- public JSONRequest setCount(int count) {
+ default JSONRequest setCount(int count) {
return puts(KEY_COUNT, count);
}
+
/**set page of Tables to query in Array layer
* @param page <= 0 ? 0 : page * @return */ - public JSONRequest setPage(int page) { + default JSONRequest setPage(int page) {
return puts(KEY_PAGE, page);
}
-
+
/**set joins of Main Table and it's Vice Tables in Array layer
* @param joins "@/User/id@", "&/User/id@,>/Comment/momentId@" ...
* @return
*/
- public JSONRequest setJoin(String... joins) {
- return puts(KEY_JOIN, StringUtil.getString(joins));
+ default JSONRequest setJoin(String... joins) {
+ return setJson(this, StringUtil.get(joins));
}
-
+
+ public static > M setJson(M m, String... joins) {
+ m.put(KEY_JOIN, StringUtil.get(joins));
+ return m;
+ }
+
/**set range for Subquery
* @param range
* @return
* @see {@link #SUBQUERY_RANGE_ALL}
* @see {@link #SUBQUERY_RANGE_ANY}
*/
- public JSONRequest setSubqueryRange(String range) {
+ default JSONRequest setSubqueryRange(String range) {
return puts(KEY_SUBQUERY_RANGE, range);
}
-
+
/**set from for Subquery
- * @param range
+ * @param from
* @return
*/
- public JSONRequest setSubqueryFrom(String from) {
+ default JSONRequest setSubqueryFrom(String from) {
return puts(KEY_SUBQUERY_FROM, from);
}
-
- //array object>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+ //array object>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- /**create a parent JSONObject named KEY_ARRAY
+ /**create a parent JSONMap named KEY_ARRAY
* @param count
* @param page
* @return {@link #toArray(int, int)}
*/
- public JSONRequest toArray(int count, int page) {
+ default M toArray(int count, int page) {
return toArray(count, page, null);
}
- /**create a parent JSONObject named name+KEY_ARRAY.
+
+ /**create a parent JSONMap named name+KEY_ARRAY.
* @param count
* @param page
* @param name
* @return {name+KEY_ARRAY : this}. if needs to be put, use {@link #putsAll(Map extends String, ? extends Object>)} instead
*/
- public JSONRequest toArray(int count, int page, String name) {
- return new JSONRequest(StringUtil.getString(name) + KEY_ARRAY, this.setCount(count).setPage(page));
+ default M toArray(int count, int page, String name) {
+ return JSON.createJSONObject(StringUtil.get(name) + KEY_ARRAY, this.setCount(count).setPage(page));
}
@Override
- public JSONObject putsAll(Map extends String, ? extends Object> map) {
- super.putsAll(map);
+ default JSONRequest putsAll(Map extends String, ? extends Object> map) {
+ putAll(map);
return this;
}
@Override
- public JSONRequest puts(Object value) {
- return puts(null, value);
+ default JSONRequest puts(Object value) {
+ put(value);
+ return this;
}
+
@Override
- public JSONRequest puts(String key, Object value) {
- super.puts(key, value);
+ default JSONRequest puts(String key, Object value) {
+ put(key, value);
return this;
}
+
+ /**ABCdEfg => upper ? A-B-CD-EFG : a-b-cd-efg
+ * @param key
+ * @return
+ */
+ public static String recoverHyphen(@NotNull String key, Boolean upper) {
+ return recoverDivider(key, "-", upper);
+ }
+
+ /**ABCdEfg => upper ? A_B_CD_EFG : a_b_cd_efg
+ * @param key
+ * @return
+ */
+ public static String recoverUnderline(@NotNull String key, Boolean upper) {
+ return recoverDivider(key, "_", upper);
+ }
+
+ /**ABCdEfg => upper ? A$B$CD$EFG : a$b$cd$efg
+ * @param key
+ * @return
+ */
+ public static String recoverDollar(@NotNull String key, Boolean upper) {
+ return recoverDivider(key, "$", upper);
+ }
+
+ /**ABCdEfg => upper ? A.B.CD.EFG : a.b.cd.efg
+ * @param key
+ * @return
+ */
+ public static String recoverDot(@NotNull String key, Boolean upper) {
+ return recoverDivider(key, ".", upper);
+ }
+
+ /**ABCdEfg => upper ? A_B_CD_EFG : a/b/cd/efg
+ * @param key
+ * @return
+ */
+ public static String recoverDivider(@NotNull String key, Boolean upper) {
+ return recoverDivider(key, "/", upper);
+ }
+
+ /**驼峰格式转为带分隔符的全大写或全小写格式
+ * @param key
+ * @param divider
+ * @param upper
+ * @return
+ */
+ public static String recoverDivider(@NotNull String key, @NotNull String divider, Boolean upper) {
+ StringBuilder name = new StringBuilder();
+ char[] cs = key.toCharArray();
+ int len = key.length();
+ for (int i = 0; i < len; i++) { + String s = key.substring(i, i + 1); + if (i> 0 && PATTERN_ALPHA_BIG.matcher(s).matches()) {
+ name.append(divider);
+ }
+ if (upper != null) {
+ s = upper ? s.toUpperCase() : s.toLowerCase();
+ }
+ name.append(s);
+ }
+ return name.toString();
+ }
+
+
}
diff --git a/APIJSONORM/src/main/java/apijson/JSONResponse.java b/APIJSONORM/src/main/java/apijson/JSONResponse.java
index c69955c3f..7527adffb 100755
--- a/APIJSONORM/src/main/java/apijson/JSONResponse.java
+++ b/APIJSONORM/src/main/java/apijson/JSONResponse.java
@@ -1,16 +1,11 @@
-/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
+/*Copyright (C) 2020 Tencent. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson;
-import java.util.List;
-import java.util.Set;
-import java.util.StringTokenizer;
-
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
+import java.util.*;
/**parser for response
* @author Lemon
@@ -20,20 +15,36 @@
*
User user = response.getObject(User.class);//not a must
*
List commenntList = response.getList("Comment[]", Comment.class);//not a must
*/
-public class JSONResponse extends apijson.JSONObject {
- private static final long serialVersionUID = 1L;
+public interface JSONResponse, L extends List> extends JSONMap {
+ static final String TAG = "JSONResponse";
- private static final String TAG = "JSONResponse";
+ // 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
+ /**格式化带 - 中横线的单词
+ */
+ public static boolean IS_FORMAT_HYPHEN = false;
+ /**格式化带 _ 下划线的单词
+ */
+ public static boolean IS_FORMAT_UNDERLINE = false;
+ /**格式化带 $ 美元符的单词
+ */
+ public static boolean IS_FORMAT_DOLLAR = false;
- public JSONResponse() {
- super();
- }
- public JSONResponse(String json) {
- this(parseObject(json));
- }
- public JSONResponse(JSONObject object) {
- super(format(object));
- }
+
+ //default JSONResponse() {
+ // super();
+ //}
+ //default JSONResponse(Object json) {
+ // this(parseObject(json));
+ //}
+ //default JSONResponse(Object json, JSONParser parser) {
+ // this(parseObject(json, parser));
+ //}
+ //default JSONResponse(Map object) {
+ // super(format(object));
+ //}
+ //default JSONResponse(M object, JSONCreator creator) {
+ // super(format(object, creator));
+ //}
//状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @@ -71,9 +82,9 @@ public JSONResponse(JSONObject object) { /**获取状态 * @return */ - public int getCode() { + default int getCode() { try { - return getIntValue(KEY_CODE); + return JSON.getIntValue(this, KEY_CODE); } catch (Exception e) { //empty } @@ -82,9 +93,9 @@ public int getCode() { /**获取状态 * @return */ - public static int getCode(JSONObject reponse) { + public static int getCode(Map reponse) {
try {
- return reponse.getIntValue(KEY_CODE);
+ return JSON.getIntValue(reponse, KEY_CODE);
} catch (Exception e) {
//empty
}
@@ -93,22 +104,22 @@ public static int getCode(JSONObject reponse) {
/**获取状态描述
* @return
*/
- public String getMsg() {
- return getString(KEY_MSG);
+ default String getMsg() {
+ return JSON.getString(this, KEY_MSG);
}
/**获取状态描述
- * @param reponse
+ * @param response
* @return
*/
- public static String getMsg(JSONObject reponse) {
- return reponse == null ? null : reponse.getString(KEY_MSG);
+ public static String getMsg(Map response) {
+ return response == null ? null : JSON.getString(response, KEY_MSG);
}
/**获取id
* @return
*/
- public long getId() {
+ default long getId() {
try {
- return getLongValue(KEY_ID);
+ return JSON.getLongValue(this, getIdKey());
} catch (Exception e) {
//empty
}
@@ -117,9 +128,9 @@ public long getId() {
/**获取数量
* @return
*/
- public int getCount() {
+ default int getCount() {
try {
- return getIntValue(KEY_COUNT);
+ return JSON.getIntValue(this, KEY_COUNT);
} catch (Exception e) {
//empty
}
@@ -128,9 +139,9 @@ public int getCount() {
/**获取总数
* @return
*/
- public int getTotal() {
+ default int getTotal() {
try {
- return getIntValue(KEY_TOTAL);
+ return JSON.getIntValue(this, KEY_TOTAL);
} catch (Exception e) {
//empty
}
@@ -141,7 +152,7 @@ public int getTotal() {
/**是否成功
* @return
*/
- public boolean isSuccess() {
+ default boolean isSuccess() {
return isSuccess(getCode());
}
/**是否成功
@@ -155,21 +166,21 @@ public static boolean isSuccess(int code) {
* @param response
* @return
*/
- public static boolean isSuccess(JSONResponse response) {
+ public static boolean isSuccess(JSONResponse, ?> response) {
return response != null && response.isSuccess();
}
/**是否成功
* @param response
* @return
*/
- public static boolean isSuccess(JSONObject response) {
- return response != null && isSuccess(response.getIntValue(KEY_CODE));
- }
+ public static boolean isSuccess(Map response) {
+ return response != null && isSuccess(JSON.getIntValue(response, KEY_CODE));
+ }
/**校验服务端是否存在table
* @return
*/
- public boolean isExist() {
+ default boolean isExist() {
return isExist(getCount());
}
/**校验服务端是否存在table
@@ -183,39 +194,39 @@ public static boolean isExist(int count) {
* @param response
* @return
*/
- public static boolean isExist(JSONResponse response) {
+ public static boolean isExist(JSONResponse, ?> response) {
return response != null && response.isExist();
}
+ public static boolean isExist(Map response) {
+ return response != null && isExist(JSON.getIntValue(response, KEY_COUNT));
+ }
/**获取内部的JSONResponse
* @param key
* @return
*/
- public JSONResponse getJSONResponse(String key) {
+ default JSONResponse getJSONResponse(String key) {
return getObject(key, JSONResponse.class);
}
+
//cannot get javaBeanDeserizer
// /**获取内部的JSONResponse
// * @param response
// * @param key
// * @return
// */
- // public static JSONResponse getJSONResponse(JSONObject response, String key) {
+ // public static JSONResponse getJSONResponse(JSONRequest response, String key) {
// return response == null ? null : response.getObject(key, JSONResponse.class);
// }
//状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-
-
-
-
/**
* key = clazz.getSimpleName()
* @param clazz
* @return
*/
- public T getObject(Class clazz) {
+ default T getObject(Class clazz) {
return getObject(clazz == null ? "" : clazz.getSimpleName(), clazz);
}
/**
@@ -223,7 +234,7 @@ public T getObject(Class clazz) {
* @param clazz
* @return
*/
- public T getObject(String key, Class clazz) {
+ default T getObject(String key, Class clazz) {
return getObject(this, key, clazz);
}
/**
@@ -232,55 +243,47 @@ public T getObject(String key, Class clazz) {
* @param clazz
* @return
*/
- public static T getObject(JSONObject object, String key, Class clazz) {
- return toObject(object == null ? null : object.getJSONObject(formatObjectKey(key)), clazz);
+ public static T getObject(
+ Map object, String key, Class clazz) {
+ return toObject(object == null ? null : JSON.get(object, formatObjectKey(key)), clazz);
}
/**
* @param clazz
* @return
*/
- public T toObject(Class clazz) {
+ default T toObject(Class clazz) {
return toObject(this, clazz);
}
+
/**
* @param object
* @param clazz
* @return
*/
- public static T toObject(JSONObject object, Class clazz) {
- return JSON.parseObject(JSON.toJSONString(object), clazz);
+ public static , L extends List> T toObject(
+ Map object, Class clazz) {
+ return JSON.parseObject(object, clazz);
}
-
- /**
- * key = KEY_ARRAY
- * @param clazz
- * @return
- */
- public List getList(Class clazz) {
- return getList(KEY_ARRAY, clazz);
- }
/**
* arrayObject = this
* @param key
- * @param clazz
* @return
*/
- public List getList(String key, Class clazz) {
- return getList(this, key, clazz);
+ default List getList(String key) {
+ return JSON.getList(this, key);
}
/**
* key = KEY_ARRAY
* @param object
- * @param clazz
* @return
*/
- public static List getList(JSONObject object, Class clazz) {
- return getList(object, KEY_ARRAY, clazz);
+ public static List getList(Map object) {
+ return JSON.getList(object, KEY_ARRAY);
}
/**
* @param object
@@ -288,29 +291,29 @@ public static List getList(JSONObject object, Class clazz) {
* @param clazz
* @return
*/
- public static List getList(JSONObject object, String key, Class clazz) {
- return object == null ? null : JSON.parseArray(object.getString(formatArrayKey(key)), clazz);
+ public static > List getList(Map object, String key, Class clazz) {
+ return object == null ? null : JSON.parseArray(JSON.getString(object, formatArrayKey(key)), clazz);
}
/**
* key = KEY_ARRAY
* @return
*/
- public JSONArray getArray() {
+ default L getArray() {
return getArray(KEY_ARRAY);
}
/**
* @param key
* @return
*/
- public JSONArray getArray(String key) {
+ default L getArray(String key) {
return getArray(this, key);
}
/**
* @param object
* @return
*/
- public static JSONArray getArray(JSONObject object) {
+ public static L getArray(Map object) {
return getArray(object, KEY_ARRAY);
}
/**
@@ -319,28 +322,29 @@ public static JSONArray getArray(JSONObject object) {
* @param key
* @return
*/
- public static JSONArray getArray(JSONObject object, String key) {
- return object == null ? null : object.getJSONArray(formatArrayKey(key));
+ public static