This commit is contained in:
Looly
2022-04-18 21:50:33 +08:00
parent 571d76f578
commit 5a1ae69c21
3 changed files with 56 additions and 5 deletions

View File

@@ -28,7 +28,10 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
@@ -143,6 +146,37 @@ public class JSONObjectTest {
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
}
@Test
public void parseBytesTest() {
String jsonStr = "{'msg':'这里还没有内容','data':{'cards':[]},'ok':0}";
//noinspection MismatchedQueryAndUpdateOfCollection
JSONObject json = new JSONObject(jsonStr.getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(new Integer(0), json.getInt("ok"));
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
}
@Test
public void parseReaderTest() {
String jsonStr = "{'msg':'这里还没有内容','data':{'cards':[]},'ok':0}";
final StringReader stringReader = new StringReader(jsonStr);
//noinspection MismatchedQueryAndUpdateOfCollection
JSONObject json = new JSONObject(stringReader);
Assert.assertEquals(new Integer(0), json.getInt("ok"));
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
}
@Test
public void parseInputStreamTest() {
String jsonStr = "{'msg':'这里还没有内容','data':{'cards':[]},'ok':0}";
final ByteArrayInputStream in = new ByteArrayInputStream(jsonStr.getBytes(StandardCharsets.UTF_8));
//noinspection MismatchedQueryAndUpdateOfCollection
JSONObject json = new JSONObject(in);
Assert.assertEquals(new Integer(0), json.getInt("ok"));
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
}
@Test
@Ignore
public void parseStringWithBomTest() {