Skip to content

Commit

Permalink
Issue2535问题fixed
Browse files Browse the repository at this point in the history
mek1986 authored and wenshao committed May 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 4c6e194 commit 3198ef5
Showing 2 changed files with 129 additions and 26 deletions.
61 changes: 35 additions & 26 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
@@ -397,7 +397,7 @@ public final void handleResolveTasks(Object root) {
}

public final ObjectReader getObjectReader(Type type) {
boolean fieldBased = (context.features & JSONReader.Feature.FieldBased.mask) != 0;
boolean fieldBased = (context.features & Feature.FieldBased.mask) != 0;
return context.provider.getObjectReader(type, fieldBased);
}

@@ -1913,7 +1913,7 @@ public void read(Map object, ObjectReader itemReader, long features) {

Object origin = map.put(name, value);
if (origin != null) {
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if ((contextFeatures & Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
map.put(name, origin);
@@ -2106,7 +2106,7 @@ public void read(Map object, long features) {

Object origin = map.put(name, value);
if (origin != null) {
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if ((contextFeatures & Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
map.put(name, origin);
@@ -2162,7 +2162,7 @@ public final void read(Map object, Type keyType, Type valueType, long features)

Object origin = object.put(name, value);
if (origin != null) {
if ((contextFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if ((contextFeatures & Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(value);
object.put(name, origin);
@@ -2293,7 +2293,7 @@ public Map<String, Object> readObject() {

Object origin = object.put(name, val);
if (origin != null) {
if ((context.features & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
if ((context.features & Feature.DuplicateKeyValueAsArray.mask) != 0) {
if (origin instanceof Collection) {
((Collection) origin).add(val);
object.put(name, origin);
@@ -2339,21 +2339,29 @@ public List readArray(Type itemType) {
return null;
}

if (!nextIfArrayStart()) {
throw new JSONException(info("syntax error : " + ch));
}

boolean fieldBased = (context.features & Feature.FieldBased.mask) != 0;
ObjectReader objectReader = context.provider.getObjectReader(itemType, fieldBased);

List list = new ArrayList();
for (Object item; !nextIfArrayEnd(); list.add(item)) {
int mark = offset;
item = objectReader.readObject(this, null, null, 0);
if (ch == '[') {
if (!nextIfArrayStart()) {
throw new JSONException(info("syntax error : " + ch));
}

if (mark == offset || ch == '}' || ch == EOI) {
throw new JSONException("illegal input : " + ch + ", offset " + getOffset());
boolean fieldBased = (context.features & Feature.FieldBased.mask) != 0;
ObjectReader objectReader = context.provider.getObjectReader(itemType, fieldBased);
for (Object item; !nextIfArrayEnd(); list.add(item)) {
int mark = offset;
item = objectReader.readObject(this, null, null, 0);

if (mark == offset || ch == '}' || ch == EOI) {
throw new JSONException("illegal input : " + ch + ", offset " + getOffset());
}
}
} else if (ch == '"' || ch == '\'' || ch == '{') {
String str = readString();
if (str != null && !str.isEmpty()) {
list.add(str);
}
} else {
throw new JSONException(info("syntax error"));
}

if (comma = (ch == ',')) {
@@ -3118,7 +3126,7 @@ public static JSONReader of(byte[] utf8Bytes) {
}

@Deprecated
public static JSONReader of(JSONReader.Context context, byte[] utf8Bytes) {
public static JSONReader of(Context context, byte[] utf8Bytes) {
boolean ascii = false;
if (PREDICATE_IS_ASCII != null) {
ascii = PREDICATE_IS_ASCII.test(utf8Bytes);
@@ -3139,7 +3147,7 @@ public static JSONReader of(JSONReader.Context context, byte[] utf8Bytes) {
}
}

public static JSONReader of(byte[] utf8Bytes, JSONReader.Context context) {
public static JSONReader of(byte[] utf8Bytes, Context context) {
boolean ascii = false;
if (PREDICATE_IS_ASCII != null) {
ascii = PREDICATE_IS_ASCII.test(utf8Bytes);
@@ -3219,27 +3227,27 @@ public static JSONReader ofJSONB(byte[] jsonbBytes) {
}

@Deprecated
public static JSONReader ofJSONB(JSONReader.Context context, byte[] jsonbBytes) {
public static JSONReader ofJSONB(Context context, byte[] jsonbBytes) {
return new JSONReaderJSONB(
context,
jsonbBytes,
0,
jsonbBytes.length);
}

public static JSONReader ofJSONB(byte[] jsonbBytes, JSONReader.Context context) {
public static JSONReader ofJSONB(byte[] jsonbBytes, Context context) {
return new JSONReaderJSONB(
context,
jsonbBytes,
0,
jsonbBytes.length);
}

public static JSONReader ofJSONB(InputStream in, JSONReader.Context context) {
public static JSONReader ofJSONB(InputStream in, Context context) {
return new JSONReaderJSONB(context, in);
}

public static JSONReader ofJSONB(byte[] jsonbBytes, JSONReader.Feature... features) {
public static JSONReader ofJSONB(byte[] jsonbBytes, Feature... features) {
Context context = JSONFactory.createReadContext();
context.config(features);
return new JSONReaderJSONB(
@@ -3420,14 +3428,14 @@ public static JSONReader of(InputStream is, Charset charset, Context context) {
return JSONReader.of(new InputStreamReader(is, charset), context);
}

public static JSONReader of(java.io.Reader is) {
public static JSONReader of(Reader is) {
return new JSONReaderUTF16(
JSONFactory.createReadContext(),
is
);
}

public static JSONReader of(java.io.Reader is, Context context) {
public static JSONReader of(Reader is, Context context) {
return new JSONReaderUTF16(
context,
is
@@ -4464,7 +4472,8 @@ public enum Feature {
ErrorOnUnknownProperties(1 << 26),

/**
* empty string "" convert to null
* empty string "" convert to null
*
* @since 2.0.48
*/
EmptyStringAsNull(1 << 27),
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.alibaba.fastjson2.issues_2500;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class Issue2535 {
@Test
public void test1() {
String json = "{}";
List<String> strings = JSON.parseArray(json, String.class);
assertNotNull(strings);
assertEquals(strings.size(), 1);
assertEquals(strings.get(0), "{}");
}

@Test
public void test2() {
String json = "\"11111\"";
List<String> strings = JSON.parseArray(json, String.class);
assertNotNull(strings);
assertEquals(strings.size(), 1);
assertEquals(strings.get(0), "11111");
}

@Test
public void test3() {
String json = "'22222'";
List<String> strings = JSON.parseArray(json, String.class);
assertNotNull(strings);
assertEquals(strings.size(), 1);
assertEquals(strings.get(0), "22222");
}

@Test
public void test4() {
String json = "[]";
List<String> strings = JSON.parseArray(json, String.class);
assertNotNull(strings);
assertEquals(strings.size(), 0);
}

@Test
public void test5() {
String json = "11\"11111\"";
assertThrows(JSONException.class, () -> {
JSON.parseArray(json, String.class);
});
}

@Test
public void test6() {
String json = "22'22222'";
assertThrows(JSONException.class, () -> {
JSON.parseArray(json, String.class);
});
}

@Test
public void test7() {
String json = "1{}";
assertThrows(JSONException.class, () -> {
JSON.parseArray(json, String.class);
});
}

@Test
public void test8() {
String json = "a{}";
assertThrows(JSONException.class, () -> {
JSON.parseArray(json, String.class);
});
}

@Test
public void test9() {
String json = "a'333'";
assertThrows(JSONException.class, () -> {
JSON.parseArray(json, String.class);
});
}

@Test
public void test10() {
String json = "a\"444\"";
assertThrows(JSONException.class, () -> {
JSON.parseArray(json, String.class);
});
}
}

0 comments on commit 3198ef5

Please sign in to comment.