Skip to content

Commit

Permalink
Fix2997&2998 (#2999)
Browse files Browse the repository at this point in the history
* fix issue #2997 throws NullPointerException

* fix issue #2998 fastjson-compatible parseObject throws fastjson2.JSONException
  • Loading branch information
Cooper-Zhong authored Oct 7, 2024
1 parent 3ea0abc commit 657ca20
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 32 deletions.
60 changes: 28 additions & 32 deletions fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,8 @@ public static <T> T parseObject(
context.config(processor);
}

JSONReader jsonReader = JSONReader.of(bytes, 0, bytes.length, charset, context);

try {
JSONReader jsonReader = JSONReader.of(bytes, 0, bytes.length, charset, context);
ObjectReader<T> objectReader = jsonReader.getObjectReader(objectClass);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand Down Expand Up @@ -320,9 +319,8 @@ public static <T> T parseObject(
context.config(processor);
}

JSONReader jsonReader = JSONReader.of(bytes, offset, len, charset, context);

try {
JSONReader jsonReader = JSONReader.of(bytes, offset, len, charset, context);
ObjectReader<T> objectReader = jsonReader.getObjectReader(objectType);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand Down Expand Up @@ -698,9 +696,8 @@ public static <T> T parseObject(
context.config(processor);
}

JSONReader jsonReader = JSONReader.of(is, charset, context);

try {
JSONReader jsonReader = JSONReader.of(is, charset, context);
ObjectReader<T> objectReader = jsonReader.getObjectReader(objectType);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand Down Expand Up @@ -743,9 +740,8 @@ public static <T> T parseObject(
DEFAULT_PARSER_FEATURE,
features
);
JSONReader jsonReader = JSONReader.of(is, charset, context);

try {
JSONReader jsonReader = JSONReader.of(is, charset, context);
ObjectReader<T> objectReader = jsonReader.getObjectReader(objectType);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand Down Expand Up @@ -777,17 +773,15 @@ public static <T> JSONObject parseObject(byte[] jsonBytes, Feature... features)
DEFAULT_PARSER_FEATURE,
features
);
JSONReader reader = JSONReader.of(jsonBytes, context);

boolean ordered = false;
for (Feature feature : features) {
if (feature == Feature.OrderedField) {
ordered = true;
break;
}
}

try {
JSONReader reader = JSONReader.of(jsonBytes, context);
Map<String, Object> map = ordered ? new LinkedHashMap<>() : new HashMap<>();
reader.read(map, 0);
JSONObject jsonObject = new JSONObject(map);
Expand Down Expand Up @@ -815,9 +809,8 @@ public static <T> T parseObject(byte[] jsonBytes, Class<T> type, Feature... feat
DEFAULT_PARSER_FEATURE,
features
);
JSONReader jsonReader = JSONReader.of(jsonBytes, context);

try {
JSONReader jsonReader = JSONReader.of(jsonBytes, context);
ObjectReader<T> objectReader = jsonReader.getObjectReader(type);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand Down Expand Up @@ -846,9 +839,8 @@ public static <T> T parseObject(byte[] jsonBytes, Type type, Feature... features
DEFAULT_PARSER_FEATURE,
features
);
JSONReader jsonReader = JSONReader.of(jsonBytes, context);

try {
JSONReader jsonReader = JSONReader.of(jsonBytes, context);
ObjectReader<T> objectReader = jsonReader.getObjectReader(type);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand All @@ -871,10 +863,8 @@ public static <T> T parseObject(byte[] jsonBytes, Type type, JSONReader.Context
if (jsonBytes == null) {
return null;
}

JSONReader jsonReader = JSONReader.of(jsonBytes, context);

try {
JSONReader jsonReader = JSONReader.of(jsonBytes, context);
ObjectReader<T> objectReader = jsonReader.getObjectReader(type);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand Down Expand Up @@ -903,13 +893,13 @@ public static <T> T parseObject(byte[] jsonBytes, Type type, SerializeFilter fil
DEFAULT_PARSER_FEATURE,
features
);
JSONReader jsonReader = JSONReader.of(jsonBytes, context);
try {
JSONReader jsonReader = JSONReader.of(jsonBytes, context);

if (filter instanceof Filter) {
context.config((Filter) filter);
}
if (filter instanceof Filter) {
context.config((Filter) filter);
}

try {
ObjectReader<T> objectReader = jsonReader.getObjectReader(type);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
Expand Down Expand Up @@ -1173,16 +1163,19 @@ public static <T> T parseObject(
DEFAULT_PARSER_FEATURE,
features
);
JSONReader jsonReader = JSONReader.of(chars, 0, position, context);

T object = jsonReader.read(clazz);
if (object != null) {
jsonReader.handleResolveTasks(object);
}
if (!jsonReader.isEnd()) {
throw new JSONException(jsonReader.info("input not end"));
try (JSONReader jsonReader = JSONReader.of(chars, 0, position, context)) {
T object = jsonReader.read(clazz);
if (object != null) {
jsonReader.handleResolveTasks(object);
}
if (!jsonReader.isEnd()) {
throw new JSONException(jsonReader.info("input not end"));
}
return object;
} catch (com.alibaba.fastjson2.JSONException ex) {
Throwable cause = ex.getCause() != null ? ex.getCause() : ex;
throw new JSONException(ex.getMessage(), cause);
}
return object;
} finally {
if (chars.length <= 1024 * 64) {
CHARS_UPDATER.set(CACHE, chars);
Expand Down Expand Up @@ -1218,6 +1211,9 @@ public static <T> T parseObject(
throw new JSONException(jsonReader.info("input not end"));
}
return object;
} catch (com.alibaba.fastjson2.JSONException ex) {
Throwable cause = ex.getCause() != null ? ex.getCause() : ex;
throw new JSONException(ex.getMessage(), cause);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.ParseProcess;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson2.JSONReader;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;

public class Issue2998 {
private final String[] binaryArray = {
"11111100", "00010100", "11100001", "11000100", "00100110",
};

private byte[] getByteArray() {
byte[] byteArray = new byte[binaryArray.length];
for (int i = 0; i < binaryArray.length; i++) {
byteArray[i] = (byte) Integer.parseInt(binaryArray[i], 2);
}
return byteArray;
}

private final byte[] byteArray = getByteArray();

// 1. parseObject(byte[], int, int, Charset, Type, Feature...)
@Test
public void testParseObject_Charset() {
try {
JSON.parseObject(byteArray, 0, byteArray.length, StandardCharsets.UTF_8, Object.class, Feature.AllowComment);
} catch (JSONException e) {
}
}

@Test
public void testParseObject_byteArray_Type_Features() {
try {
Type type = JSONObject.class;
JSON.parseObject(byteArray, type, Feature.SupportAutoType);
} catch (JSONException e) {
}
}

// 2. parseObject(byte[], int, int, CharsetDecoder, Type, Feature...)
@Test
public void testParseObject_CharsetDecoder() {
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
try {
JSON.parseObject(byteArray, 0, byteArray.length, decoder, Object.class, Feature.AllowComment);
} catch (JSONException e) {
}
}

// 3. parseObject(byte[], Type, SerializeFilter, Feature...)
@Test
public void testParseObject_SerializeFilter() {
SerializeFilter filter = null;
try {
JSON.parseObject(byteArray, Object.class, filter, Feature.AllowComment);
} catch (JSONException e) {
}
}

// 4. parseObject(byte[], Type, JSONReader.Context)
@Test
public void testParseObject_JSONReaderContext() {
JSONReader.Context context = null; // Customize context if needed
try {
JSON.parseObject(byteArray, Object.class, context);
} catch (JSONException e) {
}
}

// 5. parseObject(byte[], Type, Feature...)
@Test
public void testParseObject_Type_Feature() {
try {
JSON.parseObject(byteArray, Object.class, Feature.AllowComment);
} catch (JSONException e) {
}
}

// 6. parseObject(byte[], Class<T>, Feature...)
@Test
public void testParseObject_Class_Feature() {
try {
JSON.parseObject(byteArray, Object.class, Feature.AllowComment);
} catch (JSONException e) {
}
}

// 7. parseObject(byte[], Feature...)
@Test
public void testParseObject_JSONObject() {
try {
JSON.parseObject(byteArray, Feature.AllowComment);
} catch (JSONException e) {
}
}

// 8. parseObject(byte[], int, int, Charset, Type, ParserConfig, ParseProcess, int, Feature...)
@Test
public void testParseObject_ParserConfig_ParseProcess() {
ParserConfig config = ParserConfig.getGlobalInstance();
ParseProcess processor = null; // Customize processor if needed
try {
JSON.parseObject(byteArray, 0, byteArray.length, StandardCharsets.UTF_8, Object.class, config, processor, 0, Feature.AllowComment);
} catch (JSONException e) {
}
}

// 9. parseObject(byte[], Charset, Type, ParserConfig, ParseProcess, int, Feature...)
@Test
public void testParseObject_Charset_Config() {
ParserConfig config = ParserConfig.getGlobalInstance();
ParseProcess processor = null;
try {
JSON.parseObject(byteArray, StandardCharsets.UTF_8, Object.class, config, processor, 0, Feature.AllowComment);
} catch (JSONException e) {
}
}

// 10. parseObject(InputStream, Type, Feature...) throws IOException
@Test
public void testParseObject_InputStream_Type() throws IOException {
InputStream inputStream = new ByteArrayInputStream(byteArray);
try {
JSON.parseObject(inputStream, Object.class, Feature.AllowComment);
} catch (JSONException e) {
} finally {
inputStream.close();
}
}

// 11. parseObject(InputStream, Class<T>, Feature...) throws IOException
@Test
public void testParseObject_InputStream_Class() throws IOException {
InputStream inputStream = new ByteArrayInputStream(byteArray);
try {
JSON.parseObject(inputStream, Object.class, Feature.AllowComment);
} catch (JSONException e) {
} finally {
inputStream.close();
}
}

// 12. parseObject(InputStream, Charset, Type, ParserConfig, ParseProcess, int, Feature...) throws IOException
@Test
public void testParseObject_InputStream_Charset_Config() throws IOException {
InputStream inputStream = new ByteArrayInputStream(byteArray);
ParserConfig config = ParserConfig.getGlobalInstance();
ParseProcess processor = null;
try {
JSON.parseObject(inputStream, StandardCharsets.UTF_8, Object.class, config, processor, 0, Feature.AllowComment);
} catch (JSONException e) {
} finally {
inputStream.close();
}
}

// 13. parseObject(InputStream, Charset, Type, ParserConfig, Feature...) throws IOException
@Test
public void testParseObject_InputStream_Charset() throws IOException {
InputStream inputStream = new ByteArrayInputStream(byteArray);
ParserConfig config = ParserConfig.getGlobalInstance();
try {
JSON.parseObject(inputStream, StandardCharsets.UTF_8, Object.class, config, Feature.AllowComment);
} catch (JSONException e) {
} finally {
inputStream.close();
}
}

// 14. parseObject(InputStream, Charset, Type, Feature...) throws IOException
@Test
public void testParseObject_InputStream_Charset_Feature() throws IOException {
InputStream inputStream = new ByteArrayInputStream(byteArray);
try {
JSON.parseObject(inputStream, StandardCharsets.UTF_8, Object.class, Feature.AllowComment);
} catch (JSONException e) {
} finally {
inputStream.close();
}
}
}

0 comments on commit 657ca20

Please sign in to comment.