Skip to content

Commit

Permalink
fix put bug
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jun 15, 2021
1 parent 6aba52f commit 1755bba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

-------------------------------------------------------------------------------------------------------------

Expand Down
12 changes: 10 additions & 2 deletions hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public static SecureRandom getSecureRandom() {
*
* @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);
Expand All @@ -119,7 +119,7 @@ public static SecureRandom getSHA1PRNGRandom(byte[] seed) {
} catch (NoSuchAlgorithmException e) {
throw new UtilException(e);
}
if(null != seed){
if (null != seed) {
random.setSeed(seed);
}
return random;
Expand Down Expand Up @@ -163,6 +163,7 @@ public static int randomInt(int min, int max) {
* 获得随机数int值
*
* @return 随机数
* @see Random#nextInt()
*/
public static int randomInt() {
return getRandom().nextInt();
Expand All @@ -173,6 +174,7 @@ public static int randomInt() {
*
* @param limit 限制随机数的范围,不包括这个数
* @return 随机数
* @see Random#nextInt(int)
*/
public static int randomInt(int limit) {
return getRandom().nextInt(limit);
Expand All @@ -184,6 +186,7 @@ public static int randomInt(int limit) {
* @param min 最小数(包含)
* @param max 最大数(不包含)
* @return 随机数
* @see ThreadLocalRandom#nextLong(long, long)
* @since 3.3.0
*/
public static long randomLong(long min, long max) {
Expand All @@ -194,6 +197,7 @@ public static long randomLong(long min, long max) {
* 获得随机数
*
* @return 随机数
* @see ThreadLocalRandom#nextLong()
* @since 3.3.0
*/
public static long randomLong() {
Expand All @@ -205,6 +209,7 @@ public static long randomLong() {
*
* @param limit 限制随机数的范围,不包括这个数
* @return 随机数
* @see ThreadLocalRandom#nextLong(long)
*/
public static long randomLong(long limit) {
return getRandom().nextLong(limit);
Expand All @@ -216,6 +221,7 @@ public static long randomLong(long limit) {
* @param min 最小数(包含)
* @param max 最大数(不包含)
* @return 随机数
* @see ThreadLocalRandom#nextDouble(double, double)
* @since 3.3.0
*/
public static double randomDouble(double min, double max) {
Expand All @@ -240,6 +246,7 @@ public static double randomDouble(double min, double max, int scale, RoundingMod
* 获得随机数[0, 1)
*
* @return 随机数
* @see ThreadLocalRandom#nextDouble()
* @since 3.3.0
*/
public static double randomDouble() {
Expand All @@ -263,6 +270,7 @@ public static double randomDouble(int scale, RoundingMode roundingMode) {
*
* @param limit 限制随机数的范围,不包括这个数
* @return 随机数
* @see ThreadLocalRandom#nextDouble(double)
* @since 3.3.0
*/
public static double randomDouble(double limit) {
Expand Down
3 changes: 3 additions & 0 deletions hutool-json/src/main/java/cn/hutool/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ public void clear() {
*/
@Override
public Object set(int index, Object element) {
if(index > size()){
add(index, element);
}
return this.rawList.set(index, JSONUtil.wrap(element, this.config));
}

Expand Down
32 changes: 20 additions & 12 deletions hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* JSONArray单元测试
*
*
* @author Looly
*
*/
Expand Down Expand Up @@ -112,34 +112,34 @@ public void toListTest() {
@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"));
}
Expand Down Expand Up @@ -174,7 +174,7 @@ public void toListWithErrorTest(){
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
Expand Down Expand Up @@ -211,6 +211,14 @@ public void getByPathTest(){
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);
Expand Down

0 comments on commit 1755bba

Please sign in to comment.