|
1 | 1 | package com.alibaba.fastjson2.issues_2700; |
2 | 2 |
|
3 | | -import com.alibaba.fastjson2.JSON; |
4 | 3 | import com.alibaba.fastjson2.JSONException; |
5 | | -import com.alibaba.fastjson2_vo.Int1; |
| 4 | +import com.alibaba.fastjson2.JSONReader; |
| 5 | +import com.alibaba.fastjson2_vo.Long1; |
6 | 6 | import org.junit.jupiter.api.Test; |
7 | 7 |
|
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | + |
8 | 10 | import static org.junit.jupiter.api.Assertions.*; |
9 | 11 |
|
10 | 12 | public class Issue2749 { |
11 | 13 | @Test |
12 | 14 | public void test() { |
13 | 15 | String str = "{\"v0000\":\"Alexander77\"}"; |
14 | | - { |
15 | | - JSONException error = null; |
16 | | - try { |
17 | | - JSON.parseObject(str, Int1.class); |
18 | | - } catch (JSONException e) { |
19 | | - error = e; |
20 | | - } |
21 | | - assertNotNull(error); |
22 | | - assertTrue(error.getMessage().contains("parseInt error")); |
23 | | - assertTrue(error.getMessage().contains("Alexander77")); |
| 16 | + try (JSONReader jsonReader = JSONReader.of(str)) { |
| 17 | + validate(jsonReader); |
| 18 | + } |
| 19 | + try (JSONReader jsonReader = JSONReader.of(str.toCharArray())) { |
| 20 | + validate(jsonReader); |
| 21 | + } |
| 22 | + |
| 23 | + byte[] bytes = str.getBytes(StandardCharsets.UTF_8); |
| 24 | + try (JSONReader jsonReader = JSONReader.of(bytes)) { |
| 25 | + validate(jsonReader); |
24 | 26 | } |
25 | | - { |
26 | | - JSONException error = null; |
27 | | - try { |
28 | | - JSON.parseObject(str.toCharArray(), Int1.class); |
29 | | - } catch (JSONException e) { |
30 | | - error = e; |
31 | | - } |
32 | | - assertNotNull(error); |
33 | | - assertTrue(error.getMessage().contains("parseInt error")); |
34 | | - assertTrue(error.getMessage().contains("Alexander77")); |
| 27 | + try (JSONReader jsonReader = JSONReader.of(bytes, 0, bytes.length, StandardCharsets.ISO_8859_1)) { |
| 28 | + validate(jsonReader); |
35 | 29 | } |
36 | | - { |
37 | | - JSONException error = null; |
38 | | - try { |
39 | | - JSON.parseObject(str.getBytes(), Int1.class); |
40 | | - } catch (JSONException e) { |
41 | | - error = e; |
42 | | - } |
43 | | - assertNotNull(error); |
44 | | - assertTrue(error.getMessage().contains("parseInt error")); |
45 | | - assertTrue(error.getMessage().contains("Alexander77")); |
| 30 | + } |
| 31 | + |
| 32 | + private static void validate(JSONReader jsonReader) { |
| 33 | + JSONException error = null; |
| 34 | + try { |
| 35 | + jsonReader.read(Long1.class); |
| 36 | + } catch (JSONException e) { |
| 37 | + error = e; |
46 | 38 | } |
| 39 | + assertTrue(error.getMessage().contains("parseLong error")); |
| 40 | + assertTrue(error.getMessage().contains("Alexander77")); |
47 | 41 | } |
48 | 42 | } |
0 commit comments