mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix bug
This commit is contained in:
parent
1804b7e6da
commit
887afc8984
@ -16,6 +16,7 @@ import org.dromara.hutool.core.convert.impl.*;
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.lang.Opt;
|
||||
import org.dromara.hutool.core.lang.tuple.Pair;
|
||||
import org.dromara.hutool.core.lang.tuple.Triple;
|
||||
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
|
||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
|
||||
@ -216,6 +217,7 @@ public class RegisterConverter implements Converter, Serializable {
|
||||
defaultConverterMap.put(StackTraceElement.class, new StackTraceElementConverter());// since 4.5.2
|
||||
defaultConverterMap.put(Optional.class, new OptionalConverter());// since 5.0.0
|
||||
defaultConverterMap.put(Opt.class, new OptConverter());// since 5.7.16
|
||||
defaultConverterMap.put(Pair.class, PairConverter.INSTANCE);// since 5.7.16
|
||||
defaultConverterMap.put(Pair.class, PairConverter.INSTANCE);// since 6.0.0
|
||||
defaultConverterMap.put(Triple.class, TripleConverter.INSTANCE);// since 6.0.0
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ import java.util.Map;
|
||||
* <li>{@link Map}</li>
|
||||
* <li>{@link Map.Entry}</li>
|
||||
* <li>带分隔符的字符串,支持分隔符{@code :}、{@code =}、{@code ,}</li>
|
||||
* <li>Bean,包含{@code getKey}和{@code getValue}方法</li>
|
||||
* <li>Bean,包含{@code getLeft}和{@code getRight}方法</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author looly
|
||||
@ -49,23 +49,23 @@ public class PairConverter implements Converter {
|
||||
if (targetType instanceof TypeReference) {
|
||||
targetType = ((TypeReference<?>) targetType).getType();
|
||||
}
|
||||
final Type keyType = TypeUtil.getTypeArgument(targetType, 0);
|
||||
final Type valueType = TypeUtil.getTypeArgument(targetType, 1);
|
||||
final Type leftType = TypeUtil.getTypeArgument(targetType, 0);
|
||||
final Type rightType = TypeUtil.getTypeArgument(targetType, 1);
|
||||
|
||||
return convert(keyType, valueType, value);
|
||||
return convert(leftType, rightType, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换对象为指定键值类型的指定类型Map
|
||||
*
|
||||
* @param keyType 键类型
|
||||
* @param valueType 值类型
|
||||
* @param leftType 键类型
|
||||
* @param rightType 值类型
|
||||
* @param value 被转换的值
|
||||
* @return 转换后的Map
|
||||
* @throws ConvertException 转换异常或不支持的类型
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Pair<?, ?> convert(final Type keyType, final Type valueType, final Object value)
|
||||
public Pair<?, ?> convert(final Type leftType, final Type rightType, final Object value)
|
||||
throws ConvertException {
|
||||
Map map = null;
|
||||
if (value instanceof Map.Entry) {
|
||||
@ -79,12 +79,12 @@ public class PairConverter implements Converter {
|
||||
} else if (value instanceof CharSequence) {
|
||||
final CharSequence str = (CharSequence) value;
|
||||
map = strToMap(str);
|
||||
} else if (BeanUtil.isWritableBean(value.getClass())) {
|
||||
} else if (BeanUtil.isReadableBean(value.getClass())) {
|
||||
map = BeanUtil.beanToMap(value);
|
||||
}
|
||||
|
||||
if (null != map) {
|
||||
return mapToPair(keyType, valueType, map);
|
||||
return mapToPair(leftType, rightType, map);
|
||||
}
|
||||
|
||||
throw new ConvertException("Unsupported to map from [{}] of type: {}", value, value.getClass().getName());
|
||||
@ -109,12 +109,12 @@ public class PairConverter implements Converter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Map转Entry
|
||||
* Map转Pair
|
||||
*
|
||||
* @param keyType 键类型
|
||||
* @param valueType 值类型
|
||||
* @param map 被转换的map
|
||||
* @return Entry
|
||||
* @return Pair
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Pair<?, ?> mapToPair(final Type keyType, final Type valueType, final Map map) {
|
||||
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.convert.impl;
|
||||
|
||||
import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.convert.CompositeConverter;
|
||||
import org.dromara.hutool.core.convert.ConvertException;
|
||||
import org.dromara.hutool.core.convert.Converter;
|
||||
import org.dromara.hutool.core.lang.tuple.Pair;
|
||||
import org.dromara.hutool.core.lang.tuple.Triple;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link Triple} 转换器,支持以下类型转为Triple:
|
||||
* <ul>
|
||||
* <li>Bean,包含{@code getLeft}、{@code getMiddle}和{@code getRight}方法</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class TripleConverter implements Converter {
|
||||
|
||||
/**
|
||||
* 单例
|
||||
*/
|
||||
public static final TripleConverter INSTANCE = new TripleConverter();
|
||||
|
||||
@Override
|
||||
public Object convert(Type targetType, final Object value) throws ConvertException {
|
||||
if (targetType instanceof TypeReference) {
|
||||
targetType = ((TypeReference<?>) targetType).getType();
|
||||
}
|
||||
final Type leftType = TypeUtil.getTypeArgument(targetType, 0);
|
||||
final Type middileType = TypeUtil.getTypeArgument(targetType, 1);
|
||||
final Type rightType = TypeUtil.getTypeArgument(targetType, 2);
|
||||
|
||||
return convert(leftType, middileType, rightType, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换对象为指定键值类型的指定类型Map
|
||||
*
|
||||
* @param leftType 键类型
|
||||
* @param middleType 中值类型
|
||||
* @param rightType 值类型
|
||||
* @param value 被转换的值
|
||||
* @return 转换后的Map
|
||||
* @throws ConvertException 转换异常或不支持的类型
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Triple<?, ?, ?> convert(final Type leftType, final Type middleType, final Type rightType, final Object value)
|
||||
throws ConvertException {
|
||||
Map map = null;
|
||||
if (BeanUtil.isReadableBean(value.getClass())) {
|
||||
map = BeanUtil.beanToMap(value);
|
||||
}
|
||||
|
||||
if (null != map) {
|
||||
return mapToTriple(leftType, middleType, rightType, map);
|
||||
}
|
||||
|
||||
throw new ConvertException("Unsupported to map from [{}] of type: {}", value, value.getClass().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Map转Entry
|
||||
*
|
||||
* @param leftType 键类型
|
||||
* @param rightType 值类型
|
||||
* @param map 被转换的map
|
||||
* @return Entry
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Triple<?, ?, ?> mapToTriple(final Type leftType, final Type middleType, final Type rightType, final Map map) {
|
||||
|
||||
final Object left = map.get("left");
|
||||
final Object middle = map.get("middle");
|
||||
final Object right = map.get("right");
|
||||
|
||||
final CompositeConverter convert = CompositeConverter.getInstance();
|
||||
return Triple.of(
|
||||
TypeUtil.isUnknown(leftType) ? left : convert.convert(leftType, left),
|
||||
TypeUtil.isUnknown(middleType) ? middle : convert.convert(middleType, middle),
|
||||
TypeUtil.isUnknown(rightType) ? right : convert.convert(rightType, right)
|
||||
);
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.lang.tuple.Pair;
|
||||
import org.dromara.hutool.core.lang.tuple.Triple;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -20,9 +21,17 @@ import org.junit.jupiter.api.Test;
|
||||
public class IssueI7GPGXTest {
|
||||
@Test
|
||||
public void pairToBeanTest() {
|
||||
final Pair<String, Boolean> hutoolPair = new Pair<>("test1", true);
|
||||
final Pair<String, Boolean> hutoolPair = Pair.of("test1", true);
|
||||
final String a = JSONUtil.toJsonStr(hutoolPair);
|
||||
final Pair<String, Boolean> pair = JSONUtil.toBean(a, new TypeReference<Pair<String, Boolean>>() {});
|
||||
Assertions.assertEquals(hutoolPair, pair);
|
||||
}
|
||||
|
||||
@Test
|
||||
void tripleToBeanTest() {
|
||||
final Triple<String, Integer, Boolean> hutoolTriple = Triple.of("aaa", 123, true);
|
||||
final String a = JSONUtil.toJsonStr(hutoolTriple);
|
||||
final Triple<String, Integer, Boolean> pair = JSONUtil.toBean(a, new TypeReference<Triple<String, Integer, Boolean>>() {});
|
||||
Assertions.assertEquals(hutoolTriple, pair);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user