mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
d941fa85a7
commit
6d1f500bb2
@ -23,6 +23,7 @@ import org.dromara.hutool.core.func.SerSupplier;
|
|||||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||||
import org.dromara.hutool.core.map.MapUtil;
|
import org.dromara.hutool.core.map.MapUtil;
|
||||||
import org.dromara.hutool.core.map.MapWrapper;
|
import org.dromara.hutool.core.map.MapWrapper;
|
||||||
|
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
import org.dromara.hutool.json.support.InternalJSONUtil;
|
import org.dromara.hutool.json.support.InternalJSONUtil;
|
||||||
@ -52,6 +53,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
|||||||
private final JSONFactory factory;
|
private final JSONFactory factory;
|
||||||
|
|
||||||
// region ----- 构造
|
// region ----- 构造
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造,初始容量为 {@link #DEFAULT_CAPACITY},KEY有序
|
* 构造,初始容量为 {@link #DEFAULT_CAPACITY},KEY有序
|
||||||
*/
|
*/
|
||||||
@ -113,6 +115,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// region ----- get
|
// region ----- get
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据lambda的方法引用,获取
|
* 根据lambda的方法引用,获取
|
||||||
*
|
*
|
||||||
@ -145,6 +148,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ----- increment or append
|
// region ----- increment or append
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
|
* 对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
|
||||||
*
|
*
|
||||||
@ -197,6 +201,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ----- put
|
// region ----- put
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过lambda批量设置值<br>
|
* 通过lambda批量设置值<br>
|
||||||
* 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
|
* 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
|
||||||
@ -339,6 +344,18 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
|||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将JSONObject转换为指定类型的Map
|
||||||
|
*
|
||||||
|
* @param keyType 键类型Class
|
||||||
|
* @param valueType 值类型Class
|
||||||
|
* @param <K> 键类型
|
||||||
|
* @param <V> 值类型
|
||||||
|
* @return Map
|
||||||
|
*/
|
||||||
|
public <K, V> Map<K, V> toMap(final Class<K> keyType, final Class<V> valueType) {
|
||||||
|
return toBean(TypeUtil.createParameterizedType(Map.class, keyType, valueType));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -87,13 +87,27 @@ public class MapTypeAdapter implements MatcherJSONSerializer<Map<?, ?>>, Matcher
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
||||||
@Override
|
@Override
|
||||||
public Map<?, ?> deserialize(final JSON json, final Type deserializeType) {
|
public Map<?, ?> deserialize(final JSON json, final Type deserializeType) {
|
||||||
final Map map = MapUtil.createMap(TypeUtil.getClass(deserializeType), LinkedHashMap::new);
|
|
||||||
final Type keyType = TypeUtil.getTypeArgument(deserializeType, 0);
|
final Type keyType = TypeUtil.getTypeArgument(deserializeType, 0);
|
||||||
final Type valueType = TypeUtil.getTypeArgument(deserializeType, 1);
|
final Type valueType = TypeUtil.getTypeArgument(deserializeType, 1);
|
||||||
|
return toMap(json,
|
||||||
|
TypeUtil.getClass(deserializeType),
|
||||||
|
keyType,
|
||||||
|
valueType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将JSON对象转换为Map
|
||||||
|
*
|
||||||
|
* @param json JSON对象
|
||||||
|
* @param mapClass Map类型
|
||||||
|
* @param keyType 键类型
|
||||||
|
* @param valueType 值类型
|
||||||
|
* @return Map
|
||||||
|
*/
|
||||||
|
public Map<?, ?> toMap(final JSON json, final Class<?> mapClass, final Type keyType, final Type valueType) {
|
||||||
|
final Map<?, ?> map = MapUtil.createMap(mapClass, LinkedHashMap::new);
|
||||||
for (final Map.Entry<String, JSON> entry : (JSONObject) json) {
|
for (final Map.Entry<String, JSON> entry : (JSONObject) json) {
|
||||||
map.put(
|
map.put(
|
||||||
// key类型为String转目标类型,使用标准转换器
|
// key类型为String转目标类型,使用标准转换器
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.json.issues;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.reflect.TypeReference;
|
||||||
|
import org.dromara.hutool.json.JSONObject;
|
||||||
|
import org.dromara.hutool.json.JSONUtil;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class IssueIAWE3HTest {
|
||||||
|
@Test
|
||||||
|
void toMapTest() {
|
||||||
|
final String jsonStr = "{\n" +
|
||||||
|
" \"reply\": \"hi\",\n" +
|
||||||
|
" \"solved\": true,\n" +
|
||||||
|
" \"notifyTypes\": [\n" +
|
||||||
|
" \"push\"\n" +
|
||||||
|
" ]\n" +
|
||||||
|
"}";
|
||||||
|
|
||||||
|
final JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
|
||||||
|
|
||||||
|
final Map<String, Object> map = jsonObject.toBean(new TypeReference<Map<String, Object>>() {});
|
||||||
|
Assertions.assertEquals("hi", map.get("reply"));
|
||||||
|
|
||||||
|
final Map<String, Object> map2 = jsonObject.toMap(String.class, Object.class);
|
||||||
|
Assertions.assertEquals("hi", map2.get("reply"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user