diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/hash/MetroHashTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/hash/MetroHashTest.java index 58b31fe3d..5577a1bb5 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/hash/MetroHashTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/hash/MetroHashTest.java @@ -3,13 +3,12 @@ package cn.hutool.core.lang.hash; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.HexUtil; +import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; -import java.util.Random; - /** * https://gitee.com/dromara/hutool/pulls/532 */ @@ -41,7 +40,7 @@ public class MetroHashTest { @Test @Ignore public void bulkHashing64Test() { - String[] strArray = getRandomStringArray(10000000); + String[] strArray = getRandomStringArray(); long startCity = System.currentTimeMillis(); for (String s : strArray) { CityHash.hash64(s.getBytes()); @@ -65,7 +64,7 @@ public class MetroHashTest { @Test @Ignore public void bulkHashing128Test() { - String[] strArray = getRandomStringArray(10000000); + String[] strArray = getRandomStringArray(); long startCity = System.currentTimeMillis(); for (String s : strArray) { CityHash.hash128(s.getBytes()); @@ -83,26 +82,12 @@ public class MetroHashTest { } - private static String[] getRandomStringArray(int length) { - String[] result = new String[length]; - Random random = new Random(); + private static String[] getRandomStringArray() { + String[] result = new String[10000000]; int index = 0; - while (index < length) { - result[index++] = getRandomString(random.nextInt(64)); + while (index < 10000000) { + result[index++] = RandomUtil.randomString(RandomUtil.randomInt(64)); } return result; } - - private static String getRandomString(int length) { - String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - Random random = new Random(); - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < length; i++) { - int number = random.nextInt(62); - sb.append(str.charAt(number)); - } - return sb.toString(); - } - - } diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java index a7582edd4..6442d84c7 100644 --- a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java @@ -592,9 +592,14 @@ public class JSONObject implements JSON, JSONGetter, Map final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config) .beginObj(); this.forEach((key, value) -> { - final MutablePair pair = new MutablePair<>(key, value); - if (null == filter || filter.accept(pair)) { - jsonWriter.writeField(pair.getKey(), pair.getValue()); + if (null != filter){ + final MutablePair pair = new MutablePair<>(key, value); + if (filter.accept(pair)) { + // 使用修改后的键值对 + jsonWriter.writeField(pair.getKey(), pair.getValue()); + } + } else { + jsonWriter.writeField(key, value); } }); jsonWriter.end(); diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java index 3b18c8059..afa1616f0 100644 --- a/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java +++ b/hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java @@ -30,7 +30,6 @@ import org.junit.Test; import java.math.BigDecimal; import java.sql.Timestamp; -import java.time.LocalDate; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -658,6 +657,21 @@ public class JSONObjectTest { Assert.assertEquals("{\"b\":\"value2_edit\"}", s); } + @Test + public void toUnderLineCaseTest() { + JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true)) + .set("aKey", "value1") + .set("bJob", "value2") + .set("cGood", "value3") + .set("d", true); + + final String s = json1.toJSONString(0, (pair) -> { + pair.setKey(StrUtil.toUnderlineCase(pair.getKey())); + return true; + }); + Assert.assertEquals("{\"a_key\":\"value1\",\"b_job\":\"value2\",\"c_good\":\"value3\",\"d\":true}", s); + } + @Test public void nullToEmptyTest() { JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true).setIgnoreNullValue(false))