-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix WriteNonStringValueAsString not work on Boolean type, for issue #…
- Loading branch information
Showing
5 changed files
with
63 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/test/java/com/alibaba/fastjson2/issues_2500/Issue2560.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.alibaba.fastjson2.issues_2500; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Issue2560 { | ||
@Test | ||
public void mutatedTest() { | ||
List<TestJson> list = new ArrayList<>(); | ||
list.add(new TestJson(true, Boolean.TRUE)); | ||
list.add(new TestJson(false, Boolean.FALSE)); | ||
String json = JSON.toJSONString(list, JSONWriter.Feature.WriteNonStringValueAsString); | ||
List<TestJson> list2 = JSON.parseArray(json, TestJson.class); | ||
assertEquals(list, list2); | ||
String expected = "[{\"b\":\"true\",\"b2\":\"true\"},{\"b\":\"false\",\"b2\":\"false\"}]"; | ||
assertEquals(expected, json); | ||
assertEquals(expected, new String(JSON.toJSONBytes(list, JSONWriter.Feature.WriteNonStringValueAsString))); | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public static class TestJson { | ||
private boolean b; | ||
private Boolean b2; | ||
} | ||
|
||
@Test | ||
public void mutatedTest1() { | ||
List<TestJson1> list = new ArrayList<>(); | ||
list.add(new TestJson1(1, 1)); | ||
list.add(new TestJson1(2, 2)); | ||
String json = JSON.toJSONString(list, JSONWriter.Feature.WriteNonStringValueAsString); | ||
System.out.println(json); | ||
List<TestJson1> list2 = JSON.parseArray(json, TestJson1.class); | ||
assertEquals(list, list2); | ||
assertEquals("[{\"a\":\"1\",\"a2\":\"1\"},{\"a\":\"2\",\"a2\":\"2\"}]", json); | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public static class TestJson1 { | ||
private int a; | ||
private Integer a2; | ||
} | ||
} |