This commit is contained in:
Looly 2022-02-13 22:04:02 +08:00
parent 9439ec4228
commit 0189de2dad
3 changed files with 30 additions and 26 deletions

View File

@ -3,13 +3,12 @@ package cn.hutool.core.lang.hash;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.util.Random;
/** /**
* https://gitee.com/dromara/hutool/pulls/532 * https://gitee.com/dromara/hutool/pulls/532
*/ */
@ -41,7 +40,7 @@ public class MetroHashTest {
@Test @Test
@Ignore @Ignore
public void bulkHashing64Test() { public void bulkHashing64Test() {
String[] strArray = getRandomStringArray(10000000); String[] strArray = getRandomStringArray();
long startCity = System.currentTimeMillis(); long startCity = System.currentTimeMillis();
for (String s : strArray) { for (String s : strArray) {
CityHash.hash64(s.getBytes()); CityHash.hash64(s.getBytes());
@ -65,7 +64,7 @@ public class MetroHashTest {
@Test @Test
@Ignore @Ignore
public void bulkHashing128Test() { public void bulkHashing128Test() {
String[] strArray = getRandomStringArray(10000000); String[] strArray = getRandomStringArray();
long startCity = System.currentTimeMillis(); long startCity = System.currentTimeMillis();
for (String s : strArray) { for (String s : strArray) {
CityHash.hash128(s.getBytes()); CityHash.hash128(s.getBytes());
@ -83,26 +82,12 @@ public class MetroHashTest {
} }
private static String[] getRandomStringArray(int length) { private static String[] getRandomStringArray() {
String[] result = new String[length]; String[] result = new String[10000000];
Random random = new Random();
int index = 0; int index = 0;
while (index < length) { while (index < 10000000) {
result[index++] = getRandomString(random.nextInt(64)); result[index++] = RandomUtil.randomString(RandomUtil.randomInt(64));
} }
return result; 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();
}
} }

View File

@ -592,9 +592,14 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config) final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
.beginObj(); .beginObj();
this.forEach((key, value) -> { this.forEach((key, value) -> {
final MutablePair<String, Object> pair = new MutablePair<>(key, value); if (null != filter){
if (null == filter || filter.accept(pair)) { final MutablePair<String, Object> pair = new MutablePair<>(key, value);
jsonWriter.writeField(pair.getKey(), pair.getValue()); if (filter.accept(pair)) {
// 使用修改后的键值对
jsonWriter.writeField(pair.getKey(), pair.getValue());
}
} else {
jsonWriter.writeField(key, value);
} }
}); });
jsonWriter.end(); jsonWriter.end();

View File

@ -30,7 +30,6 @@ import org.junit.Test;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -658,6 +657,21 @@ public class JSONObjectTest {
Assert.assertEquals("{\"b\":\"value2_edit\"}", s); 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 @Test
public void nullToEmptyTest() { public void nullToEmptyTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true).setIgnoreNullValue(false)) JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true).setIgnoreNullValue(false))