Skip to content

Commit

Permalink
Improve some code details, fix some bugs (#3161)
Browse files Browse the repository at this point in the history
* refactor: reuse some methods

* refactor: remove unused field

* refactor: simplify some code

* fix: a potential bug

* style: add .gitattributes for code style

* fix: issue#2848 and issue#2988
  • Loading branch information
CodePlayer authored Nov 17, 2024
1 parent 2da286c commit 6c68b0e
Show file tree
Hide file tree
Showing 40 changed files with 239 additions and 374 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
1 change: 0 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,6 @@ public byte[] toJSONBBytes(JSONWriter.Feature... features) {
* @param type specify the {@link Type} to be converted
* @since 2.0.4
*/
@SuppressWarnings("unchecked")
public <T> T to(Type type) {
return to(type, 0L);
}
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONBDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static com.alibaba.fastjson2.JSONB.Constants.*;
import static com.alibaba.fastjson2.JSONB.typeName;
import static com.alibaba.fastjson2.util.JDKUtils.*;
import static com.alibaba.fastjson2.util.JDKUtils.BIG_ENDIAN;

final class JSONBDump {
static Charset GB18030;
Expand Down
186 changes: 34 additions & 152 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.alibaba.fastjson2.JSONWriter.Feature.*;
import static com.alibaba.fastjson2.util.BeanUtils.getAnnotations;
Expand Down Expand Up @@ -542,31 +540,7 @@ public Long getLong(String key) {
* @throws JSONException Unsupported type conversion to long value
*/
public long getLongValue(String key) {
Object value = super.get(key);

if (value == null) {
return 0;
}

if (value instanceof Number) {
return ((Number) value).longValue();
}

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0;
}

if (str.indexOf('.') != -1) {
return (long) Double.parseDouble(str);
}

return Long.parseLong(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to long value");
return getLongValue(key, 0);
}

/**
Expand Down Expand Up @@ -659,31 +633,7 @@ public Integer getInteger(String key) {
* @throws JSONException Unsupported type conversion to int value
*/
public int getIntValue(String key) {
Object value = super.get(key);

if (value == null) {
return 0;
}

if (value instanceof Number) {
return ((Number) value).intValue();
}

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0;
}

if (str.indexOf('.') != -1) {
return (int) Double.parseDouble(str);
}

return Integer.parseInt(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to int value");
return getIntValue(key, 0);
}

/**
Expand Down Expand Up @@ -756,7 +706,7 @@ public Short getShort(String key) {
return Short.parseShort(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to Short");
throw new JSONException("Can not cast '" + value.getClass() + "' to short");
}

/**
Expand All @@ -768,27 +718,8 @@ public Short getShort(String key) {
* @throws JSONException Unsupported type conversion to short value
*/
public short getShortValue(String key) {
Object value = super.get(key);

if (value == null) {
return 0;
}

if (value instanceof Number) {
return ((Number) value).shortValue();
}

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0;
}

return Short.parseShort(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to short value");
Short value = getShort(key);
return value == null ? 0 : value;
}

/**
Expand Down Expand Up @@ -820,7 +751,7 @@ public Byte getByte(String key) {
return Byte.parseByte(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to Byte");
throw new JSONException("Can not cast '" + value.getClass() + "' to byte");
}

/**
Expand All @@ -832,27 +763,8 @@ public Byte getByte(String key) {
* @throws JSONException Unsupported type conversion to byte value
*/
public byte getByteValue(String key) {
Object value = super.get(key);

if (value == null) {
return 0;
}

if (value instanceof Number) {
return ((Number) value).byteValue();
}

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0;
}

return Byte.parseByte(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to byte value");
Byte value = getByte(key);
return value == null ? 0 : value;
}

public byte[] getBytes(String key) {
Expand Down Expand Up @@ -903,7 +815,7 @@ public Boolean getBoolean(String key) {
return "true".equalsIgnoreCase(str) || "1".equals(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to Boolean");
throw new JSONException("Can not cast '" + value.getClass() + "' to boolean");
}

/**
Expand All @@ -914,26 +826,8 @@ public Boolean getBoolean(String key) {
* @throws JSONException Unsupported type conversion to boolean value
*/
public boolean getBooleanValue(String key) {
Object value = super.get(key);

if (value == null) {
return false;
}

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

if (value instanceof Number) {
return ((Number) value).intValue() == 1;
}

if (value instanceof String) {
String str = (String) value;
return "true".equalsIgnoreCase(str) || "1".equals(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to boolean value");
Boolean value = getBoolean(key);
return value != null && value;
}

/**
Expand All @@ -945,26 +839,8 @@ public boolean getBooleanValue(String key) {
* @throws JSONException Unsupported type conversion to boolean value
*/
public boolean getBooleanValue(String key, boolean defaultValue) {
Object value = super.get(key);

if (value == null) {
return defaultValue;
}

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

if (value instanceof Number) {
return ((Number) value).intValue() == 1;
}

if (value instanceof String) {
String str = (String) value;
return "true".equalsIgnoreCase(str) || "1".equals(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to boolean value");
Boolean value = getBoolean(key);
return value == null ? defaultValue : value;
}

/**
Expand Down Expand Up @@ -2075,29 +1951,35 @@ public static JSONObject of(
* @param kvArray key-value
* @since 2.0.53
*/
private static JSONObject of(JSONObject object, Object... kvArray) {
if (kvArray == null || kvArray.length <= 0) {
private static JSONObject of(JSONObject jsonObject, Object... kvArray) {
if (kvArray == null || kvArray.length == 0) {
throw new JSONException("The kvArray cannot be empty");
}
int kvArrayLength = kvArray.length;
final int kvArrayLength = kvArray.length;
if ((kvArrayLength & 1) == 1) {
throw new JSONException("The length of kvArray cannot be odd");
}
List<Object> keyList = IntStream.range(0, kvArrayLength).filter(i -> i % 2 == 0).mapToObj(i -> kvArray[i]).collect(Collectors.toList());
keyList.forEach(key -> {
if (key == null || !(key instanceof String)) {
boolean valueMaybeNull = false;
for (int i = 0; i < kvArrayLength; i++) {
Object keyObj = kvArray[i++];
if (!(keyObj instanceof String)) {
throw new JSONException("The value corresponding to the even bit index of kvArray is key, which cannot be null and must be of type string");
}
});
List<Object> distinctKeyList = keyList.stream().distinct().collect(Collectors.toList());
if (keyList.size() != distinctKeyList.size()) {
throw new JSONException("The value corresponding to the even bit index of kvArray is key and cannot be duplicated");
}
List<Object> valueList = IntStream.range(0, kvArrayLength).filter(i -> i % 2 != 0).mapToObj(i -> kvArray[i]).collect(Collectors.toList());
for (int i = 0; i < keyList.size(); i++) {
object.put(keyList.get(i).toString(), valueList.get(i));
String key = (String) keyObj;
if (valueMaybeNull) {
if (jsonObject.containsKey(key)) {
throw new JSONException("The value corresponding to the even bit index of kvArray is key and cannot be duplicated");
}
jsonObject.put(key, kvArray[i]);
} else {
Object old = jsonObject.put(key, kvArray[i]);
if (old != null) {
throw new JSONException("The value corresponding to the even bit index of kvArray is key and cannot be duplicated");
}
valueMaybeNull = kvArray[i] == null;
}
}
return object;
return jsonObject;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,7 @@ public Object apply(Object o) {
}

if (o.getClass().isArray()) {
int len = Array.getLength(o);
for (int i = 0; i < len; i++) {
return Array.get(o, i);
}
return Array.get(o, index);
}

return null;
Expand Down
16 changes: 7 additions & 9 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public final String readFieldNameUnquote() {
}
readFieldNameHashCodeUnquote();
String name = getFieldName();
if (name == null || name.equals("")) {
if (name == null || name.isEmpty()) {
throw new JSONException(info("illegal input"));
}
return name;
Expand Down Expand Up @@ -899,6 +899,9 @@ public final long getInt64Value() {
}
return number.longValue();
case JSON_TYPE_DEC:
case JSON_TYPE_INT64:
case JSON_TYPE_FLOAT:
case JSON_TYPE_DOUBLE:
return getNumber().longValue();
case JSON_TYPE_BOOL:
return boolValue ? 1 : 0;
Expand All @@ -916,11 +919,6 @@ public final long getInt64Value() {
case JSON_TYPE_ARRAY: {
return toInt((List) complex);
}
case JSON_TYPE_INT64:
case JSON_TYPE_FLOAT:
case JSON_TYPE_DOUBLE:
return getNumber()
.longValue();
case JSON_TYPE_BIG_DEC:
try {
return getBigDecimal()
Expand Down Expand Up @@ -1829,7 +1827,7 @@ public Character readCharacter() {
wasNull = true;
return '\0';
}
return Character.valueOf(str.charAt(0));
return str.charAt(0);
}

public abstract void readNull();
Expand Down Expand Up @@ -2823,7 +2821,7 @@ public final Number getNumber() {
}

if ((context.features & Feature.UseLongForInts.mask) != 0) {
return Long.valueOf(intValue);
return (long) intValue;
}

if (valueType == JSON_TYPE_INT64) {
Expand Down Expand Up @@ -4661,7 +4659,7 @@ static JSONException numberError(int offset, int ch) {
}

JSONException numberError() {
return new JSONException("illegal number, offset " + offset + ", char " + (char) ch);
return new JSONException("illegal number, offset " + offset + ", char " + ch);
}

public final String info() {
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ public final long readFieldNameHashCodeUnquote() {
for (int i = 0; ; ++i) {
if (ch == '\\') {
nameEscape = true;
ch = (char) chars[offset++];
ch = chars[offset++];
switch (ch) {
case 'u': {
ch = char4(chars[offset], chars[offset + 1], chars[offset + 2], chars[offset + 3]);
Expand Down Expand Up @@ -3279,7 +3279,7 @@ private void skipString() {
ch = chars[offset];
}
offset++;
} else if (ch != EOI && ch != '}' && ch != ']' && ch != EOI) {
} else if (ch != '}' && ch != ']' && ch != EOI) {
throw error(offset, ch);
}

Expand Down Expand Up @@ -4165,7 +4165,7 @@ public final Date readNullOrNewDate() {
Date date = null;
final char[] chars = this.chars;
int offset = this.offset;
char ch = this.ch;
char ch;
if (offset + 2 < end
&& chars[offset] == 'u'
&& chars[offset + 1] == 'l'
Expand Down Expand Up @@ -4317,7 +4317,7 @@ public final BigDecimal readBigDecimal() {
ch = chars[offset++];

if (ch == quote) {
this.ch = offset == end ? EOI : (char) chars[offset++];
this.ch = offset == end ? EOI : chars[offset++];
this.offset = offset;
nextIfComma();
return null;
Expand Down
Loading

0 comments on commit 6c68b0e

Please sign in to comment.