Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fastjson2 #401

Merged
merged 3 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions APIJSONORM/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>apijson.orm</groupId>
<artifactId>apijson-orm</artifactId>
<version>5.1.0</version>
<version>5.1.0-F2</version>
<packaging>jar</packaging>

<name>APIJSONORM</name>
Expand All @@ -21,7 +21,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
Expand Down
40 changes: 7 additions & 33 deletions APIJSONORM/src/main/java/apijson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSONReader;

import java.util.List;

Expand Down Expand Up @@ -64,18 +64,18 @@ public static String getCorrectJson(String s, boolean isArray) {
* @param json
* @return
*/
private static final JSONReader.Feature[] DEFAULT_FASTJSON_FEATURES = {JSONReader.Feature.FieldBased, JSONReader.Feature.UseBigDecimalForDoubles};
public static Object parse(Object obj) {
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
features |= Feature.OrderedField.getMask();
try {
return com.alibaba.fastjson.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), features);
return com.alibaba.fastjson2.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), DEFAULT_FASTJSON_FEATURES);
} catch (Exception e) {
Log.i(TAG, "parse catch \n" + e.getMessage());
}
return null;
}

/**obj转JSONObject
* @param json
* @param obj
* @return
*/
public static JSONObject parseObject(Object obj) {
Expand All @@ -89,31 +89,7 @@ public static JSONObject parseObject(Object obj) {
* @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);
} catch (Exception e) {
Log.i(TAG, "parseObject catch \n" + e.getMessage());
}
return null;
}

/**JSONObject转实体类
* @param object
* @param clazz
* @return
*/
public static <T> T parseObject(JSONObject object, Class<T> clazz) {
return parseObject(toJSONString(object), clazz);
return parseObject(json, JSONObject.class);
}
/**json转实体类
* @param json
Expand All @@ -125,9 +101,7 @@ public static <T> T parseObject(String json, Class<T> clazz) {
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);
return com.alibaba.fastjson2.JSON.parseObject(getCorrectJson(json), clazz, DEFAULT_FASTJSON_FEATURES);
} catch (Exception e) {
Log.i(TAG, "parseObject catch \n" + e.getMessage());
}
Expand Down
42 changes: 20 additions & 22 deletions APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@

package apijson.orm;

import static apijson.JSONObject.KEY_COMBINE;
import static apijson.JSONObject.KEY_DROP;
import static apijson.JSONObject.KEY_TRY;
import static apijson.RequestMethod.POST;
import static apijson.RequestMethod.PUT;
import static apijson.orm.SQLConfig.TYPE_ITEM;
import apijson.JSONResponse;
import apijson.Log;
import apijson.NotNull;
import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.orm.AbstractFunctionParser.FunctionBean;
import apijson.orm.exception.ConflictException;
import apijson.orm.exception.NotExistException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.activation.UnsupportedDataTypeException;
import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -22,20 +28,12 @@
import java.util.Map.Entry;
import java.util.Set;

import javax.activation.UnsupportedDataTypeException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import apijson.JSONResponse;
import apijson.Log;
import apijson.NotNull;
import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.orm.AbstractFunctionParser.FunctionBean;
import apijson.orm.exception.ConflictException;
import apijson.orm.exception.NotExistException;
import static apijson.JSONObject.KEY_COMBINE;
import static apijson.JSONObject.KEY_DROP;
import static apijson.JSONObject.KEY_TRY;
import static apijson.RequestMethod.POST;
import static apijson.RequestMethod.PUT;
import static apijson.orm.SQLConfig.TYPE_ITEM;


/**简化Parser,getObject和getArray(getArrayConfig)都能用
Expand Down Expand Up @@ -427,7 +425,7 @@ else if (value instanceof String) { // //key{}@ getRealKey, 引用赋值路径
String replaceKey = key.substring(0, key.length() - 1);

// System.out.println("getObject key.endsWith(@) >> parseRelation = " + parseRelation);
String targetPath = AbstractParser.getValuePath(type == TYPE_ITEM ? path : parentPath, new String((String) value));
String targetPath = AbstractParser.getValuePath(type == TYPE_ITEM ? path : parentPath, (String) value);

//先尝试获取,尽量保留缺省依赖路径,这样就不需要担心路径改变
Object target = onReferenceParse(targetPath);
Expand Down Expand Up @@ -572,7 +570,7 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
invalidate();
}
}
Log.i(TAG, "onChildParse ObjectParser.onParse key = " + key + "; child = " + child);
// Log.i(TAG, "onChildParse ObjectParser.onParse key = " + key + "; child = " + child);

return isEmpty ? null : child;//只添加! isChildEmpty的值,可能数据库返回数据不够count
}
Expand Down