mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix put bug
This commit is contained in:
parent
6aba52fda8
commit
1755bbadcc
@ -18,7 +18,8 @@
|
||||
### 🐞Bug修复
|
||||
* 【db 】 修复count方法丢失参数问题(issue#I3VBSL@Gitee)
|
||||
* 【db 】 修复SpringUtil工具在`@PostConstruct` 注解标注的方法下失效问题(pr#341@Gitee)
|
||||
* 【db 】 修复JSONUtil.parse方法未判断有序问题(issue#I3VHVY@Gitee)
|
||||
* 【json 】 修复JSONUtil.parse方法未判断有序问题(issue#I3VHVY@Gitee)
|
||||
* 【json 】 修复JSONArray.put越界无法加入问题(issue#I3VMLU@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -93,8 +93,8 @@ public class RandomUtil {
|
||||
*
|
||||
* @param seed 随机数种子
|
||||
* @return {@link SecureRandom}
|
||||
* @since 5.5.2
|
||||
* @see #createSecureRandom(byte[])
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public static SecureRandom getSecureRandom(byte[] seed) {
|
||||
return createSecureRandom(seed);
|
||||
@ -119,7 +119,7 @@ public class RandomUtil {
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
if(null != seed){
|
||||
if (null != seed) {
|
||||
random.setSeed(seed);
|
||||
}
|
||||
return random;
|
||||
@ -163,6 +163,7 @@ public class RandomUtil {
|
||||
* 获得随机数int值
|
||||
*
|
||||
* @return 随机数
|
||||
* @see Random#nextInt()
|
||||
*/
|
||||
public static int randomInt() {
|
||||
return getRandom().nextInt();
|
||||
@ -173,6 +174,7 @@ public class RandomUtil {
|
||||
*
|
||||
* @param limit 限制随机数的范围,不包括这个数
|
||||
* @return 随机数
|
||||
* @see Random#nextInt(int)
|
||||
*/
|
||||
public static int randomInt(int limit) {
|
||||
return getRandom().nextInt(limit);
|
||||
@ -184,6 +186,7 @@ public class RandomUtil {
|
||||
* @param min 最小数(包含)
|
||||
* @param max 最大数(不包含)
|
||||
* @return 随机数
|
||||
* @see ThreadLocalRandom#nextLong(long, long)
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static long randomLong(long min, long max) {
|
||||
@ -194,6 +197,7 @@ public class RandomUtil {
|
||||
* 获得随机数
|
||||
*
|
||||
* @return 随机数
|
||||
* @see ThreadLocalRandom#nextLong()
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static long randomLong() {
|
||||
@ -205,6 +209,7 @@ public class RandomUtil {
|
||||
*
|
||||
* @param limit 限制随机数的范围,不包括这个数
|
||||
* @return 随机数
|
||||
* @see ThreadLocalRandom#nextLong(long)
|
||||
*/
|
||||
public static long randomLong(long limit) {
|
||||
return getRandom().nextLong(limit);
|
||||
@ -216,6 +221,7 @@ public class RandomUtil {
|
||||
* @param min 最小数(包含)
|
||||
* @param max 最大数(不包含)
|
||||
* @return 随机数
|
||||
* @see ThreadLocalRandom#nextDouble(double, double)
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static double randomDouble(double min, double max) {
|
||||
@ -240,6 +246,7 @@ public class RandomUtil {
|
||||
* 获得随机数[0, 1)
|
||||
*
|
||||
* @return 随机数
|
||||
* @see ThreadLocalRandom#nextDouble()
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static double randomDouble() {
|
||||
@ -263,6 +270,7 @@ public class RandomUtil {
|
||||
*
|
||||
* @param limit 限制随机数的范围,不包括这个数
|
||||
* @return 随机数
|
||||
* @see ThreadLocalRandom#nextDouble(double)
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static double randomDouble(double limit) {
|
||||
|
@ -457,6 +457,9 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
*/
|
||||
@Override
|
||||
public Object set(int index, Object element) {
|
||||
if(index > size()){
|
||||
add(index, element);
|
||||
}
|
||||
return this.rawList.set(index, JSONUtil.wrap(element, this.config));
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* JSONArray单元测试
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
@ -112,34 +112,34 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void toListTest2() {
|
||||
String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
|
||||
|
||||
|
||||
JSONArray array = JSONUtil.parseArray(jsonArr);
|
||||
List<User> userList = JSONUtil.toList(array, User.class);
|
||||
|
||||
|
||||
Assert.assertFalse(userList.isEmpty());
|
||||
Assert.assertSame(User.class, userList.get(0).getClass());
|
||||
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(111), userList.get(0).getId());
|
||||
Assert.assertEquals(Integer.valueOf(112), userList.get(1).getId());
|
||||
|
||||
|
||||
Assert.assertEquals("test1", userList.get(0).getName());
|
||||
Assert.assertEquals("test2", userList.get(1).getName());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toDictListTest() {
|
||||
String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
|
||||
|
||||
|
||||
JSONArray array = JSONUtil.parseArray(jsonArr);
|
||||
|
||||
|
||||
List<Dict> list = JSONUtil.toList(array, Dict.class);
|
||||
|
||||
|
||||
Assert.assertFalse(list.isEmpty());
|
||||
Assert.assertSame(Dict.class, list.get(0).getClass());
|
||||
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(111), list.get(0).getInt("id"));
|
||||
Assert.assertEquals(Integer.valueOf(112), list.get(1).getInt("id"));
|
||||
|
||||
|
||||
Assert.assertEquals("test1", list.get(0).getStr("name"));
|
||||
Assert.assertEquals("test2", list.get(1).getStr("name"));
|
||||
}
|
||||
@ -174,7 +174,7 @@ public class JSONArrayTest {
|
||||
String json = "[['aaa',{'akey':'avalue','bkey':'bvalue'}]]";
|
||||
JSONArray ja = JSONUtil.parseArray(json);
|
||||
|
||||
List<List<KeyBean>> list = ja.toBean(new TypeReference<List<List<KeyBean>>>() {});
|
||||
ja.toBean(new TypeReference<List<List<KeyBean>>>() {});
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -211,6 +211,14 @@ public class JSONArrayTest {
|
||||
Assert.assertEquals("b", JSONUtil.getByPath(jsonArray, "[1].name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putTest(){
|
||||
final JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put(3, "test");
|
||||
// 第三个位置插入值,0~2都是null
|
||||
Assert.assertEquals(4, jsonArray.size());
|
||||
}
|
||||
|
||||
private static Map<String, String> buildMap(String id, String parentId, String name) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
|
Loading…
x
Reference in New Issue
Block a user