mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
7f9db2bbdc
commit
e67434c8cc
@ -16,21 +16,34 @@
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class JSONFactoryTest {
|
||||
@Test
|
||||
void parseFromStringBuilderTest() {
|
||||
final String jsonStr = "{\"name\":\"张三\"}";
|
||||
final JSON parse = JSONFactory.getInstance().parse(new StringBuilder(jsonStr));
|
||||
Assertions.assertEquals(JSONObject.class, parse.getClass());
|
||||
assertEquals(JSONObject.class, parse.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseFromStringTest() {
|
||||
final String jsonStr = "{\"name\":\"张三\"}";
|
||||
final JSON parse = JSONFactory.getInstance().parse(jsonStr);
|
||||
Assertions.assertEquals(JSONObject.class, parse.getClass());
|
||||
assertEquals(JSONObject.class, parse.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseAsNumberTest() {
|
||||
final JSON json = JSONFactory.getInstance().parse("123");
|
||||
assertEquals(Integer.class, json.asJSONPrimitive().getValue().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJSONTest() {
|
||||
final JSON json = JSONFactory.getInstance().toJSON("123");
|
||||
assertEquals(String.class, json.asJSONPrimitive().getValue().getClass());
|
||||
}
|
||||
}
|
||||
|
@ -19,34 +19,43 @@ package org.dromara.hutool.json.issues;
|
||||
import org.dromara.hutool.core.lang.Opt;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class Issue3681Test {
|
||||
@Test
|
||||
void toJsonStrOfOptionalTest() {
|
||||
String abc = JSONUtil.toJsonStr(Optional.of("abc"));
|
||||
Assertions.assertEquals("\"abc\"", abc);
|
||||
assertEquals("\"abc\"", abc);
|
||||
|
||||
// 默认解析
|
||||
abc = JSONUtil.toJsonStr(Optional.of("123"));
|
||||
Assertions.assertEquals("123", abc);
|
||||
assertEquals("123", abc);
|
||||
|
||||
// 按照字符串对待
|
||||
abc = JSONUtil.toJSON(Optional.of("123")).toString();
|
||||
assertEquals("\"123\"", abc);
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJsonStrOfOptionalTest2() {
|
||||
final String abc = JSONUtil.toJsonStr(Optional.of(MapUtil.of("a", 1)));
|
||||
Assertions.assertEquals("{\"a\":1}", abc);
|
||||
assertEquals("{\"a\":1}", abc);
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJsonStrOfOptTest() {
|
||||
String abc = JSONUtil.toJsonStr(Opt.of("abc"));
|
||||
Assertions.assertEquals("\"abc\"", abc);
|
||||
assertEquals("\"abc\"", abc);
|
||||
|
||||
abc = JSONUtil.toJsonStr(Opt.of("123"));
|
||||
Assertions.assertEquals("123", abc);
|
||||
assertEquals("123", abc);
|
||||
|
||||
// 按照字符串对待
|
||||
abc = JSONUtil.toJSON(Opt.of("123")).toString();
|
||||
assertEquals("\"123\"", abc);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user