Skip to content

Commit 10e4381

Browse files
committed
bug fix for parse from single item array, for issue #3276
1 parent 3cb9068 commit 10e4381

File tree

5 files changed

+56
-42
lines changed

5 files changed

+56
-42
lines changed

core/src/main/java/com/alibaba/fastjson2/JSONReader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,8 +4749,12 @@ public ObjectReader getObjectReaderAutoType(long typeHash, Class expectClass, lo
47494749
protected final String readStringNotMatch() {
47504750
switch (ch) {
47514751
case '[':
4752-
return toString(
4753-
readArray());
4752+
List array = readArray();
4753+
if (array.size() == 1) {
4754+
Object item = array.get(0);
4755+
return item == null ? null : item.toString();
4756+
}
4757+
return toString(array);
47544758
case '{':
47554759
return toString(
47564760
readObject());

core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,39 +5004,7 @@ public String readString() {
50045004
return str;
50055005
}
50065006

5007-
switch (ch) {
5008-
case '[':
5009-
return toString(
5010-
readArray());
5011-
case '{':
5012-
return toString(
5013-
readObject());
5014-
case '-':
5015-
case '+':
5016-
case '0':
5017-
case '1':
5018-
case '2':
5019-
case '3':
5020-
case '4':
5021-
case '5':
5022-
case '6':
5023-
case '7':
5024-
case '8':
5025-
case '9':
5026-
readNumber0();
5027-
Number number = getNumber();
5028-
return number.toString();
5029-
case 't':
5030-
case 'f':
5031-
boolValue = readBoolValue();
5032-
return boolValue ? "true" : "false";
5033-
case 'n': {
5034-
readNull();
5035-
return null;
5036-
}
5037-
default:
5038-
throw new JSONException("TODO : " + ch);
5039-
}
5007+
return readStringNotMatch();
50405008
}
50415009

50425010
@Override

core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,7 +3051,16 @@ public static double toDoubleValue(Object value) {
30513051
return 0;
30523052
}
30533053

3054-
throw new JSONException("can not cast to decimal");
3054+
if (value instanceof Collection && ((Collection<?>) value).size() == 1) {
3055+
Object first = ((Collection<?>) value).iterator().next();
3056+
if (first instanceof Number) {
3057+
return ((Number) first).doubleValue();
3058+
} else if (first instanceof String) {
3059+
return Double.parseDouble((String) first);
3060+
}
3061+
}
3062+
3063+
throw new JSONException("can not cast to double");
30553064
}
30563065

30573066
public static Double toDouble(Object value) {
@@ -3071,7 +3080,7 @@ public static Double toDouble(Object value) {
30713080
return Double.parseDouble(str);
30723081
}
30733082

3074-
throw new JSONException("can not cast to decimal");
3083+
throw new JSONException("can not cast to double");
30753084
}
30763085

30773086
public static int compare(Object a, Object b) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.alibaba.fastjson2.issues_3200;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import lombok.Data;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class Issue3276 {
10+
@Test
11+
public void jsonSerializationTest3() {
12+
String json = "{\n" +
13+
" \"c\":[\"1.1\"]\n" +
14+
"}";
15+
16+
Bean bean = JSON.parseObject(json, Bean.class);
17+
assertEquals(1.1D, bean.getC());
18+
}
19+
20+
@Test
21+
public void jsonSerializationTest2() {
22+
String json = "{\n" +
23+
" \"b\":['1']\n" +
24+
"}";
25+
Bean bean = JSON.parseObject(json, Bean.class);
26+
assertEquals('1', bean.getB());
27+
}
28+
29+
@Data
30+
public static class Bean {
31+
private Character b;
32+
private Double c;
33+
}
34+
}

core/src/test/java/com/alibaba/fastjson2/read/ParserTest_string.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,23 @@ void lexerTest(JSONReader lexer) {
163163
assertEquals(Fnv.hashCode64("v12"), lexer.readFieldNameHashCode());
164164
assertEquals(Fnv.hashCode64LCase("v12"), lexer.getNameHashCodeLCase());
165165
assertEquals("v12", lexer.getFieldName());
166-
assertEquals("[1234]", lexer
166+
assertEquals("1234", lexer
167167
.readString());
168168

169169
assertEquals(Fnv.hashCode64("v13"), lexer.readFieldNameHashCode());
170170
assertEquals(Fnv.hashCode64LCase("v13"), lexer.getNameHashCodeLCase());
171171
assertEquals("v13", lexer.getFieldName());
172-
assertEquals("[12.34]", lexer.readString());
172+
assertEquals("12.34", lexer.readString());
173173

174174
assertEquals(Fnv.hashCode64("v14"), lexer.readFieldNameHashCode());
175175
assertEquals(Fnv.hashCode64LCase("v14"), lexer.getNameHashCodeLCase());
176176
assertEquals("v14", lexer.getFieldName());
177-
assertEquals("[null]",
178-
lexer.readString());
177+
assertNull(lexer.readString());
179178

180179
assertEquals(Fnv.hashCode64("v15"), lexer.readFieldNameHashCode());
181180
assertEquals(Fnv.hashCode64LCase("v15"), lexer.getNameHashCodeLCase());
182181
assertEquals("v15", lexer.getFieldName());
183-
assertEquals("[\"123\"]",
182+
assertEquals("123",
184183
lexer.readString());
185184

186185
assertEquals('}', lexer.current());

0 commit comments

Comments
 (0)