This commit is contained in:
Looly 2023-06-29 10:26:36 +08:00
parent 824bf11d05
commit 05617ff356
3 changed files with 54 additions and 2 deletions

View File

@ -32,7 +32,11 @@ import org.dromara.hutool.core.util.ByteUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.util.ObjUtil;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.text.MessageFormat;
import java.text.Normalizer;
import java.util.HashSet;
@ -3183,6 +3187,47 @@ public class CharSequenceUtil extends StrValidator {
}
return sub(string, 0, length) + "...";
}
/**
* 截断字符串使用其按照指定编码为字节后不超过maxBytes长度
*
* @param str 原始字符串
* @param charset 指定编码
* @param maxBytesLength 最大字节数
* @param factor 速算因子取该编码下单个字符的最大可能字节数
* @param appendDots 截断后是否追加省略号(...)
* @return 限制后的长度
*/
public static String limitByteLength(final String str, final Charset charset, final int maxBytesLength,
final int factor, final boolean appendDots) {
//字符数*速算因子<=最大字节数
if (str == null || str.length() * factor <= maxBytesLength) {
return str;
}
final byte[] sba = str.getBytes(charset);
if (sba.length <= maxBytesLength) {
return str;
}
//限制字节数
final int limitBytes;
if (appendDots) {
limitBytes = maxBytesLength - "...".getBytes(charset).length;
} else {
limitBytes = maxBytesLength;
}
final ByteBuffer bb = ByteBuffer.wrap(sba, 0, limitBytes);
final CharBuffer cb = CharBuffer.allocate(limitBytes);
final CharsetDecoder decoder = charset.newDecoder();
//忽略被截断的字符
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.decode(bb, cb, true);
decoder.flush(cb);
final String result = new String(cb.array(), 0, cb.position());
if (appendDots) {
return result + "...";
}
return result;
}
// endregion
// region ----- firstXXX

View File

@ -124,7 +124,7 @@ public class JSONConverter implements Converter {
* <li>String: 转换为相应的对象"和'包围的字符串返回原字符串,""返回{@code null}</li>
* <li>ArrayIterableIterator转换为JSONArray</li>
* <li>Bean对象转为JSONObject</li>
* <li>Number返回原对象</li>
* <li>NumberBoolean返回原对象</li>
* <li>null返回{@code null}</li>
* </ul>
*
@ -138,7 +138,7 @@ public class JSONConverter implements Converter {
return null;
}
final JSON json;
if (obj instanceof JSON || obj instanceof Number) {
if (obj instanceof JSON || obj instanceof Number || obj instanceof Boolean) {
return obj;
} else if (obj instanceof CharSequence) {
final String jsonStr = StrUtil.trim((CharSequence) obj);

View File

@ -2,6 +2,7 @@ package org.dromara.hutool.json;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.json.serialize.JSONStringer;
@ -334,4 +335,10 @@ public class JSONUtilTest {
private Byte[] d = new Byte[0];
private Byte[] e = new Byte[1];
}
@Test
void toJsonStrOfBooleanTest() {
final String jsonStr = JSONUtil.toJsonStr(true);
Assertions.assertEquals("true", jsonStr);
}
}