Skip to content

Commit

Permalink
优化部分代码实现细节
Browse files Browse the repository at this point in the history
  • Loading branch information
CodePlayer committed Mar 10, 2016
1 parent 6226d9b commit 4e96484
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public abstract class JSON implements JSONStreamAware, JSONAware {
// com.alibaba.fastjson.serializer.SerializerFeature.WriteSlashAsSpecial.getMask();
DEFAULT_GENERATE_FEATURE = features;
}

/** 长度为 0 的 SerializeFilter 数组 */
public static final SerializeFilter[] EMPTY_SERIALIZE_FILTERS = new SerializeFilter[0];

public static Object parse(String text) {
return parse(text, DEFAULT_PARSER_FEATURE);
Expand Down Expand Up @@ -379,7 +382,7 @@ public static List<Object> parseArray(String text, Type[] types) {

// ======================
public static String toJSONString(Object object) {
return toJSONString(object, new SerializerFeature[0]);
return toJSONString(object, EMPTY_SERIALIZE_FILTERS);
}

public static String toJSONString(Object object, SerializerFeature... features) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public byte getByteValue(String key) {
return 0;
}

return castToByte(value).byteValue();
return castToByte(value).byteValue(); // TODO 如果 value 是""、"null"或"NULL",可能会存在报空指针的情况,是否需要加以处理? 其他转换也存在类似情况
}

public Short getShort(String key) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/JSONPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ String readName() {
throw new JSONPathException("illeal jsonpath syntax. " + path);
}

StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
while (!isEOF()) {
if (ch == '\\') {
next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, O
}

if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
StringBuffer buf = (new StringBuffer()) //
StringBuilder buf = new StringBuilder(72) // 单线程换StringBuilder,并设置初始化容量,避免内部逐步扩容开销
.append("syntax error, expect {, actual ") //
.append(lexer.tokenName()) //
.append(", pos ") //
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/alibaba/fastjson/util/ASMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public static boolean isAndroid() {
}

public static String getDesc(Method method) {
StringBuffer buf = new StringBuffer();
buf.append("(");
java.lang.Class<?>[] types = method.getParameterTypes();
java.lang.Class<?>[] types = method.getParameterTypes();
StringBuilder buf = new StringBuilder((types.length + 1) << 4);
buf.append('(');
for (int i = 0; i < types.length; ++i) {
buf.append(getDesc(types[i]));
}
buf.append(")");
buf.append(')');
buf.append(getDesc(method.getReturnType()));
return buf.toString();
}
Expand All @@ -60,7 +60,7 @@ public static String getType(Class<?> parameterType) {
} else {
if (!parameterType.isPrimitive()) {
String clsName = parameterType.getName();
return clsName.replaceAll("\\.", "/");
return clsName.replace('.', '/'); // 直接基于字符串替换,不使用正则替换
} else {
return getPrimitiveLetter(parameterType);
}
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ public static Character castToChar(Object value) {
}

if (strVal.length() != 1) {
throw new JSONException("can not cast to byte, value : " + value);
throw new JSONException("can not cast to char, value : " + value);
}

return strVal.charAt(0);
}

throw new JSONException("can not cast to byte, value : " + value);
throw new JSONException("can not cast to char, value : " + value);
}

public static Short castToShort(Object value) {
Expand Down Expand Up @@ -271,15 +271,15 @@ public static Date castToDate(Object value) {
if (value == null) {
return null;
}

if (value instanceof Date) { // 使用频率最高的,应优先处理
return (Date) value;
}

if (value instanceof Calendar) {
return ((Calendar) value).getTime();
}

if (value instanceof Date) {
return (Date) value;
}

long longValue = -1;

if (value instanceof Number) {
Expand Down Expand Up @@ -369,17 +369,17 @@ public static java.sql.Date castToSqlDate(Object value) {
if (value == null) {
return null;
}

if (value instanceof Calendar) {
return new java.sql.Date(((Calendar) value).getTimeInMillis());
}


if (value instanceof java.sql.Date) {
return (java.sql.Date) value;
return (java.sql.Date) value;
}

if (value instanceof java.util.Date) {
return new java.sql.Date(((java.util.Date) value).getTime());
return new java.sql.Date(((java.util.Date) value).getTime());
}

if (value instanceof Calendar) {
return new java.sql.Date(((Calendar) value).getTimeInMillis());
}

long longValue = 0;
Expand All @@ -398,7 +398,7 @@ public static java.sql.Date castToSqlDate(Object value) {
}

if (longValue <= 0) {
throw new JSONException("can not cast to Date, value : " + value);
throw new JSONException("can not cast to Date, value : " + value); // TODO 忽略 1970-01-01 之前的时间处理?
}

return new java.sql.Date(longValue);
Expand Down Expand Up @@ -503,10 +503,6 @@ public static Integer castToInt(Object value) {
return null;
}

if ("null".equals(strVal)) {
return null;
}

if ("null".equals(strVal) || "NULL".equals(strVal)) {
return null;
}
Expand Down

0 comments on commit 4e96484

Please sign in to comment.